-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[pkg/ottl] Add support for optional parameters in functions #20879
Comments
cc codeowners @bogdandrutu @TylerHelmuth @kentquirk |
Hype! |
I am confused about the "ottloptional". We need to ensure that only one optional argument is defined correct? Otherwise from foo("a", "b") (and if there are 2 optional params { first "optional", second, third "optional" }) you will not be able to know which of them are set (first or third). Do we need An alternative to this optional thing we can consider some other options:
|
While not require, we discussed in the SIG meeting and it was agreed that a struct tag removes any ambiguity caused by relying on only the struct's field order. I personally like the tag a lot more. |
@evan-bradley In your ConvertCaseArguments example should the optional arg have ottlarg:2 ? I agree that optional args should always be after required args. |
My goal with
I made it a requirement after the SIG discussion for the sake of consistency, and struct fields without the tag will cause an error during parsing. Personally I'm okay with relying on the struct order, but I understand that it may not be immediately clear that field ordering is guaranteed in Go.
I would support this, I think it removes a lot of ambiguity.
Thanks for catching this, I've edited the code to correct that. |
I have a working prototype of this that uses named arguments in the style of Python's keyword arguments. I'm aiming to have a draft PR ready for design discussion by sometime next week. |
@evan-bradley would you be able to share the working prototype as a draft PR? |
@rnishtala-sumo sure thing, here it is: #24456. |
**Description:** Provides one possible path for allowing functions to define optional parameters in OTTL. This uses Python-style keyword arguments, so function calls will look like `ConvertCase(attributes["source"], delimiter = ".")`. Proposed rules for arguments after this change: 1. camelCase/snake_case versions of the struct field names for the parameter can be used in function calls to specify an argument. 2. All named arguments must come after unnamed arguments. 3. Unnamed arguments must be in function signature order, named arguments can be in any order. 4. Arguments can be passed in unnamed or named. **Link to tracking Issue:** #20879 Co-authored-by: Evan Bradley <[email protected]>
Resolved via #24456 |
**Description:** Provides one possible path for allowing functions to define optional parameters in OTTL. This uses Python-style keyword arguments, so function calls will look like `ConvertCase(attributes["source"], delimiter = ".")`. Proposed rules for arguments after this change: 1. camelCase/snake_case versions of the struct field names for the parameter can be used in function calls to specify an argument. 2. All named arguments must come after unnamed arguments. 3. Unnamed arguments must be in function signature order, named arguments can be in any order. 4. Arguments can be passed in unnamed or named. **Link to tracking Issue:** open-telemetry#20879 Co-authored-by: Evan Bradley <[email protected]>
Component(s)
pkg/ottl
Is your feature request related to a problem? Please describe.
Currently there is no support for optional parameters in OTTL functions. Optional parameters have been proposed as a solution to allow functions to take on more functionality while keeping function calls cleaner in the general case, for example with
ConvertCase
: #18084.Describe the solution you'd like
See prior discussion here: #18822 (comment).
The approach introduced in #14712 opens the possibility to take advantage of the additional factory structure to annotate arguments as optional. One approach for this could be to annotate the fields on the
Arguments
struct as being optional. I think this would need two components to allow both the parser and factory to handle optional parameters:ottloptional
struct tag on the field representing the optional parameter. This would signal to the parser that an argument doesn't necessarily need to be passed in an invocation.ottl.OptionalParameter[T any]
type for optional parameters that don't have a sensible default value. Go's use of zero-values makes it difficult to tell whether an argument has been set or not, so we would need some way of easily specifying this. We could also do this with pointers, but I think a proper type would be a cleaner solution.An example of what this could look like with a type where we want to check whether a parameter has been passed:
Checking whether
ToDelimited
was passed in an invocation could then be done with aToDelimited.IsEmpty()
or something similar.For parameters with default values, here's a contrived example:
Describe alternatives you've considered
We could also add support for keyword/named arguments, e.g.
delete_key(target="attributes", key="test")
which would allow argument reordering and would make having multiple or mutually exclusive optional parameters much easier. However, this would be a larger change and provides an open door for "do everything" functions.Additional context
No response
The text was updated successfully, but these errors were encountered: