Skip to content

Commit 010f609

Browse files
edmocostachengchuanpeng
authored andcommitted
[internal/filter] Add filterottl functions with additional options (open-telemetry#37271)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description The `transformprocessor` is currently using the `internal/filter/filterottl` to create `conditions` expressions. In order to support path's with context prefix, it needs to configure the underline `ottl.Parser` using their `EnablePathContextNames` options. This PR changes this internal API adding new functions that accepts the parsers options as argument. <!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. --> #### Link to tracking issue Related to open-telemetry#29017 <!--Describe what testing was performed and which tests were added.--> #### Testing Unit tests <!--Please delete paragraphs that you did not use before submitting.-->
1 parent df1edde commit 010f609

File tree

2 files changed

+119
-7
lines changed

2 files changed

+119
-7
lines changed

internal/filter/filterottl/filter.go

+42-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ import (
2020
// The passed in functions should use the ottlspan.TransformContext.
2121
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
2222
func NewBoolExprForSpan(conditions []string, functions map[string]ottl.Factory[ottlspan.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (*ottl.ConditionSequence[ottlspan.TransformContext], error) {
23-
parser, err := ottlspan.NewParser(functions, set)
23+
return NewBoolExprForSpanWithOptions(conditions, functions, errorMode, set, nil)
24+
}
25+
26+
// NewBoolExprForSpanWithOptions is like NewBoolExprForSpan, but with additional options.
27+
func NewBoolExprForSpanWithOptions(conditions []string, functions map[string]ottl.Factory[ottlspan.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings, parserOptions []ottlspan.Option) (*ottl.ConditionSequence[ottlspan.TransformContext], error) {
28+
parser, err := ottlspan.NewParser(functions, set, parserOptions...)
2429
if err != nil {
2530
return nil, err
2631
}
@@ -36,7 +41,12 @@ func NewBoolExprForSpan(conditions []string, functions map[string]ottl.Factory[o
3641
// The passed in functions should use the ottlspanevent.TransformContext.
3742
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
3843
func NewBoolExprForSpanEvent(conditions []string, functions map[string]ottl.Factory[ottlspanevent.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (*ottl.ConditionSequence[ottlspanevent.TransformContext], error) {
39-
parser, err := ottlspanevent.NewParser(functions, set)
44+
return NewBoolExprForSpanEventWithOptions(conditions, functions, errorMode, set, nil)
45+
}
46+
47+
// NewBoolExprForSpanEventWithOptions is like NewBoolExprForSpanEvent, but with additional options.
48+
func NewBoolExprForSpanEventWithOptions(conditions []string, functions map[string]ottl.Factory[ottlspanevent.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings, parserOptions []ottlspanevent.Option) (*ottl.ConditionSequence[ottlspanevent.TransformContext], error) {
49+
parser, err := ottlspanevent.NewParser(functions, set, parserOptions...)
4050
if err != nil {
4151
return nil, err
4252
}
@@ -52,7 +62,12 @@ func NewBoolExprForSpanEvent(conditions []string, functions map[string]ottl.Fact
5262
// The passed in functions should use the ottlmetric.TransformContext.
5363
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
5464
func NewBoolExprForMetric(conditions []string, functions map[string]ottl.Factory[ottlmetric.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (*ottl.ConditionSequence[ottlmetric.TransformContext], error) {
55-
parser, err := ottlmetric.NewParser(functions, set)
65+
return NewBoolExprForMetricWithOptions(conditions, functions, errorMode, set, nil)
66+
}
67+
68+
// NewBoolExprForMetricWithOptions is like NewBoolExprForMetric, but with additional options.
69+
func NewBoolExprForMetricWithOptions(conditions []string, functions map[string]ottl.Factory[ottlmetric.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings, parserOptions []ottlmetric.Option) (*ottl.ConditionSequence[ottlmetric.TransformContext], error) {
70+
parser, err := ottlmetric.NewParser(functions, set, parserOptions...)
5671
if err != nil {
5772
return nil, err
5873
}
@@ -68,7 +83,12 @@ func NewBoolExprForMetric(conditions []string, functions map[string]ottl.Factory
6883
// The passed in functions should use the ottldatapoint.TransformContext.
6984
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
7085
func NewBoolExprForDataPoint(conditions []string, functions map[string]ottl.Factory[ottldatapoint.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (*ottl.ConditionSequence[ottldatapoint.TransformContext], error) {
71-
parser, err := ottldatapoint.NewParser(functions, set)
86+
return NewBoolExprForDataPointWithOptions(conditions, functions, errorMode, set, nil)
87+
}
88+
89+
// NewBoolExprForDataPointWithOptions is like NewBoolExprForDataPoint, but with additional options.
90+
func NewBoolExprForDataPointWithOptions(conditions []string, functions map[string]ottl.Factory[ottldatapoint.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings, parserOptions []ottldatapoint.Option) (*ottl.ConditionSequence[ottldatapoint.TransformContext], error) {
91+
parser, err := ottldatapoint.NewParser(functions, set, parserOptions...)
7292
if err != nil {
7393
return nil, err
7494
}
@@ -84,7 +104,12 @@ func NewBoolExprForDataPoint(conditions []string, functions map[string]ottl.Fact
84104
// The passed in functions should use the ottllog.TransformContext.
85105
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
86106
func NewBoolExprForLog(conditions []string, functions map[string]ottl.Factory[ottllog.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (*ottl.ConditionSequence[ottllog.TransformContext], error) {
87-
parser, err := ottllog.NewParser(functions, set)
107+
return NewBoolExprForLogWithOptions(conditions, functions, errorMode, set, nil)
108+
}
109+
110+
// NewBoolExprForLogWithOptions is like NewBoolExprForLog, but with additional options.
111+
func NewBoolExprForLogWithOptions(conditions []string, functions map[string]ottl.Factory[ottllog.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings, parserOptions []ottllog.Option) (*ottl.ConditionSequence[ottllog.TransformContext], error) {
112+
parser, err := ottllog.NewParser(functions, set, parserOptions...)
88113
if err != nil {
89114
return nil, err
90115
}
@@ -100,7 +125,12 @@ func NewBoolExprForLog(conditions []string, functions map[string]ottl.Factory[ot
100125
// The passed in functions should use the ottlresource.TransformContext.
101126
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
102127
func NewBoolExprForResource(conditions []string, functions map[string]ottl.Factory[ottlresource.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (*ottl.ConditionSequence[ottlresource.TransformContext], error) {
103-
parser, err := ottlresource.NewParser(functions, set)
128+
return NewBoolExprForResourceWithOptions(conditions, functions, errorMode, set, nil)
129+
}
130+
131+
// NewBoolExprForResourceWithOptions is like NewBoolExprForResource, but with additional options.
132+
func NewBoolExprForResourceWithOptions(conditions []string, functions map[string]ottl.Factory[ottlresource.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings, parserOptions []ottlresource.Option) (*ottl.ConditionSequence[ottlresource.TransformContext], error) {
133+
parser, err := ottlresource.NewParser(functions, set, parserOptions...)
104134
if err != nil {
105135
return nil, err
106136
}
@@ -116,7 +146,12 @@ func NewBoolExprForResource(conditions []string, functions map[string]ottl.Facto
116146
// The passed in functions should use the ottlresource.TransformContext.
117147
// If a function named `match` is not present in the function map it will be added automatically so that parsing works as expected
118148
func NewBoolExprForScope(conditions []string, functions map[string]ottl.Factory[ottlscope.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings) (*ottl.ConditionSequence[ottlscope.TransformContext], error) {
119-
parser, err := ottlscope.NewParser(functions, set)
149+
return NewBoolExprForScopeWithOptions(conditions, functions, errorMode, set, nil)
150+
}
151+
152+
// NewBoolExprForScopeWithOptions is like NewBoolExprForScope, but with additional options.
153+
func NewBoolExprForScopeWithOptions(conditions []string, functions map[string]ottl.Factory[ottlscope.TransformContext], errorMode ottl.ErrorMode, set component.TelemetrySettings, parserOptions []ottlscope.Option) (*ottl.ConditionSequence[ottlscope.TransformContext], error) {
154+
parser, err := ottlscope.NewParser(functions, set, parserOptions...)
120155
if err != nil {
121156
return nil, err
122157
}

internal/filter/filterottl/filter_test.go

+77
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ func Test_NewBoolExprForSpan(t *testing.T) {
6262
}
6363
}
6464

65+
func Test_NewBoolExprForSpanWithOptions(t *testing.T) {
66+
_, err := NewBoolExprForSpanWithOptions(
67+
[]string{`span.name == "foo"`},
68+
StandardSpanFuncs(),
69+
ottl.PropagateError,
70+
componenttest.NewNopTelemetrySettings(),
71+
[]ottlspan.Option{ottlspan.EnablePathContextNames()},
72+
)
73+
assert.NoError(t, err)
74+
}
75+
6576
func Test_NewBoolExprForSpanEvent(t *testing.T) {
6677
tests := []struct {
6778
name string
@@ -104,6 +115,17 @@ func Test_NewBoolExprForSpanEvent(t *testing.T) {
104115
}
105116
}
106117

118+
func Test_NewBoolExprForSpanEventWithOptions(t *testing.T) {
119+
_, err := NewBoolExprForSpanEventWithOptions(
120+
[]string{`spanevent.name == "foo"`},
121+
StandardSpanEventFuncs(),
122+
ottl.PropagateError,
123+
componenttest.NewNopTelemetrySettings(),
124+
[]ottlspanevent.Option{ottlspanevent.EnablePathContextNames()},
125+
)
126+
assert.NoError(t, err)
127+
}
128+
107129
func Test_NewBoolExprForMetric(t *testing.T) {
108130
tests := []struct {
109131
name string
@@ -146,6 +168,17 @@ func Test_NewBoolExprForMetric(t *testing.T) {
146168
}
147169
}
148170

171+
func Test_NewBoolExprForMetricWithOptions(t *testing.T) {
172+
_, err := NewBoolExprForMetricWithOptions(
173+
[]string{`metric.name == "foo"`},
174+
StandardMetricFuncs(),
175+
ottl.PropagateError,
176+
componenttest.NewNopTelemetrySettings(),
177+
[]ottlmetric.Option{ottlmetric.EnablePathContextNames()},
178+
)
179+
assert.NoError(t, err)
180+
}
181+
149182
func Test_NewBoolExprForDataPoint(t *testing.T) {
150183
tests := []struct {
151184
name string
@@ -188,6 +221,17 @@ func Test_NewBoolExprForDataPoint(t *testing.T) {
188221
}
189222
}
190223

224+
func Test_NewBoolExprForDataPointWithOptions(t *testing.T) {
225+
_, err := NewBoolExprForDataPointWithOptions(
226+
[]string{"datapoint.count > 0"},
227+
StandardDataPointFuncs(),
228+
ottl.PropagateError,
229+
componenttest.NewNopTelemetrySettings(),
230+
[]ottldatapoint.Option{ottldatapoint.EnablePathContextNames()},
231+
)
232+
assert.NoError(t, err)
233+
}
234+
191235
func Test_NewBoolExprForLog(t *testing.T) {
192236
tests := []struct {
193237
name string
@@ -230,6 +274,17 @@ func Test_NewBoolExprForLog(t *testing.T) {
230274
}
231275
}
232276

277+
func Test_NewBoolExprForLogWithOptions(t *testing.T) {
278+
_, err := NewBoolExprForLogWithOptions(
279+
[]string{`log.body != ""`},
280+
StandardLogFuncs(),
281+
ottl.PropagateError,
282+
componenttest.NewNopTelemetrySettings(),
283+
[]ottllog.Option{ottllog.EnablePathContextNames()},
284+
)
285+
assert.NoError(t, err)
286+
}
287+
233288
func Test_NewBoolExprForResource(t *testing.T) {
234289
tests := []struct {
235290
name string
@@ -272,6 +327,17 @@ func Test_NewBoolExprForResource(t *testing.T) {
272327
}
273328
}
274329

330+
func Test_NewBoolExprForResourceWithOptions(t *testing.T) {
331+
_, err := NewBoolExprForResourceWithOptions(
332+
[]string{`resource.dropped_attributes_count == 0`},
333+
StandardResourceFuncs(),
334+
ottl.PropagateError,
335+
componenttest.NewNopTelemetrySettings(),
336+
[]ottlresource.Option{ottlresource.EnablePathContextNames()},
337+
)
338+
assert.NoError(t, err)
339+
}
340+
275341
func Test_NewBoolExprForScope(t *testing.T) {
276342
tests := []struct {
277343
name string
@@ -321,3 +387,14 @@ func Test_NewBoolExprForScope(t *testing.T) {
321387
})
322388
}
323389
}
390+
391+
func Test_NewBoolExprForScopeWithOptions(t *testing.T) {
392+
_, err := NewBoolExprForScopeWithOptions(
393+
[]string{`scope.name != ""`},
394+
StandardScopeFuncs(),
395+
ottl.PropagateError,
396+
componenttest.NewNopTelemetrySettings(),
397+
[]ottlscope.Option{ottlscope.EnablePathContextNames()},
398+
)
399+
assert.NoError(t, err)
400+
}

0 commit comments

Comments
 (0)