Merge pull request #131902 from liuxu623/fix-storage-objects-metrics

remove apiserver_storage_objects metrics after crd deleted
This commit is contained in:
Kubernetes Prow Robot
2025-07-03 01:33:26 -07:00
committed by GitHub
3 changed files with 45 additions and 1 deletions

View File

@@ -1678,7 +1678,10 @@ func (e *Store) startObservingCount(period time.Duration, objectCountTracker flo
objectCountTracker.Set(resourceName, stats)
}
}, period, resourceCountPollPeriodJitter, true, stopCh)
return func() { close(stopCh) }
return func() {
metrics.DeleteObjectCount(e.DefaultQualifiedResource)
close(stopCh)
}
}
func (e *Store) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {

View File

@@ -184,6 +184,11 @@ func UpdateObjectCount(groupResource schema.GroupResource, count int64) {
objectCounts.WithLabelValues(groupResource.String()).Set(float64(count))
}
// DeleteObjectCount delete the apiserver_storage_object_counts metric.
func DeleteObjectCount(groupResource schema.GroupResource) {
objectCounts.DeleteLabelValues(groupResource.String())
}
// RecordEtcdRequest updates and sets the etcd_request_duration_seconds,
// etcd_request_total, etcd_request_errors_total metrics.
func RecordEtcdRequest(verb string, groupResource schema.GroupResource, err error, startTime time.Time) {

View File

@@ -268,6 +268,42 @@ apiserver_storage_objects{resource="bar.foo"} -1
}
}
func TestDeleteObjectCount(t *testing.T) {
registry := metrics.NewKubeRegistry()
registry.MustRegister(objectCounts)
testedMetrics := "apiserver_storage_objects"
UpdateObjectCount(schema.GroupResource{Group: "foo1", Resource: "bar1"}, int64(10))
UpdateObjectCount(schema.GroupResource{Group: "foo2", Resource: "bar2"}, int64(20))
expectedMetrics := `# HELP apiserver_storage_objects [STABLE] Number of stored objects at the time of last check split by kind. In case of a fetching error, the value will be -1.
# TYPE apiserver_storage_objects gauge
apiserver_storage_objects{resource="bar1.foo1"} 10
apiserver_storage_objects{resource="bar2.foo2"} 20
`
if err := testutil.GatherAndCompare(registry, strings.NewReader(expectedMetrics), testedMetrics); err != nil {
t.Fatal(err)
}
DeleteObjectCount(schema.GroupResource{Group: "foo1", Resource: "bar1"})
expectedMetrics = `# HELP apiserver_storage_objects [STABLE] Number of stored objects at the time of last check split by kind. In case of a fetching error, the value will be -1.
# TYPE apiserver_storage_objects gauge
apiserver_storage_objects{resource="bar2.foo2"} 20
`
if err := testutil.GatherAndCompare(registry, strings.NewReader(expectedMetrics), testedMetrics); err != nil {
t.Fatal(err)
}
DeleteObjectCount(schema.GroupResource{Group: "foo2", Resource: "bar2"})
expectedMetrics = `# HELP apiserver_storage_objects [STABLE] Number of stored objects at the time of last check split by kind. In case of a fetching error, the value will be -1.
# TYPE apiserver_storage_objects gauge
`
if err := testutil.GatherAndCompare(registry, strings.NewReader(expectedMetrics), testedMetrics); err != nil {
t.Fatal(err)
}
}
type fakeEtcdMonitor struct {
storageSize int64
}