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

[pkg/ottl]: Add a new Function Getter to the OTTL package, to allow passing Converters as literal parameters #24356

Merged
merged 7 commits into from
Aug 8, 2023

Conversation

rnishtala-sumo
Copy link
Contributor

@rnishtala-sumo rnishtala-sumo commented Jul 17, 2023

Description: Add a new Function Getter to the OTTL package, to allow passing Converters as literal parameters

Link to tracking Issue:

Testing: Added a unit test and also tested the following configuration

replace_pattern(attributes["message"], "device=*", attributes["device_name"], SHA256)

A tentative example of how the literal function gets invoked is

type ReplacePatternArguments[K any] struct {
	Target       ottl.GetSetter[K]      `ottlarg:"0"`
	RegexPattern string                 `ottlarg:"1"`
	Replacement  ottl.StringGetter[K]   `ottlarg:"2"`
	Function     ottl.FunctionGetter[K] `ottlarg:"3"`
}

func replacePattern[K any](target ottl.GetSetter[K], regexPattern string, replacement ottl.StringGetter[K], fn ottl.Expr[K]) (ottl.ExprFunc[K], error) {
	compiledPattern, err := regexp.Compile(regexPattern)
	if err != nil {
		return nil, fmt.Errorf("the regex pattern supplied to replace_pattern is not a valid pattern: %w", err)
	}
	return func(ctx context.Context, tCtx K) (interface{}, error) {
		originalVal, err := target.Get(ctx, tCtx)
		if err != nil {
			return nil, err
		}
		replacementVal, err := fn.Eval(ctx, tCtx)
		if err != nil {
			return nil, err
		}
          .......
          updatedStr := compiledPattern.ReplaceAllString(originalValStr, replacementValHash.(string))
}

pkg/ottl/expression_test.go Outdated Show resolved Hide resolved
pkg/ottl/expression.go Outdated Show resolved Hide resolved
@TylerHelmuth
Copy link
Member

@rnishtala-sumo can you add some tests to functions_test.go to show the parser can actually inject FunctionGetters correctly?

@TylerHelmuth
Copy link
Member

@evan-bradley this issue is certainly complicated. I want to make sure we're still on track with the right idea, can you take a look.

@evan-bradley
Copy link
Contributor

@evan-bradley this issue is certainly complicated. I want to make sure we're still on track with the right idea, can you take a look.

Sure thing. I have a few ideas, but I want to validate them before I endorse anything. I'll take a look hopefully in the next day or two and report back here.

@rnishtala-sumo
Copy link
Contributor Author

@TylerHelmuth I pushed an update and described the steps here: #24356 (comment)

I also added a test for the new Caller which parses a statement to initialize a converter and then invokes it to return a result.

@rnishtala-sumo rnishtala-sumo force-pushed the function-getter branch 2 times, most recently from 90df6ae to 46b677d Compare July 20, 2023 19:19
@evan-bradley
Copy link
Contributor

Here's a draft implementation of FunctionGetter. My goal here was to do as much validation and reflection up front and only run the actual underlying OTTL function in the hot path. This is definitely just a first pass, I think some work could still be done to improve reliability and the developer experience here. @rnishtala-sumo @TylerHelmuth let me know what you think.
evan-bradley@b6edc4b

@rnishtala-sumo
Copy link
Contributor Author

rnishtala-sumo commented Jul 21, 2023

@evan-bradley The approach you're suggesting makes sense to me. I like the way that the function is being created and returned here, ready for evaluation. And then we just pass in the function and evaluate in the hot path. I like it and am definitely on-board with this approach.

@rnishtala-sumo
Copy link
Contributor Author

@evan-bradley one question I have about this approach is, how are we resolving from a converter's canonical name to its factory function - for example below

replace_pattern(attributes["message"], "device=*", attributes["device_name"], SHA256)

we pass in the canonical name SHA256 but eventually we want to call NewSHA256Factory. An example of this is seen in the newFunctionCall definition here. I thought we could re-use newFunctionCall and that's what I did in my last commit.

@evan-bradley
Copy link
Contributor

The way it's implemented is essentially a stripped down version of newFunctionCall. We do the mapping of name -> factory here, which uses the same map that newFunctionCall uses.

@rnishtala-sumo
Copy link
Contributor Author

@evan-bradley would you like to add to this commit and raise a PR or could I prepare a new PR with this approach along with tests?

@TylerHelmuth are you in agreement with Evan's approach?

@evan-bradley
Copy link
Contributor

@evan-bradley would you like to add to this commit and raise a PR or could I prepare a new PR with this approach along with tests?

Go for it. Feel free to use anything from my commit.

@TylerHelmuth
Copy link
Member

I don't have the bandwidth to review this PR in depth today, but @evan-bradley is OTTL's function wizard, so I'm good with whatever he's suggesting.

@rnishtala-sumo rnishtala-sumo force-pushed the function-getter branch 2 times, most recently from b1d1a99 to 9b744ff Compare July 24, 2023 16:25
@rnishtala-sumo rnishtala-sumo marked this pull request as ready for review July 24, 2023 16:25
@rnishtala-sumo rnishtala-sumo requested a review from a team July 24, 2023 16:25
@rnishtala-sumo rnishtala-sumo force-pushed the function-getter branch 4 times, most recently from 132e723 to 0ae9be3 Compare August 1, 2023 23:04
@rnishtala-sumo
Copy link
Contributor Author

@TylerHelmuth @evan-bradley all the tests seem to indicate that these changes should work. Please let me know if you find anything else.

Copy link
Member

@TylerHelmuth TylerHelmuth left a comment

Choose a reason for hiding this comment

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

@rnishtala-sumo thank you for working on this and sticking with it! Definitely a complicated addition.

pkg/ottl/functions.go Outdated Show resolved Hide resolved
@rnishtala-sumo rnishtala-sumo force-pushed the function-getter branch 2 times, most recently from 0e8c80c to 834f814 Compare August 3, 2023 19:07
Copy link
Contributor

@evan-bradley evan-bradley left a comment

Choose a reason for hiding this comment

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

Thanks for your persistence in working on this. I'd say we're really close.

pkg/ottl/expression.go Outdated Show resolved Hide resolved
pkg/ottl/expression.go Outdated Show resolved Hide resolved
pkg/ottl/expression.go Outdated Show resolved Hide resolved
pkg/ottl/expression.go Outdated Show resolved Hide resolved
pkg/ottl/expression.go Outdated Show resolved Hide resolved
pkg/ottl/expression.go Outdated Show resolved Hide resolved
pkg/ottl/functions.go Outdated Show resolved Hide resolved
@evan-bradley
Copy link
Contributor

@rnishtala-sumo After reviewing your changes, I realized that the ottlarg struct tag isn't really worth the complexity. I spoke about it offline with @TylerHelmuth and we agreed that it would be okay to deprecate it. I opened #24874 to track this. It may save you some effort in implementing handling the struct tag if that issue is resolved prior to this PR getting merged. If you'd like to push ahead with this instead and work with the struct tag, that's also okay.

@rnishtala-sumo
Copy link
Contributor Author

@evan-bradley I would like to get this in with the struct tag and let any breaking changes introduced by removing the 'ottlarg` struct tag be its own PR. While I agree that relying on the top-down ordering of fields might make struct tags redundant, I'm not sure if that should hold up this PR.

@evan-bradley
Copy link
Contributor

evan-bradley commented Aug 4, 2023

That's good with me, let's continue as planned.

@rnishtala-sumo rnishtala-sumo force-pushed the function-getter branch 3 times, most recently from 371c565 to 002f549 Compare August 4, 2023 20:28
pkg/ottl/expression.go Outdated Show resolved Hide resolved
pkg/ottl/expression.go Outdated Show resolved Hide resolved
@TylerHelmuth TylerHelmuth merged commit f9041bd into open-telemetry:main Aug 8, 2023
@github-actions github-actions bot added this to the next release milestone Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants