-
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
[confighttp] Review usage of 'pointer to type' #9478
Comments
Some background: Lots of these were added after the initial release of the confighttp, and authors/reviewers decide to use pointers to signal the presence of the field. I do agree that this is not ideal, but would like to have a concrete option/design on how we would support adding new members to the config when we need to know the presence. I believe we can do some tricks with |
Maybe not fully idiomatic Go, but OTTL makes signaling field presence a little easier using an Optional type, which are available out-of-the-box in other languages. We could potentially use a type like this to help make it more obvious when a field is optional, since pointers may not immediately make that intention clear.
I think for config structs the reasons are:
The field docs in confighttp indicate that these fields are optional because there's a default, so I think we can keep these.
It's possible we could do this, but it would be nice if we could make the definition of whether a field is optional exist on the type itself and let the libraries handle the unmarshaling. If we do that we would need to indicate presence, either through a sentinel value, another field on the struct, or a type that supports indicating value presence. I think pointers are a fine way to do this, but would personally prefer a type that is explicitly intended for that purpose, e.g. an |
This reminds me of something. We should add a So we can say that in order to guarantee "backwards compatible behavior" user should always initialize the config with default and overwrite anything as needed instead of creating a struct with "initial value" and leave others unset. |
+1 to using something like Optional, we can probably use @bogdandrutu can you open a separate issue for #9478 (comment) ? (edit: Done, see #9508) |
I recommend we go at this in multiple PRs:
|
) #### Description Set the defaults on `NewDefaultClientConfig`. This change is a no-op since those defaults are set if the value is not set. Add documentation to all fields as well. #### Link to tracking issue #9478 --------- Co-authored-by: Pablo Baeyens <[email protected]>
…ually creating struct This PR makes usage of `NewDefaultClientConfig` instead of manually creating the confighttp.ClientConfig struct. The updates to defaults are maintained (Timeout, Compression, WriteBufferSize) whereas the fields that match the default are not manually set anymore (Endpoint, Headers). Issue: open-telemetry#9478 (comment)
I've looked into this, and here are the options I considered: Using optional type: I've attempted to make the pointer fields in confighttp an Optional type based on this POC: #10260. This does not work as our current custom unmarshalling hook (meant to call We may be able to get this working, if we explicitely ignore the Optional type in our existing hooks, and create a hook specifically meant for our optional type. However, I'm not sure if this added complexity is a better solution than pointers. Using a sentinel value: We could use a sentinel value. That said, I don't think this is a cleaner solution than using pointers. Pointers is a common strategy in Go in order to indicate presence of a field, so I'm unsure how beneficial it would be to move away from this if it adds more complexity. |
Thanks @mackjmr! Based on this and the discussions we have had offline, I also agree that using a pointer seems like the best option. I think to continue the discussion, we can open a PR on the Coding guidelines to settle that this is the way to represent optional stuff. Then the remaining step is to review the existing fields and ensure that them being optional is the right choice (IMO after skimming again through the list it probably is). |
Specify to use pointers to represent optional fields. This is a follow up to: open-telemetry#9478.
We discussed this issue during the 2024-10-09 Collector stability working group meeting and determined that the benefits of adding Optional type need to meaningfully outweigh the complexity it adds in order to offer it in place of using pointers for optional fields. I think Optional fields offer the following advantages over using pointers, based on the usage I see in config structs in core and contrib:
An Optional type would have the following disadvantages:
|
There are a few fields in
confighttp
structs that use e.g. a pointer to int instead of an int. This is an unusual pattern in configuration from components, so I think we should review why we use it and whether it is justified.To fix this issue we need to:
confighttp
based on this.On
configgrpc
we have a similar pattern for theconfigtls
configuration.The text was updated successfully, but these errors were encountered: