Skip to content

Commit 9113f99

Browse files
committed
chore: Reduce code duplication for metadata metric families
1 parent 2e8c3bf commit 9113f99

File tree

3 files changed

+146
-210
lines changed

3 files changed

+146
-210
lines changed

internal/store/horizontalpodautoscaler.go

Lines changed: 4 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,15 @@ func (m metricTargetType) String() string {
4444
}
4545

4646
var (
47-
descHorizontalPodAutoscalerAnnotationsName = "kube_horizontalpodautoscaler_annotations"
48-
descHorizontalPodAutoscalerAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels."
49-
descHorizontalPodAutoscalerLabelsName = "kube_horizontalpodautoscaler_labels"
50-
descHorizontalPodAutoscalerLabelsHelp = "Kubernetes labels converted to Prometheus labels."
5147
descHorizontalPodAutoscalerLabelsDefaultLabels = []string{"namespace", "horizontalpodautoscaler"}
5248

5349
targetMetricLabels = []string{"metric_name", "metric_target_type"}
5450
)
5551

5652
func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator {
57-
return []generator.FamilyGenerator{
53+
metadataFamilies := createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descHorizontalPodAutoscalerLabelsDefaultLabels, "kube_horizontalpodautoscaler")
54+
55+
return append(metadataFamilies,
5856
createHPAInfo(),
5957
createHPAMetaDataGeneration(),
6058
createHPASpecMaxReplicas(),
@@ -63,12 +61,8 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
6361
createHPAStatusTargetMetric(),
6462
createHPAStatusCurrentReplicas(),
6563
createHPAStatusDesiredReplicas(),
66-
createHPAAnnotations(allowAnnotationsList),
67-
createHPALabels(allowLabelsList),
6864
createHPAStatusCondition(),
69-
createHPACreated(),
70-
createHPADeletionTimestamp(),
71-
}
65+
)
7266
}
7367

7468
func wrapHPAFunc(f func(*autoscaling.HorizontalPodAutoscaler) *metric.Family) func(interface{}) *metric.Family {
@@ -338,56 +332,6 @@ func createHPAStatusDesiredReplicas() generator.FamilyGenerator {
338332
)
339333
}
340334

341-
func createHPAAnnotations(allowAnnotationsList []string) generator.FamilyGenerator {
342-
return *generator.NewFamilyGeneratorWithStability(
343-
descHorizontalPodAutoscalerAnnotationsName,
344-
descHorizontalPodAutoscalerAnnotationsHelp,
345-
metric.Gauge,
346-
basemetrics.ALPHA,
347-
"",
348-
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
349-
if len(allowAnnotationsList) == 0 {
350-
return &metric.Family{}
351-
}
352-
annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", a.Annotations, allowAnnotationsList)
353-
return &metric.Family{
354-
Metrics: []*metric.Metric{
355-
{
356-
LabelKeys: annotationKeys,
357-
LabelValues: annotationValues,
358-
Value: 1,
359-
},
360-
},
361-
}
362-
}),
363-
)
364-
}
365-
366-
func createHPALabels(allowLabelsList []string) generator.FamilyGenerator {
367-
return *generator.NewFamilyGeneratorWithStability(
368-
descHorizontalPodAutoscalerLabelsName,
369-
descHorizontalPodAutoscalerLabelsHelp,
370-
metric.Gauge,
371-
basemetrics.STABLE,
372-
"",
373-
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
374-
if len(allowLabelsList) == 0 {
375-
return &metric.Family{}
376-
}
377-
labelKeys, labelValues := createPrometheusLabelKeysValues("label", a.Labels, allowLabelsList)
378-
return &metric.Family{
379-
Metrics: []*metric.Metric{
380-
{
381-
LabelKeys: labelKeys,
382-
LabelValues: labelValues,
383-
Value: 1,
384-
},
385-
},
386-
}
387-
}),
388-
)
389-
}
390-
391335
func createHPAStatusCondition() generator.FamilyGenerator {
392336
return *generator.NewFamilyGeneratorWithStability(
393337
"kube_horizontalpodautoscaler_status_condition",
@@ -415,49 +359,3 @@ func createHPAStatusCondition() generator.FamilyGenerator {
415359
}),
416360
)
417361
}
418-
419-
func createHPACreated() generator.FamilyGenerator {
420-
return *generator.NewFamilyGeneratorWithStability(
421-
"kube_horizontalpodautoscaler_created",
422-
"Unix creation timestamp",
423-
metric.Gauge,
424-
basemetrics.ALPHA,
425-
"",
426-
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
427-
ms := []*metric.Metric{}
428-
429-
if !a.CreationTimestamp.IsZero() {
430-
ms = append(ms, &metric.Metric{
431-
Value: float64(a.CreationTimestamp.Unix()),
432-
})
433-
}
434-
435-
return &metric.Family{
436-
Metrics: ms,
437-
}
438-
}),
439-
)
440-
}
441-
442-
func createHPADeletionTimestamp() generator.FamilyGenerator {
443-
return *generator.NewFamilyGeneratorWithStability(
444-
"kube_horizontalpodautoscaler_deletion_timestamp",
445-
"Unix deletion timestamp",
446-
metric.Gauge,
447-
basemetrics.ALPHA,
448-
"",
449-
wrapHPAFunc(func(a *autoscaling.HorizontalPodAutoscaler) *metric.Family {
450-
ms := []*metric.Metric{}
451-
452-
if !a.DeletionTimestamp.IsZero() {
453-
ms = append(ms, &metric.Metric{
454-
Value: float64(a.DeletionTimestamp.Unix()),
455-
})
456-
}
457-
458-
return &metric.Family{
459-
Metrics: ms,
460-
}
461-
}),
462-
)
463-
}

internal/store/metadata.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package store
18+
19+
import (
20+
basemetrics "k8s.io/component-base/metrics"
21+
22+
metaapi "k8s.io/apimachinery/pkg/api/meta"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
25+
"k8s.io/kube-state-metrics/v2/pkg/metric"
26+
27+
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
28+
)
29+
30+
var (
31+
descAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels."
32+
descLabelsHelp = "Kubernetes labels converted to Prometheus labels."
33+
descCreationHelp = "Creation timestamp converted to Unix timestamp."
34+
descDeletionHelp = "Deletion timestamp converted to Unix timestamp."
35+
)
36+
37+
// createMetadataMetricFamiliesGenerator provides metadata metrics for all resources
38+
func createMetadataMetricFamiliesGenerator(allowAnnotationsList, allowLabelsList, descLabelsDefaultLabels []string, descPrefixName string) []generator.FamilyGenerator {
39+
return []generator.FamilyGenerator{
40+
*generator.NewFamilyGeneratorWithStability(
41+
descPrefixName+"_created",
42+
descCreationHelp,
43+
metric.Gauge,
44+
basemetrics.STABLE,
45+
"",
46+
wrapMetadataFunc(func(t *metav1.ObjectMeta) *metric.Family {
47+
ms := []*metric.Metric{}
48+
49+
if !t.CreationTimestamp.IsZero() {
50+
ms = append(ms, &metric.Metric{
51+
Value: float64(t.CreationTimestamp.Unix()),
52+
})
53+
}
54+
55+
return &metric.Family{
56+
Metrics: ms,
57+
}
58+
}, descLabelsDefaultLabels),
59+
),
60+
*generator.NewFamilyGeneratorWithStability(
61+
descPrefixName+"_deletion_timestamp",
62+
descDeletionHelp,
63+
metric.Gauge,
64+
basemetrics.STABLE,
65+
"",
66+
wrapMetadataFunc(func(t *metav1.ObjectMeta) *metric.Family {
67+
ms := []*metric.Metric{}
68+
69+
if t.DeletionTimestamp != nil && !t.DeletionTimestamp.IsZero() {
70+
ms = append(ms, &metric.Metric{
71+
Value: float64(t.DeletionTimestamp.Unix()),
72+
})
73+
}
74+
75+
return &metric.Family{
76+
Metrics: ms,
77+
}
78+
}, descLabelsDefaultLabels),
79+
),
80+
81+
*generator.NewFamilyGeneratorWithStability(
82+
descPrefixName+"_annotations",
83+
descAnnotationsHelp,
84+
metric.Gauge,
85+
basemetrics.ALPHA,
86+
"",
87+
wrapMetadataFunc(func(t *metav1.ObjectMeta) *metric.Family {
88+
if len(allowAnnotationsList) == 0 {
89+
return &metric.Family{}
90+
}
91+
annotationKeys, annotationValues := createPrometheusLabelKeysValues("annotation", t.Annotations, allowAnnotationsList)
92+
return &metric.Family{
93+
Metrics: []*metric.Metric{
94+
{
95+
LabelKeys: annotationKeys,
96+
LabelValues: annotationValues,
97+
Value: 1,
98+
},
99+
}}
100+
}, descLabelsDefaultLabels),
101+
),
102+
*generator.NewFamilyGeneratorWithStability(
103+
descPrefixName+"_labels",
104+
descLabelsHelp,
105+
metric.Gauge,
106+
basemetrics.ALPHA,
107+
"",
108+
wrapMetadataFunc(func(t *metav1.ObjectMeta) *metric.Family {
109+
if len(allowLabelsList) == 0 {
110+
return &metric.Family{}
111+
}
112+
labelKeys, labelValues := createPrometheusLabelKeysValues("label", t.Labels, allowLabelsList)
113+
return &metric.Family{
114+
Metrics: []*metric.Metric{
115+
{
116+
LabelKeys: labelKeys,
117+
LabelValues: labelValues,
118+
Value: 1,
119+
},
120+
}}
121+
122+
}, descLabelsDefaultLabels),
123+
),
124+
}
125+
}
126+
127+
func wrapMetadataFunc(f func(*metav1.ObjectMeta) *metric.Family, defaultLabels []string) func(any) *metric.Family {
128+
return func(obj any) *metric.Family {
129+
o := obj.(metav1.Object)
130+
objectMeta := metaapi.AsPartialObjectMetadata(o).ObjectMeta
131+
metricFamily := f(&objectMeta)
132+
133+
for _, m := range metricFamily.Metrics {
134+
m.LabelKeys, m.LabelValues = mergeKeyValues(defaultLabels, []string{o.GetNamespace(), o.GetName()}, m.LabelKeys, m.LabelValues)
135+
}
136+
137+
return metricFamily
138+
}
139+
}

0 commit comments

Comments
 (0)