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] Update ExprFunc and boolExpressionEvaluator to return an error #15649

Merged
merged 7 commits into from
Oct 26, 2022

Conversation

TylerHelmuth
Copy link
Member

Description:
Changes ExprFunc,Get and Set. This allows errors from contexts to be propagated upward to a place where they can be better handled.

This PR only adds the capability to return errors. It does not update any error handling.

Link to tracking Issue:
Related to #13690

Testing:
Updated unit tests

Copy link
Member

@MikeGoldsmith MikeGoldsmith left a comment

Choose a reason for hiding this comment

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

Change overall looks good but there's lots of places we're ignoring the returned error, both in test and functional components (I've tagged some but there were a lot). We should check the error for nils, as the structure for checking shouldn't change.

pkg/ottl/contexts/internal/ottlcommon/metric_test.go Outdated Show resolved Hide resolved
pkg/ottl/contexts/internal/ottlcommon/resource_test.go Outdated Show resolved Hide resolved
pkg/ottl/contexts/internal/ottlcommon/scope_test.go Outdated Show resolved Hide resolved
pkg/ottl/functions_test.go Show resolved Hide resolved
pkg/ottl/ottlfuncs/func_concat.go Outdated Show resolved Hide resolved
@TylerHelmuth
Copy link
Member Author

@MikeGoldsmith I'll update the tests to be more rigorous and the functions to return their errors, but the calling function, Execute will continue to swallow errors for now.

@bogdandrutu
Copy link
Member

bogdandrutu commented Oct 25, 2022

If you do this change, I would suggest to change ExprFunc to an interface Expr. It is more flexible for the future, you can still have the ExprFunc implementing the interface.

type Expr[K any] interface {
  func Eval(ctx context.Context, tCtx K) (any, error)
}

type ExprFunc[K any] func(ctx context.Context, tCtx K) (interface{}, error)

func (f ExprFunc[K]) Eval(ctx context.Context, tCtx K) {
  return f(ctx, tCtx)
}

This way in the future we can have optional interfaces that these instances can implement and based on that can extend the capabilities. With the current type ExprFunc we are locked in this, since we cannot add other private members and does not make too much sense to add extra funcs on it since cannot be configured in any way.

Another option that allows extension is to have a struct:

type Expr[K any] struct {
  EvalFunc[K]
}

func (e Expr[K]) Eval(ctx context.Context, tCtx K) {
  return e.EvalFunc(ctx, tCtx)
}

type EvalFunc[K any] func(ctx context.Context, tCtx K) (interface{}, error)

@TylerHelmuth
Copy link
Member Author

If you do this change, I would suggest to change ExprFunc to an interface Expr

@bogdandrutu this request is being tracked via #14713, do you want to do that as part of this PR or can it be its own?

@TylerHelmuth TylerHelmuth changed the title [pkg/ottl] Update ExprFunc to return an error [pkg/ottl] Update ExprFunc and boolExpressionEvaluator to return an error Oct 25, 2022
@bogdandrutu
Copy link
Member

@TylerHelmuth I think at least we should also add "context.Context" to the func :) since we touch that signature. We can do a separate one for the interface/struct.

@TylerHelmuth
Copy link
Member Author

TylerHelmuth commented Oct 25, 2022

@bogdandrutu we don't have a use case yet for passing context.Context, whereas this PR will enable us to return an error from any ottl context that attempts to use the MetricType setter. Can we hold off on that change until we have a concrete use case, or at least via its own PR?

@bogdandrutu
Copy link
Member

bogdandrutu commented Oct 25, 2022

@bogdandrutu we don't have a use case yet for passing context.Context

  1. It is best practice to pass Context. So you can for example when log something do correlation with tracing.
  2. We can extract things from "Client" part of the Context. See https://github.com/open-telemetry/opentelemetry-collector/tree/main/client

In other words we should pass context almost every time in golang :)

We can do a separate PR for sure, but it should be done.

@TylerHelmuth
Copy link
Member Author

In other words we should pass context almost every time in golang

@bogdandrutu is the claim that any function that accepts context.Context in golang should also pass context.Context to every function its calls?

@bogdandrutu
Copy link
Member

@bogdandrutu is the claim that any function that accepts context.Context in golang should also pass context.Context to every function its calls?

Not "every". But in 90% of the cases yes :)

@TylerHelmuth
Copy link
Member Author

@bogdandrutu ok, I will implement that in a later PR

@bogdandrutu bogdandrutu merged commit 67fad2d into open-telemetry:main Oct 26, 2022
@TylerHelmuth
Copy link
Member Author

@evan-bradley @kentquirk please review.

@TylerHelmuth TylerHelmuth deleted the ottl-getsetter-errors branch October 26, 2022 19:57
shalper2 pushed a commit to shalper2/opentelemetry-collector-contrib that referenced this pull request Dec 6, 2022
…rror (open-telemetry#15649)

* Update ExprFunc to return an error

* fix dependent processors

* Add changelog entry

* Fix lint

* Fix lint

* Apply feedback

* Add error handling all the way through
@plantfansam plantfansam mentioned this pull request Jul 21, 2023
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.

None yet

4 participants