Skip to content

Commit d89a6e1

Browse files
authored
[receiver/k8scluster] Fix empty k8s.namespace.name in k8s.namespace.phase metrics (open-telemetry#23453)
Kubernetes namespace is a cluster-scoped resource and doesn't seem to have populated `namespace` field in Kubernetes (at least on 1.25.x and 1.26.x). This PR changes it so the name of the namespace is simply taken from `name` field instead.
1 parent 6ee548b commit d89a6e1

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use this changelog template to create an entry for release notes.
2+
# If your change doesn't affect end users, such as a test fix or a tooling change,
3+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
4+
5+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
6+
change_type: bug_fix
7+
8+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
9+
component: k8sclusterreceiver
10+
11+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
12+
note: Fix empty k8s.namespace.name attribute in k8s.namespace.phase metric
13+
14+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
15+
issues: [23452]
16+
17+
# (Optional) One or more lines of additional information to render under the primary note.
18+
# These lines will be padded with 2 spaces and then inserted directly into the document.
19+
# Use pipe (|) for multiline entries.
20+
subtext:

receiver/k8sclusterreceiver/internal/namespace/namespaces.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func GetMetrics(set receiver.CreateSettings, ns *corev1.Namespace) pmetric.Metri
1818
mb := imetadata.NewMetricsBuilder(imetadata.DefaultMetricsBuilderConfig(), set)
1919
ts := pcommon.NewTimestampFromTime(time.Now())
2020
mb.RecordK8sNamespacePhaseDataPoint(ts, int64(namespacePhaseValues[ns.Status.Phase]))
21-
return mb.Emit(imetadata.WithK8sNamespaceUID(string(ns.UID)), imetadata.WithK8sNamespaceName(ns.Namespace), imetadata.WithOpencensusResourcetype("k8s"))
21+
return mb.Emit(imetadata.WithK8sNamespaceUID(string(ns.UID)), imetadata.WithK8sNamespaceName(ns.Name), imetadata.WithOpencensusResourcetype("k8s"))
2222
}
2323

2424
var namespacePhaseValues = map[corev1.NamespacePhase]int32{

receiver/k8sclusterreceiver/internal/namespace/namespaces_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ func TestNamespaceMetrics(t *testing.T) {
3636
func newNamespace(id string) *corev1.Namespace {
3737
return &corev1.Namespace{
3838
ObjectMeta: v1.ObjectMeta{
39-
Name: "test-namespace-" + id,
40-
Namespace: "test-namespace",
41-
UID: types.UID("test-namespace-" + id + "-uid"),
39+
Name: "test-namespace-" + id,
40+
UID: types.UID("test-namespace-" + id + "-uid"),
4241
Labels: map[string]string{
4342
"foo": "bar",
4443
"foo1": "",

receiver/k8sclusterreceiver/internal/namespace/testdata/expected.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resourceMetrics:
33
attributes:
44
- key: k8s.namespace.name
55
value:
6-
stringValue: test-namespace
6+
stringValue: test-namespace-1
77
- key: k8s.namespace.uid
88
value:
99
stringValue: test-namespace-1-uid
@@ -21,4 +21,4 @@ resourceMetrics:
2121
unit: "1"
2222
scope:
2323
name: otelcol/k8sclusterreceiver
24-
version: latest
24+
version: latest

receiver/k8sclusterreceiver/testdata/e2e/expected.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resourceMetrics:
33
attributes:
44
- key: k8s.namespace.name
55
value:
6-
stringValue: ""
6+
stringValue: kube-system
77
- key: k8s.namespace.uid
88
value:
99
stringValue: 3604b135-20f2-404b-9c1a-175ef649793e
@@ -27,7 +27,7 @@ resourceMetrics:
2727
attributes:
2828
- key: k8s.namespace.name
2929
value:
30-
stringValue: ""
30+
stringValue: local-path-storage
3131
- key: k8s.namespace.uid
3232
value:
3333
stringValue: 414da07d-33d0-4043-ae7c-d6b264d134e5
@@ -51,7 +51,7 @@ resourceMetrics:
5151
attributes:
5252
- key: k8s.namespace.name
5353
value:
54-
stringValue: ""
54+
stringValue: kube-public
5555
- key: k8s.namespace.uid
5656
value:
5757
stringValue: 7516afba-1597-49e3-8569-9732b7b94865
@@ -75,7 +75,7 @@ resourceMetrics:
7575
attributes:
7676
- key: k8s.namespace.name
7777
value:
78-
stringValue: ""
78+
stringValue: kube-node-lease
7979
- key: k8s.namespace.uid
8080
value:
8181
stringValue: 8dd32894-d0ff-4cff-bd75-b818c20fc72b
@@ -99,7 +99,7 @@ resourceMetrics:
9999
attributes:
100100
- key: k8s.namespace.name
101101
value:
102-
stringValue: ""
102+
stringValue: default
103103
- key: k8s.namespace.uid
104104
value:
105105
stringValue: caa467a2-d3e8-4e66-8b76-a155464bac79

0 commit comments

Comments
 (0)