Skip to content

Commit 7e0c50a

Browse files
bacherflChrsMark
authored andcommitted
[processor/k8sattributes]: apply attribute value from retrieved k8s object if original value is empty (open-telemetry#36466)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description This PR extends the previous check whether an attribute value has not been present with a check for an empty value in the original resource attributes. This way, attributes, such as `k8s.namespace.name` will be set based on the retrieved kubernetes resource also if the original value has been set to e.g. an empty string <!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes open-telemetry#36373 <!--Describe what testing was performed and which tests were added.--> #### Testing Added unit tests --------- Signed-off-by: Florian Bacher <[email protected]> Co-authored-by: Christos Markou <[email protected]>
1 parent 9d50458 commit 7e0c50a

File tree

3 files changed

+101
-24
lines changed

3 files changed

+101
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: k8sattributesprocessor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Override extracted k8s attributes if original value has been empty
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [36373]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

processor/k8sattributesprocessor/processor.go

+15-24
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,7 @@ func (kp *kubernetesprocessor) processResource(ctx context.Context, resource pco
144144

145145
for i := range podIdentifierValue {
146146
if podIdentifierValue[i].Source.From == kube.ConnectionSource && podIdentifierValue[i].Value != "" {
147-
if _, found := resource.Attributes().Get(kube.K8sIPLabelName); !found {
148-
resource.Attributes().PutStr(kube.K8sIPLabelName, podIdentifierValue[i].Value)
149-
}
147+
setResourceAttribute(resource.Attributes(), kube.K8sIPLabelName, podIdentifierValue[i].Value)
150148
break
151149
}
152150
}
@@ -161,9 +159,7 @@ func (kp *kubernetesprocessor) processResource(ctx context.Context, resource pco
161159
kp.logger.Debug("getting the pod", zap.Any("pod", pod))
162160

163161
for key, val := range pod.Attributes {
164-
if _, found := resource.Attributes().Get(key); !found {
165-
resource.Attributes().PutStr(key, val)
166-
}
162+
setResourceAttribute(resource.Attributes(), key, val)
167163
}
168164
kp.addContainerAttributes(resource.Attributes(), pod)
169165
}
@@ -173,29 +169,30 @@ func (kp *kubernetesprocessor) processResource(ctx context.Context, resource pco
173169
if namespace != "" {
174170
attrsToAdd := kp.getAttributesForPodsNamespace(namespace)
175171
for key, val := range attrsToAdd {
176-
if _, found := resource.Attributes().Get(key); !found {
177-
resource.Attributes().PutStr(key, val)
178-
}
172+
setResourceAttribute(resource.Attributes(), key, val)
179173
}
180174
}
181175

182176
nodeName := getNodeName(pod, resource.Attributes())
183177
if nodeName != "" {
184178
attrsToAdd := kp.getAttributesForPodsNode(nodeName)
185179
for key, val := range attrsToAdd {
186-
if _, found := resource.Attributes().Get(key); !found {
187-
resource.Attributes().PutStr(key, val)
188-
}
180+
setResourceAttribute(resource.Attributes(), key, val)
189181
}
190182
nodeUID := kp.getUIDForPodsNode(nodeName)
191183
if nodeUID != "" {
192-
if _, found := resource.Attributes().Get(conventions.AttributeK8SNodeUID); !found {
193-
resource.Attributes().PutStr(conventions.AttributeK8SNodeUID, nodeUID)
194-
}
184+
setResourceAttribute(resource.Attributes(), conventions.AttributeK8SNodeUID, nodeUID)
195185
}
196186
}
197187
}
198188

189+
func setResourceAttribute(attributes pcommon.Map, key string, val string) {
190+
attr, found := attributes.Get(key)
191+
if !found || attr.AsString() == "" {
192+
attributes.PutStr(key, val)
193+
}
194+
}
195+
199196
func getNamespace(pod *kube.Pod, resAttrs pcommon.Map) string {
200197
if pod != nil && pod.Namespace != "" {
201198
return pod.Namespace
@@ -233,19 +230,13 @@ func (kp *kubernetesprocessor) addContainerAttributes(attrs pcommon.Map, pod *ku
233230
return
234231
}
235232
if containerSpec.Name != "" {
236-
if _, found := attrs.Get(conventions.AttributeK8SContainerName); !found {
237-
attrs.PutStr(conventions.AttributeK8SContainerName, containerSpec.Name)
238-
}
233+
setResourceAttribute(attrs, conventions.AttributeK8SContainerName, containerSpec.Name)
239234
}
240235
if containerSpec.ImageName != "" {
241-
if _, found := attrs.Get(conventions.AttributeContainerImageName); !found {
242-
attrs.PutStr(conventions.AttributeContainerImageName, containerSpec.ImageName)
243-
}
236+
setResourceAttribute(attrs, conventions.AttributeContainerImageName, containerSpec.ImageName)
244237
}
245238
if containerSpec.ImageTag != "" {
246-
if _, found := attrs.Get(conventions.AttributeContainerImageTag); !found {
247-
attrs.PutStr(conventions.AttributeContainerImageTag, containerSpec.ImageTag)
248-
}
239+
setResourceAttribute(attrs, conventions.AttributeContainerImageTag, containerSpec.ImageTag)
249240
}
250241
// attempt to get container ID from restart count
251242
runID := -1

processor/k8sattributesprocessor/processor_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -1635,3 +1635,62 @@ func (nh *nopHost) GetExtensions() map[component.ID]component.Component {
16351635
func (nh *nopHost) Report(event *componentstatus.Event) {
16361636
nh.reportFunc(event)
16371637
}
1638+
1639+
func Test_setResourceAttribute(t *testing.T) {
1640+
tests := []struct {
1641+
name string
1642+
attributes func() pcommon.Map
1643+
key string
1644+
val string
1645+
wantAttrs func() pcommon.Map
1646+
}{
1647+
{
1648+
name: "attribute not present - add value",
1649+
attributes: pcommon.NewMap,
1650+
key: "foo",
1651+
val: "bar",
1652+
wantAttrs: func() pcommon.Map {
1653+
m := pcommon.NewMap()
1654+
m.PutStr("foo", "bar")
1655+
return m
1656+
},
1657+
},
1658+
{
1659+
name: "attribute present with non-empty value - do not overwrite value",
1660+
attributes: func() pcommon.Map {
1661+
m := pcommon.NewMap()
1662+
m.PutStr("foo", "bar")
1663+
return m
1664+
},
1665+
key: "foo",
1666+
val: "baz",
1667+
wantAttrs: func() pcommon.Map {
1668+
m := pcommon.NewMap()
1669+
m.PutStr("foo", "bar")
1670+
return m
1671+
},
1672+
},
1673+
{
1674+
name: "attribute present with empty value - set value",
1675+
attributes: func() pcommon.Map {
1676+
m := pcommon.NewMap()
1677+
m.PutStr("foo", "")
1678+
return m
1679+
},
1680+
key: "foo",
1681+
val: "bar",
1682+
wantAttrs: func() pcommon.Map {
1683+
m := pcommon.NewMap()
1684+
m.PutStr("foo", "bar")
1685+
return m
1686+
},
1687+
},
1688+
}
1689+
for _, tt := range tests {
1690+
t.Run(tt.name, func(t *testing.T) {
1691+
attrs := tt.attributes()
1692+
setResourceAttribute(attrs, tt.key, tt.val)
1693+
require.Equal(t, tt.wantAttrs(), attrs)
1694+
})
1695+
}
1696+
}

0 commit comments

Comments
 (0)