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

[confmap] Automatically invoke Validate() during Conf.Unmarshal #12031

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

Conversation

TylerHelmuth
Copy link
Member

@TylerHelmuth TylerHelmuth commented Jan 6, 2025

Description

Duplicate component.ConfigValidator into confmap. Add capability for ConfigValidator.Validate to be invoked as part of unmarshal. Add a new UnmarshalOption to allow controlling if validation is invoked.

If we like this feature, we'll need to do some refactoring to how otelcol unmarshals if we want to take advantage of it. See #11524 (comment). After implementing it I'm still not convinced this solution is better than what we already have.

Link to tracking issue

Related to #11524

Testing

Added new unit tests. Tested manually using local build.

Documentation

Added godoc comments.

@TylerHelmuth TylerHelmuth marked this pull request as ready for review January 7, 2025 21:51
@TylerHelmuth TylerHelmuth requested review from mx-psi, evan-bradley and a team as code owners January 7, 2025 21:51
Copy link

codecov bot commented Jan 7, 2025

Codecov Report

Attention: Patch coverage is 88.70968% with 7 lines in your changes missing coverage. Please review.

Project coverage is 91.66%. Comparing base (b682d8e) to head (83c701c).
Report is 16 commits behind head on main.

Files with missing lines Patch % Lines
confmap/confmap.go 88.70% 5 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #12031      +/-   ##
==========================================
- Coverage   91.68%   91.66%   -0.02%     
==========================================
  Files         455      455              
  Lines       24053    24099      +46     
==========================================
+ Hits        22052    22091      +39     
- Misses       1629     1634       +5     
- Partials      372      374       +2     

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

@bogdandrutu
Copy link
Member

bogdandrutu commented Jan 8, 2025

There are some problems with this design, because the "validation" chain may be broken by custom "Unmarshaler" implementations which can lead to invalid configs being used:

struct A {
  struct B {
    struct C {}
    field D
  }
}

func (B) Unmarshal(...) error {
   // Here I may endup manually unmarshaling D and not calling Validate on C/D.
}  

Other problems can be if config is initially unmarshalled in a map then moved to the struct, etc.

To prove this works, we need lots of tests for lots of different usage patterns.

confmap/confmap.go Outdated Show resolved Hide resolved
@TylerHelmuth
Copy link
Member Author

@bogdandrutu I was able to test the custom unmarshal scenario with the otlp receiver and confirmed it works since the custom unmarshal implementation invokes conf.Unmarshal.

If the custom unmarshaller does not invoked conf.Unmarshal it will still work since validate is ultimately called on the top level object that is being unmarshalled into.

I've added a tests to demonstrate these 2 scenarios.

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.

2 participants