-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
How to declare optional parts of the config? #10266
Comments
The way we work around this on the OTLP receiver is opentelemetry-collector/receiver/otlpreceiver/config.go Lines 68 to 73 in 5ba6000
I don't love that approach, but it has worked so far |
Thanks for the pointer. Indeed, I was wrong in the issue description - OTLP receiver does disable If there are no plans or possibilities to have a better solution, feel free to close this issue. |
I would prefer to solve this with a declarative solution instead of requiring manual
I think both of these have trade-offs and I'm not sure which one is the better approach. |
@evan-bradley re (2), how would that be different than just having a pointer? I think we always need a structural distinction between required and optional sub-configs, which is achieved via Optional type in (1). (1) sounds like a good path. I supposed the components would still need to define the complete default configuration, but the |
The problem is that we can also have pointers to config structs that are intended to be references and not just optional values. Combined with default values, this makes it ambiguous to the unmarshaler whether the value is required but has a default, or is truly optional and the pointer should be set to Overall I like (1) more since the handling is more explicit, but it may come with trade-offs that make (2) a better overall solution. |
@yurishkuro If you are willing to own this, I think we would be happy to review a PR that adds a wrapper like this to opentelemetry-collector (the concrete package where this would live is to be decided I guess, but we can discuss this on the PR). |
Not sure if it helps, but OTTL has an |
I fiddled with this a bit in #10260 BTW, don't know why I opened the issue in contrib, we should move it to collector. The issue I bumped into is that I could not find a good place to execute an override to erase default values if the input config omits the corresponding config section. The main confmap code uses decoder hooks, which fire at two relevant occasions
The issue with (2) is that it only fires when there is a corresponding entry in the input config, so cannot be used to unset the default values already in the struct. (1) is equivalent to the custom unmarshaler #10266. This could be the hook that we need, but it needs to:
All of that is of course doable, but manually / from scratch (especially step 3), as all the same functionality done by mapstructure is private. I also looked into using Metadata that can be populated by mapstructure.Decoder. It provides the name of the fields that were "unset" in the config. However, those keys are still the YAML names, so it's not easy to map them back to the corresponding field in the struct. |
One option is to put it on the configuration consumer to check if the value is none. I think this is what we currently do on the fields where we use 'pointer to T'. Another option is to make a type that stores the default value (so, the same as your |
@mx-psi |
Slightly related to the discussion in open-telemetry/opentelemetry-collector-contrib#9478 (cc @mx-psi), but more about the conflict between a desire to have optional configs which nonetheless have defaults. Most obvious example is OTLP Receiver, which is often configured like this:
Here the grpc/http subsections do not have any fields, in particular port numbers, because they are provided by
createDefaultConfig()
. However, it makes it look like one could simple remove e.g.http
to disable creating an HTTP server, which is not actually true - because the default config still populateshttp
even if it's absent in the config.Now if there were no defaults, this would work fine - both
http
andgrpc
fields are declared as pointers and thus could be nil, indicating an absence. But this is completely circumvented by thecreateDefaultConfig()
mechanism.Motivation
Solution?
I don't have a clear one, but it would be nice to have more granular
createDefault
functions that would only be called once the config is read and a given section is seen as non-empty in the config. As I understand the first pass of the config parsing is by reading YAML into nested maps, which are then decoded into the objects.The text was updated successfully, but these errors were encountered: