Skip to content
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

[config] Deprecate config.UnmarshalConfig #9750

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

atoulme
Copy link
Contributor

@atoulme atoulme commented Mar 13, 2024

Description:
This PR removes the top level if/else in component.UnmarshalConfig, handling recursive state in the confmap.Conf object instead.
This PR deprecates component.UnmarshalConfig in favor of calling directly Unmarshal on the confmap.Conf object.

Link to tracking Issue:
Fixes #7102
Fixes #7101

@atoulme atoulme requested a review from a team as a code owner March 13, 2024 01:30
@atoulme atoulme requested a review from jpkrohling March 13, 2024 01:30
Copy link

codecov bot commented Mar 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.02%. Comparing base (d1e631b) to head (dfa57f1).

❗ Current head dfa57f1 differs from pull request most recent head 05a0d08. Consider uploading reports for the commit 05a0d08 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9750      +/-   ##
==========================================
- Coverage   91.67%   91.02%   -0.65%     
==========================================
  Files         360      353       -7     
  Lines       16642    18633    +1991     
==========================================
+ Hits        15256    16961    +1705     
- Misses       1052     1347     +295     
+ Partials      334      325       -9     

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

@atoulme
Copy link
Contributor Author

atoulme commented Mar 13, 2024

Contrib tests fix: open-telemetry/opentelemetry-collector-contrib#31727

cmd/mdatagen/loader.go Outdated Show resolved Hide resolved
cmd/mdatagen/metricdata.go Outdated Show resolved Hide resolved
component/config_test.go Show resolved Hide resolved
confmap/confmap.go Outdated Show resolved Hide resolved
@@ -37,7 +40,8 @@ func NewFromStringMap(data map[string]any) *Conf {
// Conf represents the raw configuration map for the OpenTelemetry Collector.
// The confmap.Conf can be unmarshalled into the Collector's config using the "service" package.
type Conf struct {
k *koanf.Koanf
k *koanf.Koanf
self any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before I think we could call the same Conf to unmarshal it on two different structs, but now it can only be used once, right? Is there any way we can restore that property? If not, is there a way where we can error out if we have already unmarshaled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't break your ability to use the same Conf to unmarshal on two different structs. It stops recursive calls as part of unmarshaling from calling Unmarshal on the same object twice.

confmap/confmap.go Outdated Show resolved Hide resolved
dmitryax pushed a commit that referenced this pull request Mar 14, 2024
This is a split of #9750 that tries to work around mapstructure, which
wraps an error around a decoding error.

In the case when an error is returned from a top level construct, we get
a not so helpful message that says:
```
error decoding '': error running encode hook: marshaling error
```

With this change, the error is unwrapped, giving the following string
representation:
```
error running encode hook: marshaling error
```

Because #9750 enforces going through mapstructure, it would change
errors returned with this not-so-helpful preamble. Adding this removes
the problem.
dmitryax pushed a commit that referenced this pull request Mar 14, 2024
This change is required in preparation of #9750 

This removes the call to `component.UnmarshalConfig` in preparation of
its deprecation, and instead has the `Conf` object unmarshal itself into
the `Config` struct.
@atoulme atoulme force-pushed the embedded_struct_unmarshaler branch from 51e5f78 to dfa57f1 Compare March 14, 2024 21:35
dmitryax pushed a commit that referenced this pull request Mar 15, 2024
Reverts #9765

We need to revert those changes as contrib has issues with them in
isolation from #9750.
@mx-psi
Copy link
Member

mx-psi commented Mar 18, 2024

Can you add 'Fixes #7101' to the PR description?

for i := 0; i < to.Type().NumField(); i++ {
// check embedded structs:
f := to.Type().Field(i)
if f.IsExported() && f.Anonymous && slices.Contains(strings.Split(f.Tag.Get("mapstructure"), ","), "squash") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to check for the mapstructure tag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is explained in the comment above: if the embedded struct is using the ,squash value for the mapstructure key, then this struct Unmarshal method should not be used as the main struct unmarshaling.

mx-psi pushed a commit to open-telemetry/opentelemetry-collector-contrib that referenced this pull request Mar 27, 2024
**Description:** 
This is a companion PR to handle recursive state of unmarshalers with
open-telemetry/opentelemetry-collector#9750.
Changing this behavior will allow the confmap.Conf object to recognize
that it has already run the `Unmarshal` method on the struct, and run
the mapstructure decoding of fields.
dmitryax pushed a commit that referenced this pull request Apr 3, 2024
…squashing (#9861)

This is taking a small slice of #9750 to document the behavior of
confmap and make sure we can unmarshal embedded structs.
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added Stale and removed Stale labels Apr 10, 2024
@atoulme atoulme force-pushed the embedded_struct_unmarshaler branch from 429f386 to 4445cc4 Compare April 21, 2024 00:25
@atoulme atoulme force-pushed the embedded_struct_unmarshaler branch from 4445cc4 to 05a0d08 Compare April 21, 2024 00:28
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.

Remove component.UnmarshalConfig Investigate alternative for Unmarshaler and Marshaler
3 participants