Skip to content

Commit

Permalink
[receiver/k8scluster] Fix empty k8s.namespace.name in k8s.namespace.p…
Browse files Browse the repository at this point in the history
…hase metrics (#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.
  • Loading branch information
eplightning committed Jun 19, 2023
1 parent 6ee548b commit d89a6e1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
20 changes: 20 additions & 0 deletions .chloggen/k8scluster-fix-empty-namespace-in-namespacephase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: k8sclusterreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix empty k8s.namespace.name attribute in k8s.namespace.phase metric

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [23452]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func GetMetrics(set receiver.CreateSettings, ns *corev1.Namespace) pmetric.Metri
mb := imetadata.NewMetricsBuilder(imetadata.DefaultMetricsBuilderConfig(), set)
ts := pcommon.NewTimestampFromTime(time.Now())
mb.RecordK8sNamespacePhaseDataPoint(ts, int64(namespacePhaseValues[ns.Status.Phase]))
return mb.Emit(imetadata.WithK8sNamespaceUID(string(ns.UID)), imetadata.WithK8sNamespaceName(ns.Namespace), imetadata.WithOpencensusResourcetype("k8s"))
return mb.Emit(imetadata.WithK8sNamespaceUID(string(ns.UID)), imetadata.WithK8sNamespaceName(ns.Name), imetadata.WithOpencensusResourcetype("k8s"))
}

var namespacePhaseValues = map[corev1.NamespacePhase]int32{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ func TestNamespaceMetrics(t *testing.T) {
func newNamespace(id string) *corev1.Namespace {
return &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: "test-namespace-" + id,
Namespace: "test-namespace",
UID: types.UID("test-namespace-" + id + "-uid"),
Name: "test-namespace-" + id,
UID: types.UID("test-namespace-" + id + "-uid"),
Labels: map[string]string{
"foo": "bar",
"foo1": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resourceMetrics:
attributes:
- key: k8s.namespace.name
value:
stringValue: test-namespace
stringValue: test-namespace-1
- key: k8s.namespace.uid
value:
stringValue: test-namespace-1-uid
Expand All @@ -21,4 +21,4 @@ resourceMetrics:
unit: "1"
scope:
name: otelcol/k8sclusterreceiver
version: latest
version: latest
10 changes: 5 additions & 5 deletions receiver/k8sclusterreceiver/testdata/e2e/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resourceMetrics:
attributes:
- key: k8s.namespace.name
value:
stringValue: ""
stringValue: kube-system
- key: k8s.namespace.uid
value:
stringValue: 3604b135-20f2-404b-9c1a-175ef649793e
Expand All @@ -27,7 +27,7 @@ resourceMetrics:
attributes:
- key: k8s.namespace.name
value:
stringValue: ""
stringValue: local-path-storage
- key: k8s.namespace.uid
value:
stringValue: 414da07d-33d0-4043-ae7c-d6b264d134e5
Expand All @@ -51,7 +51,7 @@ resourceMetrics:
attributes:
- key: k8s.namespace.name
value:
stringValue: ""
stringValue: kube-public
- key: k8s.namespace.uid
value:
stringValue: 7516afba-1597-49e3-8569-9732b7b94865
Expand All @@ -75,7 +75,7 @@ resourceMetrics:
attributes:
- key: k8s.namespace.name
value:
stringValue: ""
stringValue: kube-node-lease
- key: k8s.namespace.uid
value:
stringValue: 8dd32894-d0ff-4cff-bd75-b818c20fc72b
Expand All @@ -99,7 +99,7 @@ resourceMetrics:
attributes:
- key: k8s.namespace.name
value:
stringValue: ""
stringValue: default
- key: k8s.namespace.uid
value:
stringValue: caa467a2-d3e8-4e66-8b76-a155464bac79
Expand Down

0 comments on commit d89a6e1

Please sign in to comment.