Skip to content

Commit 93caa93

Browse files
franciscovalentecastromwearevan-bradley
authored
[processor/filter] Create With*Functions factory options to register custom OTTL funcs programatically. (#40933)
Co-authored-by: Matthew Wear <[email protected]> Co-authored-by: Evan Bradley <[email protected]>
1 parent 1931839 commit 93caa93

File tree

13 files changed

+772
-35
lines changed

13 files changed

+772
-35
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: filterprocessor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Create `With*Functions` factory options to provide custom OTTL functions for logs, metrics or traces to the resulting filter processor.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [40948]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [api]

processor/filterprocessor/config.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import (
1818
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
1919
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset/regexp"
2020
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
21+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoint"
22+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllog"
23+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
24+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlspan"
25+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlspanevent"
2126
)
2227

2328
// Config defines configuration for Resource processor.
@@ -36,6 +41,12 @@ type Config struct {
3641
Spans filterconfig.MatchConfig `mapstructure:"spans"`
3742

3843
Traces TraceFilters `mapstructure:"traces"`
44+
45+
dataPointFunctions map[string]ottl.Factory[ottldatapoint.TransformContext]
46+
logFunctions map[string]ottl.Factory[ottllog.TransformContext]
47+
metricFunctions map[string]ottl.Factory[ottlmetric.TransformContext]
48+
spanEventFunctions map[string]ottl.Factory[ottlspanevent.TransformContext]
49+
spanFunctions map[string]ottl.Factory[ottlspan.TransformContext]
3950
}
4051

4152
// MetricFilters filters by Metric properties.
@@ -279,27 +290,27 @@ func (cfg *Config) Validate() error {
279290
var errors error
280291

281292
if cfg.Traces.SpanConditions != nil {
282-
_, err := filterottl.NewBoolExprForSpan(cfg.Traces.SpanConditions, filterottl.StandardSpanFuncs(), ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()})
293+
_, err := filterottl.NewBoolExprForSpan(cfg.Traces.SpanConditions, cfg.spanFunctions, ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()})
283294
errors = multierr.Append(errors, err)
284295
}
285296

286297
if cfg.Traces.SpanEventConditions != nil {
287-
_, err := filterottl.NewBoolExprForSpanEvent(cfg.Traces.SpanEventConditions, filterottl.StandardSpanEventFuncs(), ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()})
298+
_, err := filterottl.NewBoolExprForSpanEvent(cfg.Traces.SpanEventConditions, cfg.spanEventFunctions, ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()})
288299
errors = multierr.Append(errors, err)
289300
}
290301

291302
if cfg.Metrics.MetricConditions != nil {
292-
_, err := filterottl.NewBoolExprForMetric(cfg.Metrics.MetricConditions, filterottl.StandardMetricFuncs(), ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()})
303+
_, err := filterottl.NewBoolExprForMetric(cfg.Metrics.MetricConditions, cfg.metricFunctions, ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()})
293304
errors = multierr.Append(errors, err)
294305
}
295306

296307
if cfg.Metrics.DataPointConditions != nil {
297-
_, err := filterottl.NewBoolExprForDataPoint(cfg.Metrics.DataPointConditions, filterottl.StandardDataPointFuncs(), ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()})
308+
_, err := filterottl.NewBoolExprForDataPoint(cfg.Metrics.DataPointConditions, cfg.dataPointFunctions, ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()})
298309
errors = multierr.Append(errors, err)
299310
}
300311

301312
if cfg.Logs.LogConditions != nil {
302-
_, err := filterottl.NewBoolExprForLog(cfg.Logs.LogConditions, filterottl.StandardLogFuncs(), ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()})
313+
_, err := filterottl.NewBoolExprForLog(cfg.Logs.LogConditions, cfg.logFunctions, ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()})
303314
errors = multierr.Append(errors, err)
304315
}
305316

processor/filterprocessor/config_test.go

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ import (
2121
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/filterprocessor/internal/metadata"
2222
)
2323

24+
func assertConfigContainsDefaultFunctions(t *testing.T, config Config) {
25+
t.Helper()
26+
for _, f := range DefaultLogFunctions() {
27+
assert.Contains(t, config.logFunctions, f.Name(), "missing log function %v", f.Name())
28+
}
29+
for _, f := range DefaultDataPointFunctions() {
30+
assert.Contains(t, config.dataPointFunctions, f.Name(), "missing data point function %v", f.Name())
31+
}
32+
for _, f := range DefaultMetricFunctions() {
33+
assert.Contains(t, config.metricFunctions, f.Name(), "missing metric function %v", f.Name())
34+
}
35+
for _, f := range DefaultSpanFunctions() {
36+
assert.Contains(t, config.spanFunctions, f.Name(), "missing span function %v", f.Name())
37+
}
38+
for _, f := range DefaultSpanEventFunctions() {
39+
assert.Contains(t, config.spanEventFunctions, f.Name(), "missing span event function %v", f.Name())
40+
}
41+
}
42+
2443
// TestLoadingConfigRegexp tests loading testdata/config_strict.yaml
2544
func TestLoadingConfigStrict(t *testing.T) {
2645
// list of filters used repeatedly on testdata/config_strict.yaml
@@ -91,7 +110,8 @@ func TestLoadingConfigStrict(t *testing.T) {
91110
require.NoError(t, sub.Unmarshal(cfg))
92111

93112
assert.NoError(t, xconfmap.Validate(cfg))
94-
assert.Equal(t, tt.expected, cfg)
113+
assert.EqualExportedValues(t, tt.expected, cfg)
114+
assertConfigContainsDefaultFunctions(t, *cfg.(*Config))
95115
})
96116
}
97117
}
@@ -173,7 +193,8 @@ func TestLoadingConfigStrictLogs(t *testing.T) {
173193
require.NoError(t, sub.Unmarshal(cfg))
174194

175195
assert.NoError(t, xconfmap.Validate(cfg))
176-
assert.Equal(t, tt.expected, cfg)
196+
assert.EqualExportedValues(t, tt.expected, cfg)
197+
assertConfigContainsDefaultFunctions(t, *cfg.(*Config))
177198
})
178199
}
179200
}
@@ -235,7 +256,8 @@ func TestLoadingConfigSeverityLogsStrict(t *testing.T) {
235256
require.NoError(t, sub.Unmarshal(cfg))
236257

237258
assert.NoError(t, xconfmap.Validate(cfg))
238-
assert.Equal(t, tt.expected, cfg)
259+
assert.EqualExportedValues(t, tt.expected, cfg)
260+
assertConfigContainsDefaultFunctions(t, *cfg.(*Config))
239261
})
240262
}
241263
}
@@ -297,7 +319,8 @@ func TestLoadingConfigSeverityLogsRegexp(t *testing.T) {
297319
require.NoError(t, sub.Unmarshal(cfg))
298320

299321
assert.NoError(t, xconfmap.Validate(cfg))
300-
assert.Equal(t, tt.expected, cfg)
322+
assert.EqualExportedValues(t, tt.expected, cfg)
323+
assertConfigContainsDefaultFunctions(t, *cfg.(*Config))
301324
})
302325
}
303326
}
@@ -359,7 +382,8 @@ func TestLoadingConfigBodyLogsStrict(t *testing.T) {
359382
require.NoError(t, sub.Unmarshal(cfg))
360383

361384
assert.NoError(t, xconfmap.Validate(cfg))
362-
assert.Equal(t, tt.expected, cfg)
385+
assert.EqualExportedValues(t, tt.expected, cfg)
386+
assertConfigContainsDefaultFunctions(t, *cfg.(*Config))
363387
})
364388
}
365389
}
@@ -421,7 +445,8 @@ func TestLoadingConfigBodyLogsRegexp(t *testing.T) {
421445
require.NoError(t, sub.Unmarshal(cfg))
422446

423447
assert.NoError(t, xconfmap.Validate(cfg))
424-
assert.Equal(t, tt.expected, cfg)
448+
assert.EqualExportedValues(t, tt.expected, cfg)
449+
assertConfigContainsDefaultFunctions(t, *cfg.(*Config))
425450
})
426451
}
427452
}
@@ -486,7 +511,8 @@ func TestLoadingConfigMinSeverityNumberLogs(t *testing.T) {
486511
require.NoError(t, sub.Unmarshal(cfg))
487512

488513
assert.NoError(t, xconfmap.Validate(cfg))
489-
assert.Equal(t, tt.expected, cfg)
514+
assert.EqualExportedValues(t, tt.expected, cfg)
515+
assertConfigContainsDefaultFunctions(t, *cfg.(*Config))
490516
})
491517
}
492518
}
@@ -575,7 +601,8 @@ func TestLoadingConfigRegexp(t *testing.T) {
575601
require.NoError(t, sub.Unmarshal(cfg))
576602

577603
assert.NoError(t, xconfmap.Validate(cfg))
578-
assert.Equal(t, tt.expected, cfg)
604+
assert.EqualExportedValues(t, tt.expected, cfg)
605+
assertConfigContainsDefaultFunctions(t, *cfg.(*Config))
579606
})
580607
}
581608
}
@@ -625,7 +652,8 @@ func TestLoadingSpans(t *testing.T) {
625652
require.NoError(t, sub.Unmarshal(cfg))
626653

627654
assert.NoError(t, xconfmap.Validate(cfg))
628-
assert.Equal(t, tt.expected, cfg)
655+
assert.EqualExportedValues(t, tt.expected, cfg)
656+
assertConfigContainsDefaultFunctions(t, *cfg.(*Config))
629657
})
630658
}
631659
}
@@ -710,7 +738,8 @@ func TestLoadingConfigExpr(t *testing.T) {
710738
require.NoError(t, sub.Unmarshal(cfg))
711739

712740
assert.NoError(t, xconfmap.Validate(cfg))
713-
assert.Equal(t, tt.expected, cfg)
741+
assert.EqualExportedValues(t, tt.expected, cfg)
742+
assertConfigContainsDefaultFunctions(t, *cfg.(*Config))
714743
})
715744
}
716745
}
@@ -922,7 +951,8 @@ func TestLoadingConfigOTTL(t *testing.T) {
922951
}
923952
} else {
924953
assert.NoError(t, xconfmap.Validate(cfg))
925-
assert.Equal(t, tt.expected, cfg)
954+
assert.EqualExportedValues(t, tt.expected, cfg)
955+
assertConfigContainsDefaultFunctions(t, *cfg.(*Config))
926956
}
927957
})
928958
}

0 commit comments

Comments
 (0)