Skip to content

[confmap] Enable DecodeNil option #12996

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

Merged
merged 4 commits into from
May 13, 2025
Merged

Conversation

mx-psi
Copy link
Member

@mx-psi mx-psi commented May 8, 2025

Description

Enables DecoderConfig.DecodeNil

Link to tracking issue

Taken from #10260
Updates #12981

@github-actions github-actions bot requested a review from evan-bradley May 8, 2025 11:30
@mx-psi mx-psi force-pushed the mx-psi/decodenil branch from 263318b to b09ab27 Compare May 8, 2025 11:36
Copy link

codecov bot commented May 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.64%. Comparing base (5800834) to head (2057ce2).
Report is 11 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #12996      +/-   ##
==========================================
+ Coverage   91.62%   91.64%   +0.02%     
==========================================
  Files         504      504              
  Lines       27888    27886       -2     
==========================================
+ Hits        25553    25557       +4     
+ Misses       1844     1840       -4     
+ Partials      491      489       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mx-psi

This comment was marked as resolved.

@mx-psi mx-psi mentioned this pull request May 8, 2025
@yurishkuro
Copy link
Member

yurishkuro commented May 8, 2025

The DecodeHookFuncType style hooks are fundamentally incompatible with DecodeNil because they are forcing a loss of information, see go-viper/mapstructure#37 (comment). I was even suggesting a new "ultimate" hook type but later realized that we can get away with DecodeHookFuncValue if we change our hook implementations to return reflect.Value (allowed by the signature) instead of the actual interface, and @mahadzaryab1 added a fix for that in go-viper/mapstructure#52

So TLDR - OTEL hooks need to be modified to match DecodeHookFuncValue signature AND to return the reflect.Value (especially when they are not making any changes) to preserve the type information.

@mx-psi
Copy link
Member Author

mx-psi commented May 8, 2025

@yurishkuro Okay, thanks. It still would make sense to me for mapstructure to give a more meaningful error rather than panic here, but I see what you mean.

Can you review #13001 to move existing hooks into reflect.Value?

github-merge-queue bot pushed a commit that referenced this pull request May 9, 2025
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

If we enable `DecodeNil` as true, we may have [untyped
nils](https://go.dev/doc/faq#nil_error) being passed. Unfortunately,
these are not valid values for `reflect`, which leads to surprising
behavior such as golang/go/issues/51649.

Unfortunately, the default hooks from mapstructure do not deal with this
properly. To account for this, we:
- Vendor and change the `ComposeDecodeHookFunc` function so that this
case is accounted for the kinds of hooks that just won't work with
untyped nils
- Create a safe wrapper for the hooks that do work with untyped nils.
This wrapper is used in all hooks, but in the interest of keeping as
close to what I would imagine upstream will accept, I did not add this
to the compose function.

This should not have any end-user observable behavior.

<!-- Issue number if applicable -->
#### Link to tracking issue

Attempt to work around
#12996 (comment)

---------

Co-authored-by: Evan Bradley <[email protected]>
@mx-psi mx-psi marked this pull request as ready for review May 9, 2025 15:59
@mx-psi mx-psi requested a review from a team as a code owner May 9, 2025 15:59
@mx-psi
Copy link
Member Author

mx-psi commented May 9, 2025

@evan-bradley You can see on https://app.codecov.io/gh/open-telemetry/opentelemetry-collector/pull/12996/indirect-changes that this change leads to increased coverage on somes lines added over at #13001

Is there any other test you think we should add? I am honestly a bit scared every time I make changes on confmap, but in this case I am not even sure about what tests I would add here

@evan-bradley
Copy link
Contributor

@evan-bradley You can see on app.codecov.io/gh/open-telemetry/opentelemetry-collector/pull/12996/indirect-changes that this change leads to increased coverage on somes lines added over at #13001

Great, thank you.

Is there any other test you think we should add?

I can look through it more, some initial ideas:

  1. Are there any tests we should re-add from [confmap] Maintain nil slice values when marshaling and unmarshaling #11882?
  2. Were any tests added that verify we won't run into the same issue that [confmap] Maintain nil slice values when marshaling and unmarshaling #11882 introduced?

I am honestly a bit scared every time I make changes on confmap, but in this case I am not even sure about what tests I would add here

Same here. I think if we merge this after today's release, that should hopefully give us two weeks to at least think it over or potentially catch any issues found by anyone using the latest from main.

@evan-bradley
Copy link
Contributor

We discussed this offline and #12871 added tests that cover both of the situations I was asking about. At the moment I can't think of any other scenarios we need to test.

@mx-psi mx-psi added this pull request to the merge queue May 13, 2025
Merged via the queue into open-telemetry:main with commit e9f3dec May 13, 2025
74 checks passed
@mx-psi mx-psi deleted the mx-psi/decodenil branch May 13, 2025 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants