-
Notifications
You must be signed in to change notification settings - Fork 14
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
Allow DecodeHook to run for zero values #37
Comments
Any feedback from maintainers? Will you accept a PR implementing the proposal? |
Hey @yurishkuro! I think your proposal makes sense. I'd be happy to accept a PR. |
@sagikazarmark The PR is ready for review at #42. Thank you so much! |
hey @sagikazarmark! just wanted to follow up and ask if it'd be possible to get a review on #42 ? |
Thanks for the ping! I'll take a look. |
Thank you so much! |
@sagikazarmark @mahadzaryab1 turns out the latest fix is still not enough. I tracked the issue to the transition captured in the following log statements (where
This transformation reproduced here https://go.dev/play/p/byXvFSo01nk. Basically, the hooks have a fundamental design issue that they do not tolerate What we have in OTEL Collector is a half-dozen long chain combined into a Composite hook where this transformation from value to One option I see out of that is by introducing the ultimate 4th signature for the hook function that does not force it to drop into the value space:
All previous signatures can be reduced to this one as they are special cases. However, in order to achieve This is not a trivial change so would like to run it by here for a stink test. |
This looks great to me! Let me know if you need help in landing the patch in mapstructure for adding the new decode hook function signature. |
@yurishkuro, apologies; I forgot to reply here. If I understand correctly, you propose adding a 4th type of decode hook that would become standard internal signature (currently being converted here). Am I correct? Although it may come with a minor performance hit (would be nice to see how much), I don't think we need to change any of the existing hooks (which would be a massive BC break anyway). But that also means you need to reimplement all of the hooks you want to use in OTEL (or we do that here with different names or something). In any case, I think those are two separate issues. We can tackle the first one and figure out what to do with the second later. Maybe we can tag a v3 if necessary. WDYT? Are you open to sending a PR? |
Thanks @sagikazarmark - since you're ok with the direction of introducing the "ultimate" hook signature, @mahadzaryab1 and I can work on a prototype. I don't expect any performance hit from this change because internally the framework already treats hooks as if they can get and return I expect this change to be fully backwards compatible, it's just a new flavor of the hook that does not require additional wrappers from the framework. Yes, we will need to change OTEL's hooks to the new all-Value signature in order to get to the original desired functionality, but that's not a problem. |
I just realized that we might be able to get the same functionality without a new hook signature, just by changing this line Line 103 in 0b972d9
Instead of doing unconditional |
@yurishkuro I can try giving this a shot. When you say the existing hooks can just return |
Use Case
In the OpenTelemetry Collector we support a configuration like this:
Here the
http:
entry has no fields, but its presence indicate that the user wants an HTTP server, with all standard defaults. Ifhttp:
is missing than this server will not be started.Problem
Today we're handling it with a custom hook for the
receiver:
which catches the scenario and applies the defaults. The hook is brittle as it depends on the exact YAML field name.We've been experimenting with a custom "Optional" type that would allow to express this much cleaner:
Here
optional.WithDefault
returnsOptional
that has no value, but if during unmarshaling it sees that there was a corresponding entry it first creates default value and then runs normal unmarshal on it.The issue is that this approach doesn't work today because when
mapstructure
sees an empty valuehttp:
it just exists, without running the decode hook (which could've made the result non-empty).mapstructure/mapstructure.go
Lines 442 to 453 in 0382e5b
Proposal
Could we add another config flag to allow not bailing early on empty values, at least to allow decoder hooks to run?
The text was updated successfully, but these errors were encountered: