Skip to content

Commit 0738de0

Browse files
authored
Merge pull request #2539 from mrueg/expose-empty-labels
fix: expose empty labels
2 parents dfb688c + 2aa766e commit 0738de0

File tree

7 files changed

+50
-89
lines changed

7 files changed

+50
-89
lines changed

internal/store/endpoint.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,35 +131,25 @@ func endpointMetricFamilies(allowAnnotationsList, allowLabelsList []string) []ge
131131
"",
132132
wrapEndpointFunc(func(e *v1.Endpoints) *metric.Family {
133133
ms := []*metric.Metric{}
134+
labelKeys := []string{"port_protocol", "port_number", "port_name", "ip", "ready"}
135+
134136
for _, s := range e.Subsets {
135137
for _, port := range s.Ports {
136138
for _, available := range s.Addresses {
137-
labelValues := []string{string(port.Protocol), strconv.FormatInt(int64(port.Port), 10)}
138-
labelKeys := []string{"port_protocol", "port_number"}
139-
140-
if port.Name != "" {
141-
labelKeys = append(labelKeys, "port_name")
142-
labelValues = append(labelValues, port.Name)
143-
}
139+
labelValues := []string{string(port.Protocol), strconv.FormatInt(int64(port.Port), 10), port.Name}
144140

145141
ms = append(ms, &metric.Metric{
146142
LabelValues: append(labelValues, available.IP, "true"),
147-
LabelKeys: append(labelKeys, "ip", "ready"),
143+
LabelKeys: labelKeys,
148144
Value: 1,
149145
})
150146
}
151147
for _, notReadyAddresses := range s.NotReadyAddresses {
152-
labelValues := []string{string(port.Protocol), strconv.FormatInt(int64(port.Port), 10)}
153-
labelKeys := []string{"port_protocol", "port_number"}
154-
155-
if port.Name != "" {
156-
labelKeys = append(labelKeys, "port_name")
157-
labelValues = append(labelValues, port.Name)
158-
}
148+
labelValues := []string{string(port.Protocol), strconv.FormatInt(int64(port.Port), 10), port.Name}
159149

160150
ms = append(ms, &metric.Metric{
161151
LabelValues: append(labelValues, notReadyAddresses.IP, "false"),
162-
LabelKeys: append(labelKeys, "ip", "ready"),
152+
LabelKeys: labelKeys,
163153
Value: 1,
164154
})
165155
}

internal/store/endpoint_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ func TestEndpointStore(t *testing.T) {
135135
kube_endpoint_created{endpoint="single-port-endpoint",namespace="default"} 1.5e+09
136136
kube_endpoint_info{endpoint="single-port-endpoint",namespace="default"} 1
137137
kube_endpoint_ports{endpoint="single-port-endpoint",namespace="default",port_name="",port_number="8080",port_protocol="TCP"} 1
138-
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.1",namespace="default",port_number="8080",port_protocol="TCP",ready="true"} 1
139-
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.10",namespace="default",port_number="8080",port_protocol="TCP",ready="false"} 1
140-
kube_endpoint_address{endpoint="single-port-endpoint",ip="127.0.0.1",namespace="default",port_number="8080",port_protocol="TCP",ready="true"} 1
138+
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.1",namespace="default",port_name="",port_number="8080",port_protocol="TCP",ready="true"} 1
139+
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.10",namespace="default",port_name="",port_number="8080",port_protocol="TCP",ready="false"} 1
140+
kube_endpoint_address{endpoint="single-port-endpoint",ip="127.0.0.1",namespace="default",port_name="",port_number="8080",port_protocol="TCP",ready="true"} 1
141141
`,
142142
},
143143
}
@@ -269,9 +269,9 @@ func TestEndpointStoreWithLabels(t *testing.T) {
269269
kube_endpoint_info{endpoint="single-port-endpoint",namespace="default"} 1
270270
kube_endpoint_labels{endpoint="single-port-endpoint",label_app="single-foobar",namespace="default"} 1
271271
kube_endpoint_ports{endpoint="single-port-endpoint",namespace="default",port_name="",port_number="8080",port_protocol="TCP"} 1
272-
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.1",namespace="default",port_number="8080",port_protocol="TCP",ready="true"} 1
273-
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.10",namespace="default",port_number="8080",port_protocol="TCP",ready="false"} 1
274-
kube_endpoint_address{endpoint="single-port-endpoint",ip="127.0.0.1",namespace="default",port_number="8080",port_protocol="TCP",ready="true"} 1
272+
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.1",namespace="default",port_name="",port_number="8080",port_protocol="TCP",ready="true"} 1
273+
kube_endpoint_address{endpoint="single-port-endpoint",ip="10.0.0.10",namespace="default",port_name="",port_number="8080",port_protocol="TCP",ready="false"} 1
274+
kube_endpoint_address{endpoint="single-port-endpoint",ip="127.0.0.1",namespace="default",port_name="",port_number="8080",port_protocol="TCP",ready="true"} 1
275275
`,
276276
},
277277
}

internal/store/endpointslice.go

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -121,54 +121,41 @@ func endpointSliceMetricFamilies(allowAnnotationsList, allowLabelsList []string)
121121
wrapEndpointSliceFunc(func(e *discoveryv1.EndpointSlice) *metric.Family {
122122
m := []*metric.Metric{}
123123
for _, ep := range e.Endpoints {
124-
var (
125-
labelKeys,
126-
labelValues []string
127-
)
124+
125+
var ready, serving, terminating, hostname, targetrefKind, targetrefName, targetrefNamespace, endpointNodename, endpointZone string
128126

129127
if ep.Conditions.Ready != nil {
130-
labelKeys = append(labelKeys, "ready")
131-
labelValues = append(labelValues, strconv.FormatBool(*ep.Conditions.Ready))
128+
ready = strconv.FormatBool(*ep.Conditions.Ready)
132129
}
130+
133131
if ep.Conditions.Serving != nil {
134-
labelKeys = append(labelKeys, "serving")
135-
labelValues = append(labelValues, strconv.FormatBool(*ep.Conditions.Serving))
132+
serving = strconv.FormatBool(*ep.Conditions.Serving)
136133
}
134+
137135
if ep.Conditions.Terminating != nil {
138-
labelKeys = append(labelKeys, "terminating")
139-
labelValues = append(labelValues, strconv.FormatBool(*ep.Conditions.Terminating))
136+
serving = strconv.FormatBool(*ep.Conditions.Terminating)
140137
}
141-
142138
if ep.Hostname != nil {
143-
labelKeys = append(labelKeys, "hostname")
144-
labelValues = append(labelValues, *ep.Hostname)
139+
hostname = *ep.Hostname
145140
}
146141

147142
if ep.TargetRef != nil {
148-
if ep.TargetRef.Kind != "" {
149-
labelKeys = append(labelKeys, "targetref_kind")
150-
labelValues = append(labelValues, ep.TargetRef.Kind)
151-
}
152-
if ep.TargetRef.Name != "" {
153-
labelKeys = append(labelKeys, "targetref_name")
154-
labelValues = append(labelValues, ep.TargetRef.Name)
155-
}
156-
if ep.TargetRef.Namespace != "" {
157-
labelKeys = append(labelKeys, "targetref_namespace")
158-
labelValues = append(labelValues, ep.TargetRef.Namespace)
159-
}
143+
targetrefKind = ep.TargetRef.Kind
144+
targetrefName = ep.TargetRef.Name
145+
targetrefNamespace = ep.TargetRef.Namespace
160146
}
161147

162148
if ep.NodeName != nil {
163-
labelKeys = append(labelKeys, "endpoint_nodename")
164-
labelValues = append(labelValues, *ep.NodeName)
149+
endpointNodename = *ep.NodeName
165150
}
166151

167152
if ep.Zone != nil {
168-
labelKeys = append(labelKeys, "endpoint_zone")
169-
labelValues = append(labelValues, *ep.Zone)
153+
endpointZone = *ep.Zone
170154
}
171-
labelKeys = append(labelKeys, "address")
155+
156+
labelKeys := []string{"ready", "serving", "hostname", "terminating", "targetref_kind", "targetref_name", "targetref_namespace", "endpoint_nodename", "endpoint_zone", "address"}
157+
labelValues := []string{ready, serving, terminating, hostname, targetrefKind, targetrefName, targetrefNamespace, endpointNodename, endpointZone}
158+
172159
for _, address := range ep.Addresses {
173160
newlabelValues := make([]string, len(labelValues))
174161
copy(newlabelValues, labelValues)

internal/store/endpointslice_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ func TestEndpointSliceStore(t *testing.T) {
120120
# HELP kube_endpointslice_endpoints_hints Topology routing hints attached to endpoints
121121
# TYPE kube_endpointslice_endpoints gauge
122122
# TYPE kube_endpointslice_endpoints_hints gauge
123-
kube_endpointslice_endpoints{address="10.0.0.1",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="host",ready="true",terminating="false",namespace="test"} 1
124-
kube_endpointslice_endpoints{address="192.168.1.10",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="host",ready="true",terminating="false",namespace="test"} 1
123+
kube_endpointslice_endpoints{address="10.0.0.1",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="",namespace="test",ready="true",serving="false",targetref_kind="",targetref_name="",targetref_namespace="",terminating="host"} 1
124+
kube_endpointslice_endpoints{address="192.168.1.10",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="",namespace="test",ready="true",serving="false",targetref_kind="",targetref_name="",targetref_namespace="",terminating="host"} 1
125125
`,
126126

127127
MetricNames: []string{
@@ -159,9 +159,9 @@ func TestEndpointSliceStore(t *testing.T) {
159159
# TYPE kube_endpointslice_endpoints gauge
160160
# TYPE kube_endpointslice_endpoints_hints gauge
161161
kube_endpointslice_endpoints_hints{address="10.0.0.1",endpointslice="test_endpointslice-endpoints",for_zone="zone1",namespace="test"} 1
162-
kube_endpointslice_endpoints{address="10.0.0.1",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="host",ready="true",terminating="false",namespace="test"} 1
163-
kube_endpointslice_endpoints{address="192.168.1.10",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="host",ready="true",terminating="false",namespace="test"} 1
164-
`,
162+
kube_endpointslice_endpoints{address="10.0.0.1",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="",namespace="test",ready="true",serving="false",targetref_kind="",targetref_name="",targetref_namespace="",terminating="host"} 1
163+
kube_endpointslice_endpoints{address="192.168.1.10",endpoint_nodename="node",endpoint_zone="west",endpointslice="test_endpointslice-endpoints",hostname="",namespace="test",ready="true",serving="false",targetref_kind="",targetref_name="",targetref_namespace="",terminating="host"} 1
164+
`,
165165

166166
MetricNames: []string{
167167
"kube_endpointslice_endpoints",

internal/store/pod.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,41 +1653,26 @@ func createPodTolerationsFamilyGenerator() generator.FamilyGenerator {
16531653
var ms []*metric.Metric
16541654

16551655
for _, t := range p.Spec.Tolerations {
1656-
var labelKeys []string
1657-
var labelValues []string
1658-
1659-
if t.Key != "" {
1660-
labelKeys = append(labelKeys, "key")
1661-
labelValues = append(labelValues, t.Key)
1662-
}
1656+
var key, operator, value, effect, tolerationSeconds string
16631657

1658+
key = t.Key
16641659
if t.Operator != "" {
1665-
labelKeys = append(labelKeys, "operator")
1666-
labelValues = append(labelValues, string(t.Operator))
1660+
operator = string(t.Operator)
16671661
}
16681662

1669-
if t.Value != "" {
1670-
labelKeys = append(labelKeys, "value")
1671-
labelValues = append(labelValues, t.Value)
1672-
}
1663+
value = t.Value
16731664

16741665
if t.Effect != "" {
1675-
labelKeys = append(labelKeys, "effect")
1676-
labelValues = append(labelValues, string(t.Effect))
1666+
effect = string(t.Effect)
16771667
}
16781668

16791669
if t.TolerationSeconds != nil {
1680-
labelKeys = append(labelKeys, "toleration_seconds")
1681-
labelValues = append(labelValues, strconv.FormatInt(*t.TolerationSeconds, 10))
1682-
}
1683-
1684-
if len(labelKeys) == 0 {
1685-
continue
1670+
tolerationSeconds = strconv.FormatInt(*t.TolerationSeconds, 10)
16861671
}
16871672

16881673
ms = append(ms, &metric.Metric{
1689-
LabelKeys: labelKeys,
1690-
LabelValues: labelValues,
1674+
LabelKeys: []string{"key", "operator", "value", "effect", "toleration_seconds"},
1675+
LabelValues: []string{key, operator, value, effect, tolerationSeconds},
16911676
Value: 1,
16921677
})
16931678
}

internal/store/pod_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,9 +2123,10 @@ func TestPodStore(t *testing.T) {
21232123
Want: `
21242124
# HELP kube_pod_tolerations Information about the pod tolerations
21252125
# TYPE kube_pod_tolerations gauge
2126-
kube_pod_tolerations{namespace="ns1",pod="pod1",uid="uid1",key="key1",operator="Equal",value="value1",effect="NoSchedule"} 1
2127-
kube_pod_tolerations{namespace="ns1",pod="pod1",uid="uid1",key="key2",operator="Exists"} 1
2128-
kube_pod_tolerations{namespace="ns1",pod="pod1",uid="uid1",key="key3",operator="Equal",value="value3"} 1
2126+
kube_pod_tolerations{effect="",key="",namespace="ns1",operator="",pod="pod1",toleration_seconds="",uid="uid1",value=""} 1
2127+
kube_pod_tolerations{effect="",key="key2",namespace="ns1",operator="Exists",pod="pod1",toleration_seconds="",uid="uid1",value=""} 1
2128+
kube_pod_tolerations{effect="",key="key3",namespace="ns1",operator="Equal",pod="pod1",toleration_seconds="",uid="uid1",value="value3"} 1
2129+
kube_pod_tolerations{effect="NoSchedule",key="key1",namespace="ns1",operator="Equal",pod="pod1",toleration_seconds="",uid="uid1",value="value1"} 1
21292130
`,
21302131
MetricNames: []string{
21312132
"kube_pod_tolerations",

internal/store/serviceaccount.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,16 @@ func createServiceAccountInfoFamilyGenerator() generator.FamilyGenerator {
5656
basemetrics.ALPHA,
5757
"",
5858
wrapServiceAccountFunc(func(sa *v1.ServiceAccount) *metric.Family {
59-
var labelKeys []string
60-
var labelValues []string
59+
var automountToken string
6160

6261
if sa.AutomountServiceAccountToken != nil {
63-
labelKeys = append(labelKeys, "automount_token")
64-
labelValues = append(labelValues, strconv.FormatBool(*sa.AutomountServiceAccountToken))
62+
automountToken = strconv.FormatBool(*sa.AutomountServiceAccountToken)
6563
}
6664

6765
return &metric.Family{
6866
Metrics: []*metric.Metric{{
69-
LabelKeys: labelKeys,
70-
LabelValues: labelValues,
67+
LabelKeys: []string{"automount_token"},
68+
LabelValues: []string{automountToken},
7169
Value: 1,
7270
}},
7371
}

0 commit comments

Comments
 (0)