Skip to content

Commit

Permalink
[processor/k8sattributes]: add k8s resource attributes if original at…
Browse files Browse the repository at this point in the history
…tribute value is empty

Signed-off-by: Florian Bacher <[email protected]>
  • Loading branch information
bacherfl committed Nov 14, 2024
1 parent 4e7f7d2 commit 76cd61e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 24 deletions.
39 changes: 15 additions & 24 deletions processor/k8sattributesprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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)
}
Expand All @@ -153,29 +149,30 @@ 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)
}
}

nodeName := getNodeName(pod, resource.Attributes())
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
Expand Down Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions processor/k8sattributesprocessor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 76cd61e

Please sign in to comment.