Skip to content

Commit 2a9ab3a

Browse files
committed
Remove kube_namespace_annotations metric
Because we do not know what could be in the annotations that are created by kubernetes, we want to remove all annotation metrics.
1 parent 03122fe commit 2a9ab3a

File tree

5 files changed

+0
-111
lines changed

5 files changed

+0
-111
lines changed

docs/namespace-metrics.md

-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44
| ---------- | ----------- | ----------- | ----------- |
55
| kube_namespace_status_phase| Gauge | `namespace`=&lt;namespace-name&gt; <br> `status`=&lt;Active\|Terminating&gt; | STABLE |
66
| kube_namespace_labels | Gauge | `namespace`=&lt;namespace-name&gt; <br> `label_NS_LABEL`=&lt;NS_LABEL&gt; | STABLE |
7-
| kube_namespace_annotations | Gauge | `namespace`=&lt;namespace-name&gt; <br> `annotation_NS_ANNOTATION`=&lt;NS_ANNOTATION&gt; | STABLE |
87
| kube_namespace_created | Gauge | `namespace`=&lt;namespace-name&gt; | STABLE |

internal/store/namespace.go

-21
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ var (
3232
descNamespaceLabelsHelp = "Kubernetes labels converted to Prometheus labels."
3333
descNamespaceLabelsDefaultLabels = []string{"namespace"}
3434

35-
descNamespaceAnnotationsName = "kube_namespace_annotations"
36-
descNamespaceAnnotationsHelp = "Kubernetes annotations converted to Prometheus labels."
37-
descNamespaceAnnotationsDefaultLabels = []string{"namespace"}
38-
3935
namespaceMetricFamilies = []metric.FamilyGenerator{
4036
{
4137
Name: "kube_namespace_created",
@@ -71,23 +67,6 @@ var (
7167
}
7268
}),
7369
},
74-
{
75-
Name: descNamespaceAnnotationsName,
76-
Type: metric.Gauge,
77-
Help: descNamespaceAnnotationsHelp,
78-
GenerateFunc: wrapNamespaceFunc(func(n *v1.Namespace) *metric.Family {
79-
annotationKeys, annotationValues := kubeAnnotationsToPrometheusLabels(n.Annotations)
80-
return &metric.Family{
81-
Metrics: []*metric.Metric{
82-
{
83-
LabelKeys: annotationKeys,
84-
LabelValues: annotationValues,
85-
Value: 1,
86-
},
87-
},
88-
}
89-
}),
90-
},
9170
{
9271
Name: "kube_namespace_status_phase",
9372
Type: metric.Gauge,

internal/store/namespace_test.go

-13
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ func TestNamespaceStore(t *testing.T) {
3333
# TYPE kube_namespace_created gauge
3434
# HELP kube_namespace_labels Kubernetes labels converted to Prometheus labels.
3535
# TYPE kube_namespace_labels gauge
36-
# HELP kube_namespace_annotations Kubernetes annotations converted to Prometheus labels.
37-
# TYPE kube_namespace_annotations gauge
3836
# HELP kube_namespace_status_phase kubernetes namespace status phase.
3937
# TYPE kube_namespace_status_phase gauge
4038
`
@@ -54,7 +52,6 @@ func TestNamespaceStore(t *testing.T) {
5452
},
5553
Want: `
5654
kube_namespace_labels{namespace="nsActiveTest"} 1
57-
kube_namespace_annotations{namespace="nsActiveTest"} 1
5855
kube_namespace_status_phase{namespace="nsActiveTest",phase="Active"} 1
5956
kube_namespace_status_phase{namespace="nsActiveTest",phase="Terminating"} 0
6057
`,
@@ -73,7 +70,6 @@ func TestNamespaceStore(t *testing.T) {
7370
},
7471
Want: `
7572
kube_namespace_labels{namespace="nsTerminateTest"} 1
76-
kube_namespace_annotations{namespace="nsTerminateTest"} 1
7773
kube_namespace_status_phase{namespace="nsTerminateTest",phase="Active"} 0
7874
kube_namespace_status_phase{namespace="nsTerminateTest",phase="Terminating"} 1
7975
`,
@@ -87,9 +83,6 @@ func TestNamespaceStore(t *testing.T) {
8783
Labels: map[string]string{
8884
"app": "example1",
8985
},
90-
Annotations: map[string]string{
91-
"app": "example1",
92-
},
9386
},
9487
Spec: v1.NamespaceSpec{
9588
Finalizers: []v1.FinalizerName{v1.FinalizerKubernetes},
@@ -101,7 +94,6 @@ func TestNamespaceStore(t *testing.T) {
10194
Want: `
10295
kube_namespace_created{namespace="ns1"} 1.5e+09
10396
kube_namespace_labels{label_app="example1",namespace="ns1"} 1
104-
kube_namespace_annotations{annotation_app="example1",namespace="ns1"} 1
10597
kube_namespace_status_phase{namespace="ns1",phase="Active"} 1
10698
kube_namespace_status_phase{namespace="ns1",phase="Terminating"} 0
10799
`,
@@ -114,10 +106,6 @@ func TestNamespaceStore(t *testing.T) {
114106
"app": "example2",
115107
"l2": "label2",
116108
},
117-
Annotations: map[string]string{
118-
"app": "example2",
119-
"l2": "label2",
120-
},
121109
},
122110
Spec: v1.NamespaceSpec{
123111
Finalizers: []v1.FinalizerName{v1.FinalizerKubernetes},
@@ -128,7 +116,6 @@ func TestNamespaceStore(t *testing.T) {
128116
},
129117
Want: `
130118
kube_namespace_labels{label_app="example2",label_l2="label2",namespace="ns2"} 1
131-
kube_namespace_annotations{annotation_app="example2",annotation_l2="label2",namespace="ns2"} 1
132119
kube_namespace_status_phase{namespace="ns2",phase="Active"} 1
133120
kube_namespace_status_phase{namespace="ns2",phase="Terminating"} 0
134121
`,

internal/store/utils.go

-12
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,6 @@ func kubeLabelsToPrometheusLabels(labels map[string]string) ([]string, []string)
7070
return labelKeys, labelValues
7171
}
7272

73-
func kubeAnnotationsToPrometheusLabels(annotations map[string]string) ([]string, []string) {
74-
annotationKeys := make([]string, len(annotations))
75-
annotationValues := make([]string, len(annotations))
76-
i := 0
77-
for k, v := range annotations {
78-
annotationKeys[i] = "annotation_" + sanitizeLabelName(k)
79-
annotationValues[i] = v
80-
i++
81-
}
82-
return annotationKeys, annotationValues
83-
}
84-
8573
func sanitizeLabelName(s string) string {
8674
return invalidLabelCharRE.ReplaceAllString(s, "_")
8775
}

internal/store/utils_test.go

-64
Original file line numberDiff line numberDiff line change
@@ -189,67 +189,3 @@ func TestKubeLabelsToPrometheusLabels(t *testing.T) {
189189
}
190190

191191
}
192-
193-
func TestKubeAnnotationsToPrometheusLabels(t *testing.T) {
194-
testCases := []struct {
195-
kubeAnnotations map[string]string
196-
expectKeys []string
197-
expectValues []string
198-
}{
199-
{
200-
kubeAnnotations: map[string]string{
201-
"app1": "normal",
202-
},
203-
expectKeys: []string{"annotation_app1"},
204-
expectValues: []string{"normal"},
205-
},
206-
{
207-
kubeAnnotations: map[string]string{
208-
"0_app3": "starts_with_digit",
209-
},
210-
expectKeys: []string{"annotation_0_app3"},
211-
expectValues: []string{"starts_with_digit"},
212-
},
213-
{
214-
kubeAnnotations: map[string]string{
215-
"": "empty",
216-
},
217-
expectKeys: []string{"annotation_"},
218-
expectValues: []string{"empty"},
219-
},
220-
{
221-
kubeAnnotations: map[string]string{
222-
"$app4": "special_char",
223-
},
224-
expectKeys: []string{"annotation__app4"},
225-
expectValues: []string{"special_char"},
226-
},
227-
{
228-
kubeAnnotations: map[string]string{
229-
"_app5": "starts_with_underscore",
230-
},
231-
expectKeys: []string{"annotation__app5"},
232-
expectValues: []string{"starts_with_underscore"},
233-
},
234-
}
235-
236-
for _, tc := range testCases {
237-
t.Run(fmt.Sprintf("kubeannotations input=%v , expected prometheus keys=%v, expected prometheus values=%v", tc.kubeAnnotations, tc.expectKeys, tc.expectValues), func(t *testing.T) {
238-
annotationKeys, annotationValues := kubeAnnotationsToPrometheusLabels(tc.kubeAnnotations)
239-
if len(annotationKeys) != len(tc.expectKeys) {
240-
t.Errorf("Got Prometheus label keys with len %d but expected %d", len(annotationKeys), len(tc.expectKeys))
241-
}
242-
243-
if len(annotationValues) != len(tc.expectValues) {
244-
t.Errorf("Got Prometheus label values with len %d but expected %d", len(annotationValues), len(tc.expectValues))
245-
}
246-
247-
for i := range tc.expectKeys {
248-
if !(tc.expectKeys[i] == annotationKeys[i] && tc.expectValues[i] == annotationValues[i]) {
249-
t.Errorf("Got Prometheus label %q: %q but expected %q: %q", annotationKeys[i], annotationValues[i], tc.expectKeys[i], tc.expectValues[i])
250-
}
251-
}
252-
})
253-
}
254-
255-
}

0 commit comments

Comments
 (0)