Skip to content

Commit

Permalink
[internal/filterottl] Switch to match instead of drop (open-telemet…
Browse files Browse the repository at this point in the history
…ry#27471)

**Description:** 
We noticed that if you pass in a condition that fails parsing that the
error message includes `drop() where ...` and it was jarring to see
`drop` as the function when using filterottl to generate a condition
that was not related to dropping data. While we wait for the ability to
parse conditions in isolation
(open-telemetry#13545),
this change makes `filterottl` a little friendlier.

**Testing:** <Describe what testing was performed and which tests were
added.>
Updated unit tests
  • Loading branch information
TylerHelmuth authored Oct 10, 2023
1 parent 8da82c1 commit 5fcd082
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
50 changes: 25 additions & 25 deletions internal/filter/filterottl/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (

// NewBoolExprForSpan creates a BoolExpr[ottlspan.TransformContext] that will return true if any of the given OTTL conditions evaluate to true.
// The passed in functions should use the ottlspan.TransformContext.
// If a function named `drop` is not present in the function map it will be added automatically so that parsing works as expected
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
func NewBoolExprForSpan(conditions []string, functions map[string]ottl.Factory[ottlspan.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (expr.BoolExpr[ottlspan.TransformContext], error) {
drop := newDropFactory[ottlspan.TransformContext]()
if _, ok := functions[drop.Name()]; !ok {
functions[drop.Name()] = drop
match := newMatchFactory[ottlspan.TransformContext]()
if _, ok := functions[match.Name()]; !ok {
functions[match.Name()] = match
}
statmentsStr := conditionsToStatements(conditions)
parser, err := ottlspan.NewParser(functions, set)
Expand All @@ -39,11 +39,11 @@ func NewBoolExprForSpan(conditions []string, functions map[string]ottl.Factory[o

// NewBoolExprForSpanEvent creates a BoolExpr[ottlspanevent.TransformContext] that will return true if any of the given OTTL conditions evaluate to true.
// The passed in functions should use the ottlspanevent.TransformContext.
// If a function named `drop` is not present in the function map it will be added automatically so that parsing works as expected
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
func NewBoolExprForSpanEvent(conditions []string, functions map[string]ottl.Factory[ottlspanevent.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (expr.BoolExpr[ottlspanevent.TransformContext], error) {
drop := newDropFactory[ottlspanevent.TransformContext]()
if _, ok := functions[drop.Name()]; !ok {
functions[drop.Name()] = drop
match := newMatchFactory[ottlspanevent.TransformContext]()
if _, ok := functions[match.Name()]; !ok {
functions[match.Name()] = match
}
statmentsStr := conditionsToStatements(conditions)
parser, err := ottlspanevent.NewParser(functions, set)
Expand All @@ -60,11 +60,11 @@ func NewBoolExprForSpanEvent(conditions []string, functions map[string]ottl.Fact

// NewBoolExprForMetric creates a BoolExpr[ottlmetric.TransformContext] that will return true if any of the given OTTL conditions evaluate to true.
// The passed in functions should use the ottlmetric.TransformContext.
// If a function named `drop` is not present in the function map it will be added automatically so that parsing works as expected
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
func NewBoolExprForMetric(conditions []string, functions map[string]ottl.Factory[ottlmetric.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (expr.BoolExpr[ottlmetric.TransformContext], error) {
drop := newDropFactory[ottlmetric.TransformContext]()
if _, ok := functions[drop.Name()]; !ok {
functions[drop.Name()] = drop
match := newMatchFactory[ottlmetric.TransformContext]()
if _, ok := functions[match.Name()]; !ok {
functions[match.Name()] = match
}
statmentsStr := conditionsToStatements(conditions)
parser, err := ottlmetric.NewParser(functions, set)
Expand All @@ -81,11 +81,11 @@ func NewBoolExprForMetric(conditions []string, functions map[string]ottl.Factory

// NewBoolExprForDataPoint creates a BoolExpr[ottldatapoint.TransformContext] that will return true if any of the given OTTL conditions evaluate to true.
// The passed in functions should use the ottldatapoint.TransformContext.
// If a function named `drop` is not present in the function map it will be added automatically so that parsing works as expected
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
func NewBoolExprForDataPoint(conditions []string, functions map[string]ottl.Factory[ottldatapoint.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (expr.BoolExpr[ottldatapoint.TransformContext], error) {
drop := newDropFactory[ottldatapoint.TransformContext]()
if _, ok := functions[drop.Name()]; !ok {
functions[drop.Name()] = drop
match := newMatchFactory[ottldatapoint.TransformContext]()
if _, ok := functions[match.Name()]; !ok {
functions[match.Name()] = match
}
statmentsStr := conditionsToStatements(conditions)
parser, err := ottldatapoint.NewParser(functions, set)
Expand All @@ -102,11 +102,11 @@ func NewBoolExprForDataPoint(conditions []string, functions map[string]ottl.Fact

// NewBoolExprForLog creates a BoolExpr[ottllog.TransformContext] that will return true if any of the given OTTL conditions evaluate to true.
// The passed in functions should use the ottllog.TransformContext.
// If a function named `drop` is not present in the function map it will be added automatically so that parsing works as expected
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
func NewBoolExprForLog(conditions []string, functions map[string]ottl.Factory[ottllog.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (expr.BoolExpr[ottllog.TransformContext], error) {
drop := newDropFactory[ottllog.TransformContext]()
if _, ok := functions[drop.Name()]; !ok {
functions[drop.Name()] = drop
match := newMatchFactory[ottllog.TransformContext]()
if _, ok := functions[match.Name()]; !ok {
functions[match.Name()] = match
}
statmentsStr := conditionsToStatements(conditions)
parser, err := ottllog.NewParser(functions, set)
Expand All @@ -123,11 +123,11 @@ func NewBoolExprForLog(conditions []string, functions map[string]ottl.Factory[ot

// NewBoolExprForResource creates a BoolExpr[ottlresource.TransformContext] that will return true if any of the given OTTL conditions evaluate to true.
// The passed in functions should use the ottlresource.TransformContext.
// If a function named `drop` is not present in the function map it will be added automatically so that parsing works as expected
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
func NewBoolExprForResource(conditions []string, functions map[string]ottl.Factory[ottlresource.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (expr.BoolExpr[ottlresource.TransformContext], error) {
drop := newDropFactory[ottlresource.TransformContext]()
if _, ok := functions[drop.Name()]; !ok {
functions[drop.Name()] = drop
match := newMatchFactory[ottlresource.TransformContext]()
if _, ok := functions[match.Name()]; !ok {
functions[match.Name()] = match
}
statmentsStr := conditionsToStatements(conditions)
parser, err := ottlresource.NewParser(functions, set)
Expand All @@ -145,7 +145,7 @@ func NewBoolExprForResource(conditions []string, functions map[string]ottl.Facto
func conditionsToStatements(conditions []string) []string {
statements := make([]string, len(conditions))
for i, condition := range conditions {
statements[i] = "drop() where " + condition
statements[i] = "match() where " + condition
}
return statements
}
12 changes: 6 additions & 6 deletions internal/filter/filterottl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ func StandardResourceFuncs() map[string]ottl.Factory[ottlresource.TransformConte

func standardFuncs[K any]() map[string]ottl.Factory[K] {
m := ottlfuncs.StandardConverters[K]()
f := newDropFactory[K]()
f := newMatchFactory[K]()
m[f.Name()] = f
return m
}

func newDropFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("drop", nil, createDropFunction[K])
func newMatchFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("match", nil, createMatchFunction[K])
}

func createDropFunction[K any](_ ottl.FunctionContext, _ ottl.Arguments) (ottl.ExprFunc[K], error) {
return dropFn[K]()
func createMatchFunction[K any](_ ottl.FunctionContext, _ ottl.Arguments) (ottl.ExprFunc[K], error) {
return matchFn[K]()
}

func dropFn[K any]() (ottl.ExprFunc[K], error) {
func matchFn[K any]() (ottl.ExprFunc[K], error) {
return func(context.Context, K) (interface{}, error) {
return true, nil
}, nil
Expand Down

0 comments on commit 5fcd082

Please sign in to comment.