Skip to content

Commit 6996280

Browse files
authored
Remove unnecessary full copy of maps/slices when setting value on sub-map (open-telemetry#43949)
The problems happens when assume inside "attributes" there is a key called "tags" that is a pcommon.Map. Here is an example: ``` delete_key(attributes[tags], "test_key") where metric.name = "foo" ``` Because of the changes to always call set value on the target (see open-telemetry#42350) we will get to do a SetValue where the src and destination are the same, but because we always set the destination to emptyMap will do a full copy of the sub-map tags. Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 282046b commit 6996280

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

.chloggen/rm-unnecessary-copy.yaml

Lines changed: 27 additions & 0 deletions
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: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Remove unnecessary full copy of maps/slices when setting value on sub-map
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: [43949]
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: [user]

pkg/ottl/contexts/internal/ctxutil/value.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,21 @@ func SetValue(value pcommon.Value, val any) error {
5959
err = SetValue(pval, a)
6060
}
6161
case pcommon.Slice:
62-
v.CopyTo(value.SetEmptySlice())
62+
var dest pcommon.Slice
63+
if value.Type() == pcommon.ValueTypeSlice {
64+
dest = value.Slice()
65+
} else {
66+
dest = value.SetEmptySlice()
67+
}
68+
v.CopyTo(dest)
6369
case pcommon.Map:
64-
v.CopyTo(value.SetEmptyMap())
70+
var dest pcommon.Map
71+
if value.Type() == pcommon.ValueTypeMap {
72+
dest = value.Map()
73+
} else {
74+
dest = value.SetEmptyMap()
75+
}
76+
v.CopyTo(dest)
6577
case map[string]any:
6678
err = value.FromRaw(v)
6779
}

0 commit comments

Comments
 (0)