-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[pkg/ottl] Add merge_maps function #16461
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that making this a function instead of a factory function is the right move in terms of efficiency for probably the majority of use cases. However, I can also see more complex uses arising for transforming maps, like KeepKeys(MergeMaps(ParseJSON(body), resource.attributes), "http_.*")
. One way to do both could be to have factory function versions of some functions, like merge_maps
and MergeMaps
, but that risks being confusing to users. Alternatively, we could sacrifice some efficiency and standardize on using factory functions with set
, or just forgo the factory function versions for now until there are requests for more complex functionality. What do you think?
if err != nil { | ||
return nil, err | ||
} | ||
if targetMap, ok := targetVal.(pcommon.Map); ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessarily an issue that needs to be solved in this PR, but I think we should do something to notify the user that they've provided an invalid value. Should we emit a debug log if either of the values aren't maps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya this is a problem with lots of our functions. Need to figure out a good way to guarantee access to the telemetrysettings.Logger in all functions. We could enforce telemetrysettings to always be the first param as a start.
pkg/ottl/ottlfuncs/README.md
Outdated
@@ -238,6 +239,27 @@ Examples: | |||
|
|||
- `limit(resource.attributes, 50, ["http.host", "http.method"])` | |||
|
|||
### merge_maps | |||
|
|||
`merge_maps(target, source)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just "merge" and say for the moment only works with maps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can try this out. Is slice the only other type that we'd support in a merge function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bogdandrutu updated the name to merge, but with that addition of the strategy
param, is there another type that you think we'll be supporting in a merge function in the future?
@evan-bradley I think in the situation of
If I am counting correctly thats 1 loop inside If we go with
I think that results in 1 loop within Overall going the Factory Function route would mean more flexibility, but I kinda like the simplicity of |
5bfd142
to
8764f6b
Compare
@djaglowski @evan-bradley @bogdandrutu @kentquirk ready for re-review. |
I definitely agree here, just wanted to bring up a few options. I think this implementation is the best route for now, we can consider additional paths forward if we need more flexibility. |
@evan-bradley thanks for the great discussion. I think the more log transformations we enable the more times we're gonna have to decide whether we want to chain factory functions or if multiple statements can handle the use case. Once conditions get involved its nice to be able to do things in a single statement so that the condition doesn't have to be spread between multiple statements. Could be a reason to allow OTTL's grammar to support multiple Invocations per condition. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. I think we should probably keep the name as merge_maps
considering the merge strategies don't make sense for slices, and having disjoint sets of strategies in a single function signature seems like a likely source of confusion.
a406666
to
4801358
Compare
I like the strategy parameter and associated values. An additional aspect of merging to consider is recursion. Should we have a way to support the following? target:
a: 1
b: 2
details:
important: foo
source:
b: 3
c: 4
details:
not_important: bar
result:
a: 1
b: 3
c: 4
details:
important: foo
not_important: bar |
@djaglowski that's a good point. How does Stanza handle this? |
It doesn't actually, just works like I don't feel strongly that we should support it. It introduces a lot of complexity and it's not clearly needed. |
@djaglowski In that case I'd vote for moving this forward as is for now and supporting that capability once we have a specific request/need. |
* Add merge_maps function
Description:
Adds a new function that is able to gracefully merge a source map into a target map. If target already contains a key from source, the source's value is used.
For efficiency, I opted for a Function for this capability instead of a Factory Function combined with
set
for efficiency.Link to tracking Issue:
Related to #9410
Related to #14938
Testing:
added unit tests
Documentation:
updated function doc