From 76cd61edc02479e3912267f181d0ccea032bb610 Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Thu, 14 Nov 2024 10:32:12 +0100 Subject: [PATCH 1/4] [processor/k8sattributes]: add k8s resource attributes if original attribute value is empty Signed-off-by: Florian Bacher --- processor/k8sattributesprocessor/processor.go | 39 +++++------- .../k8sattributesprocessor/processor_test.go | 61 +++++++++++++++++++ 2 files changed, 76 insertions(+), 24 deletions(-) diff --git a/processor/k8sattributesprocessor/processor.go b/processor/k8sattributesprocessor/processor.go index 40347a55f0ef..61d5e635495e 100644 --- a/processor/k8sattributesprocessor/processor.go +++ b/processor/k8sattributesprocessor/processor.go @@ -124,9 +124,7 @@ func (kp *kubernetesprocessor) processResource(ctx context.Context, resource pco for i := range podIdentifierValue { if podIdentifierValue[i].Source.From == kube.ConnectionSource && podIdentifierValue[i].Value != "" { - if _, found := resource.Attributes().Get(kube.K8sIPLabelName); !found { - resource.Attributes().PutStr(kube.K8sIPLabelName, podIdentifierValue[i].Value) - } + setResourceAttribute(resource.Attributes(), kube.K8sIPLabelName, podIdentifierValue[i].Value) break } } @@ -141,9 +139,7 @@ func (kp *kubernetesprocessor) processResource(ctx context.Context, resource pco kp.logger.Debug("getting the pod", zap.Any("pod", pod)) for key, val := range pod.Attributes { - if _, found := resource.Attributes().Get(key); !found { - resource.Attributes().PutStr(key, val) - } + setResourceAttribute(resource.Attributes(), key, val) } kp.addContainerAttributes(resource.Attributes(), pod) } @@ -153,9 +149,7 @@ func (kp *kubernetesprocessor) processResource(ctx context.Context, resource pco if namespace != "" { attrsToAdd := kp.getAttributesForPodsNamespace(namespace) for key, val := range attrsToAdd { - if _, found := resource.Attributes().Get(key); !found { - resource.Attributes().PutStr(key, val) - } + setResourceAttribute(resource.Attributes(), key, val) } } @@ -163,19 +157,22 @@ func (kp *kubernetesprocessor) processResource(ctx context.Context, resource pco if nodeName != "" { attrsToAdd := kp.getAttributesForPodsNode(nodeName) for key, val := range attrsToAdd { - if _, found := resource.Attributes().Get(key); !found { - resource.Attributes().PutStr(key, val) - } + setResourceAttribute(resource.Attributes(), key, val) } nodeUID := kp.getUIDForPodsNode(nodeName) if nodeUID != "" { - if _, found := resource.Attributes().Get(conventions.AttributeK8SNodeUID); !found { - resource.Attributes().PutStr(conventions.AttributeK8SNodeUID, nodeUID) - } + setResourceAttribute(resource.Attributes(), conventions.AttributeK8SNodeUID, nodeUID) } } } +func setResourceAttribute(attributes pcommon.Map, key string, val string) { + attr, found := attributes.Get(key) + if !found || attr.AsString() == "" { + attributes.PutStr(key, val) + } +} + func getNamespace(pod *kube.Pod, resAttrs pcommon.Map) string { if pod != nil && pod.Namespace != "" { return pod.Namespace @@ -213,19 +210,13 @@ func (kp *kubernetesprocessor) addContainerAttributes(attrs pcommon.Map, pod *ku return } if containerSpec.Name != "" { - if _, found := attrs.Get(conventions.AttributeK8SContainerName); !found { - attrs.PutStr(conventions.AttributeK8SContainerName, containerSpec.Name) - } + setResourceAttribute(attrs, conventions.AttributeK8SContainerName, containerSpec.Name) } if containerSpec.ImageName != "" { - if _, found := attrs.Get(conventions.AttributeContainerImageName); !found { - attrs.PutStr(conventions.AttributeContainerImageName, containerSpec.ImageName) - } + setResourceAttribute(attrs, conventions.AttributeContainerImageName, containerSpec.ImageName) } if containerSpec.ImageTag != "" { - if _, found := attrs.Get(conventions.AttributeContainerImageTag); !found { - attrs.PutStr(conventions.AttributeContainerImageTag, containerSpec.ImageTag) - } + setResourceAttribute(attrs, conventions.AttributeContainerImageTag, containerSpec.ImageTag) } // attempt to get container ID from restart count runID := -1 diff --git a/processor/k8sattributesprocessor/processor_test.go b/processor/k8sattributesprocessor/processor_test.go index f7493cc38cdc..9676d4c0f02f 100644 --- a/processor/k8sattributesprocessor/processor_test.go +++ b/processor/k8sattributesprocessor/processor_test.go @@ -1572,3 +1572,64 @@ func (nh *nopHost) GetExtensions() map[component.ID]component.Component { func (nh *nopHost) Report(event *componentstatus.Event) { nh.reportFunc(event) } + +func Test_setResourceAttribute(t *testing.T) { + tests := []struct { + name string + attributes func() pcommon.Map + key string + val string + wantAttrs func() pcommon.Map + }{ + { + name: "attribute not present - add value", + attributes: func() pcommon.Map { + return pcommon.NewMap() + }, + key: "foo", + val: "bar", + wantAttrs: func() pcommon.Map { + m := pcommon.NewMap() + m.PutStr("foo", "bar") + return m + }, + }, + { + name: "attribute present - do not overwrite value", + attributes: func() pcommon.Map { + m := pcommon.NewMap() + m.PutStr("foo", "bar") + return m + }, + key: "foo", + val: "baz", + wantAttrs: func() pcommon.Map { + m := pcommon.NewMap() + m.PutStr("foo", "bar") + return m + }, + }, + { + name: "attribute present with empty value - set value", + attributes: func() pcommon.Map { + m := pcommon.NewMap() + m.PutStr("foo", "") + return m + }, + key: "foo", + val: "bar", + wantAttrs: func() pcommon.Map { + m := pcommon.NewMap() + m.PutStr("foo", "bar") + return m + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + attrs := tt.attributes() + setResourceAttribute(attrs, tt.key, tt.val) + require.Equal(t, tt.wantAttrs(), attrs) + }) + } +} From 0a657a79d2d25513944a2020b6634b5c5744988a Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Mon, 2 Dec 2024 08:13:36 +0100 Subject: [PATCH 2/4] add changelog entry Signed-off-by: Florian Bacher --- .chloggen/k8s-override-empty-attributes.yaml | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .chloggen/k8s-override-empty-attributes.yaml diff --git a/.chloggen/k8s-override-empty-attributes.yaml b/.chloggen/k8s-override-empty-attributes.yaml new file mode 100644 index 000000000000..2503ca4156dd --- /dev/null +++ b/.chloggen/k8s-override-empty-attributes.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# 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: k8sattributesprocessor + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Override extracted k8s attributes if original value has been empty + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [36373] + +# (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: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] From e14af847cf7667688152be3ed3165a23aa373c67 Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Mon, 2 Dec 2024 09:50:53 +0100 Subject: [PATCH 3/4] fix linting Signed-off-by: Florian Bacher --- processor/k8sattributesprocessor/processor_test.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/processor/k8sattributesprocessor/processor_test.go b/processor/k8sattributesprocessor/processor_test.go index d4e119766107..e6f8faf8ced3 100644 --- a/processor/k8sattributesprocessor/processor_test.go +++ b/processor/k8sattributesprocessor/processor_test.go @@ -1647,12 +1647,10 @@ func Test_setResourceAttribute(t *testing.T) { wantAttrs func() pcommon.Map }{ { - name: "attribute not present - add value", - attributes: func() pcommon.Map { - return pcommon.NewMap() - }, - key: "foo", - val: "bar", + name: "attribute not present - add value", + attributes: pcommon.NewMap, + key: "foo", + val: "bar", wantAttrs: func() pcommon.Map { m := pcommon.NewMap() m.PutStr("foo", "bar") From 3851eff00a27b857f118c14d30c8865dd12f88e9 Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Thu, 12 Dec 2024 09:08:07 +0100 Subject: [PATCH 4/4] specific test name Signed-off-by: Florian Bacher --- processor/k8sattributesprocessor/processor_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/processor/k8sattributesprocessor/processor_test.go b/processor/k8sattributesprocessor/processor_test.go index bc26ac8bca93..4cd56e2e3362 100644 --- a/processor/k8sattributesprocessor/processor_test.go +++ b/processor/k8sattributesprocessor/processor_test.go @@ -1656,7 +1656,7 @@ func Test_setResourceAttribute(t *testing.T) { }, }, { - name: "attribute present - do not overwrite value", + name: "attribute present with non-empty value - do not overwrite value", attributes: func() pcommon.Map { m := pcommon.NewMap() m.PutStr("foo", "bar")