Merge pull request #132965 from serathius/metrics-storage-objects

Deprecate apiserver_storage_objects and replace it with apiserver_resource_objects metric using labels consistent with other metrics

Kubernetes-commit: 356d0b9f1834c336abb190fda0033eb6adeb2726
This commit is contained in:
Kubernetes Publisher 2025-07-17 14:18:31 -07:00
commit c0b5cc212d
2 changed files with 34 additions and 8 deletions

View File

@ -72,9 +72,10 @@ var (
)
objectCounts = compbasemetrics.NewGaugeVec(
&compbasemetrics.GaugeOpts{
Name: "apiserver_storage_objects",
Help: "Number of stored objects at the time of last check split by kind. In case of a fetching error, the value will be -1.",
StabilityLevel: compbasemetrics.STABLE,
Name: "apiserver_storage_objects",
Help: "Number of stored objects at the time of last check split by kind. In case of a fetching error, the value will be -1.",
StabilityLevel: compbasemetrics.STABLE,
DeprecatedVersion: "1.34.0",
},
[]string{"resource"},
)
@ -86,6 +87,14 @@ var (
},
[]string{"group", "resource"},
)
newObjectCounts = compbasemetrics.NewGaugeVec(
&compbasemetrics.GaugeOpts{
Name: "apiserver_resource_objects",
Help: "Number of stored objects at the time of last check split by kind. In case of a fetching error, the value will be -1.",
StabilityLevel: compbasemetrics.ALPHA,
},
[]string{"group", "resource"},
)
dbTotalSize = compbasemetrics.NewGaugeVec(
&compbasemetrics.GaugeOpts{
Subsystem: "apiserver",
@ -178,6 +187,7 @@ func Register() {
legacyregistry.MustRegister(etcdRequestErrorCounts)
legacyregistry.MustRegister(objectCounts)
legacyregistry.MustRegister(resourceSizeEstimate)
legacyregistry.MustRegister(newObjectCounts)
legacyregistry.MustRegister(dbTotalSize)
legacyregistry.CustomMustRegister(storageMonitor)
legacyregistry.MustRegister(etcdEventsReceivedCounts)
@ -195,12 +205,14 @@ func Register() {
func UpdateStoreStats(groupResource schema.GroupResource, stats storage.Stats, err error) {
if err != nil {
objectCounts.WithLabelValues(groupResource.String()).Set(-1)
newObjectCounts.WithLabelValues(groupResource.Group, groupResource.Resource).Set(-1)
if utilfeature.DefaultFeatureGate.Enabled(features.SizeBasedListCostEstimate) {
resourceSizeEstimate.WithLabelValues(groupResource.Group, groupResource.Resource).Set(-1)
}
return
}
objectCounts.WithLabelValues(groupResource.String()).Set(float64(stats.ObjectCount))
newObjectCounts.WithLabelValues(groupResource.Group, groupResource.Resource).Set(float64(stats.ObjectCount))
if utilfeature.DefaultFeatureGate.Enabled(features.SizeBasedListCostEstimate) {
if stats.ObjectCount > 0 && stats.EstimatedAverageObjectSizeBytes == 0 {
resourceSizeEstimate.WithLabelValues(groupResource.Group, groupResource.Resource).Set(-1)
@ -213,6 +225,7 @@ func UpdateStoreStats(groupResource schema.GroupResource, stats storage.Stats, e
// DeleteStoreStats delete the stats metrics.
func DeleteStoreStats(groupResource schema.GroupResource) {
objectCounts.DeleteLabelValues(groupResource.String())
newObjectCounts.DeleteLabelValues(groupResource.Group, groupResource.Resource)
if utilfeature.DefaultFeatureGate.Enabled(features.SizeBasedListCostEstimate) {
resourceSizeEstimate.DeleteLabelValues(groupResource.Group, groupResource.Resource)
}

View File

@ -230,6 +230,7 @@ func TestStorageSizeCollector(t *testing.T) {
func TestUpdateStoreStats(t *testing.T) {
registry := metrics.NewKubeRegistry()
registry.Register(objectCounts)
registry.MustRegister(newObjectCounts)
registry.MustRegister(resourceSizeEstimate)
testCases := []struct {
@ -243,7 +244,10 @@ func TestUpdateStoreStats(t *testing.T) {
desc: "successful object count",
resource: schema.GroupResource{Group: "foo", Resource: "bar"},
stats: storage.Stats{ObjectCount: 10},
want: `# HELP apiserver_resource_size_estimate_bytes [ALPHA] Estimated size of stored objects in database. Estimate is based on sum of last observed sizes of serialized objects. In case of a fetching error, the value will be -1.
want: `# HELP apiserver_resource_objects [ALPHA] 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_resource_objects gauge
apiserver_resource_objects{group="foo",resource="bar"} 10
# HELP apiserver_resource_size_estimate_bytes [ALPHA] Estimated size of stored objects in database. Estimate is based on sum of last observed sizes of serialized objects. In case of a fetching error, the value will be -1.
# TYPE apiserver_resource_size_estimate_bytes gauge
apiserver_resource_size_estimate_bytes{group="foo",resource="bar"} -1
# 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.
@ -255,7 +259,10 @@ apiserver_storage_objects{resource="bar.foo"} 10
desc: "successful object count and size",
resource: schema.GroupResource{Group: "foo", Resource: "bar"},
stats: storage.Stats{ObjectCount: 10, EstimatedAverageObjectSizeBytes: 10},
want: `# HELP apiserver_resource_size_estimate_bytes [ALPHA] Estimated size of stored objects in database. Estimate is based on sum of last observed sizes of serialized objects. In case of a fetching error, the value will be -1.
want: `# HELP apiserver_resource_objects [ALPHA] 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_resource_objects gauge
apiserver_resource_objects{group="foo",resource="bar"} 10
# HELP apiserver_resource_size_estimate_bytes [ALPHA] Estimated size of stored objects in database. Estimate is based on sum of last observed sizes of serialized objects. In case of a fetching error, the value will be -1.
# TYPE apiserver_resource_size_estimate_bytes gauge
apiserver_resource_size_estimate_bytes{group="foo",resource="bar"} 100
# 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.
@ -267,7 +274,10 @@ apiserver_storage_objects{resource="bar.foo"} 10
desc: "empty object count",
resource: schema.GroupResource{Group: "foo", Resource: "bar"},
stats: storage.Stats{ObjectCount: 0, EstimatedAverageObjectSizeBytes: 0},
want: `# HELP apiserver_resource_size_estimate_bytes [ALPHA] Estimated size of stored objects in database. Estimate is based on sum of last observed sizes of serialized objects. In case of a fetching error, the value will be -1.
want: `# HELP apiserver_resource_objects [ALPHA] 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_resource_objects gauge
apiserver_resource_objects{group="foo",resource="bar"} 0
# HELP apiserver_resource_size_estimate_bytes [ALPHA] Estimated size of stored objects in database. Estimate is based on sum of last observed sizes of serialized objects. In case of a fetching error, the value will be -1.
# TYPE apiserver_resource_size_estimate_bytes gauge
apiserver_resource_size_estimate_bytes{group="foo",resource="bar"} 0
# 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.
@ -279,7 +289,10 @@ apiserver_storage_objects{resource="bar.foo"} 0
desc: "failed fetch",
resource: schema.GroupResource{Group: "foo", Resource: "bar"},
err: errors.New("dummy"),
want: `# 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.
want: `# HELP apiserver_resource_objects [ALPHA] 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_resource_objects gauge
apiserver_resource_objects{group="foo",resource="bar"} -1
# 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="bar.foo"} -1
# HELP apiserver_resource_size_estimate_bytes [ALPHA] Estimated size of stored objects in database. Estimate is based on sum of last observed sizes of serialized objects. In case of a fetching error, the value will be -1.
@ -293,7 +306,7 @@ apiserver_resource_size_estimate_bytes{group="foo",resource="bar"} -1
t.Run(test.desc, func(t *testing.T) {
defer registry.Reset()
UpdateStoreStats(test.resource, test.stats, test.err)
if err := testutil.GatherAndCompare(registry, strings.NewReader(test.want), "apiserver_storage_objects", "apiserver_resource_size_estimate_bytes"); err != nil {
if err := testutil.GatherAndCompare(registry, strings.NewReader(test.want), "apiserver_storage_objects", "apiserver_resource_size_estimate_bytes", "apiserver_resource_objects"); err != nil {
t.Fatal(err)
}
})