Skip to content

Commit 8faa947

Browse files
authored
[pkg/ottl] Add StandardFuncs and StandardConverters to ottlfuncs (#23190)
* Add StandardFuncs and StandardConverters to ottlfuncs * changelog * add license * make goporto * update changelog
1 parent 47b208f commit 8faa947

File tree

10 files changed

+94
-58
lines changed

10 files changed

+94
-58
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use this changelog template to create an entry for release notes.
2+
# If your change doesn't affect end users, such as a test fix or a tooling change,
3+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
4+
5+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
6+
change_type: enhancement
7+
8+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
9+
component: pkg/ottl
10+
11+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
12+
note: Adds `StandardFuncs` and `StandardConverters` to facilitate function map generation.
13+
14+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
15+
issues: [23190]
16+
17+
# (Optional) One or more lines of additional information to render under the primary note.
18+
# These lines will be padded with 2 spaces and then inserted directly into the document.
19+
# Use pipe (|) for multiline entries.
20+
subtext: This change means that new functions added to ottlfuncs get automatically added to Cotnrib components that use OTTL

internal/filter/filterottl/filter.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,10 @@ func StandardLogFuncs() map[string]ottl.Factory[ottllog.TransformContext] {
152152
}
153153

154154
func standardFuncs[K any]() map[string]ottl.Factory[K] {
155-
return ottl.CreateFactoryMap(
156-
ottlfuncs.NewTraceIDFactory[K](),
157-
ottlfuncs.NewSpanIDFactory[K](),
158-
ottlfuncs.NewIsMatchFactory[K](),
159-
ottlfuncs.NewConcatFactory[K](),
160-
ottlfuncs.NewSplitFactory[K](),
161-
ottlfuncs.NewIntFactory[K](),
162-
ottlfuncs.NewConvertCaseFactory[K](),
163-
ottlfuncs.NewSubstringFactory[K](),
164-
ottlfuncs.NewLogFactory[K](),
165-
ottlfuncs.NewUUIDFactory[K](),
166-
ottlfuncs.NewParseJSONFactory[K](),
167-
newDropFactory[K](),
168-
)
155+
m := ottlfuncs.StandardConverters[K]()
156+
f := newDropFactory[K]()
157+
m[f.Name()] = f
158+
return m
169159
}
170160

171161
func newDropFactory[K any]() ottl.Factory[K] {

pkg/ottl/ottlfuncs/functions.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
5+
6+
import (
7+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
8+
)
9+
10+
func StandardFuncs[K any]() map[string]ottl.Factory[K] {
11+
f := []ottl.Factory[K]{
12+
// Editors
13+
NewDeleteKeyFactory[K](),
14+
NewDeleteMatchingKeysFactory[K](),
15+
NewKeepKeysFactory[K](),
16+
NewLimitFactory[K](),
17+
NewMergeMapsFactory[K](),
18+
NewReplaceAllMatchesFactory[K](),
19+
NewReplaceAllPatternsFactory[K](),
20+
NewReplaceMatchFactory[K](),
21+
NewReplacePatternFactory[K](),
22+
NewSetFactory[K](),
23+
NewTruncateAllFactory[K](),
24+
}
25+
f = append(f, converters[K]()...)
26+
27+
return ottl.CreateFactoryMap(f...)
28+
}
29+
30+
func StandardConverters[K any]() map[string]ottl.Factory[K] {
31+
return ottl.CreateFactoryMap(converters[K]()...)
32+
}
33+
34+
func converters[K any]() []ottl.Factory[K] {
35+
return []ottl.Factory[K]{
36+
// Converters
37+
NewConcatFactory[K](),
38+
NewConvertCaseFactory[K](),
39+
NewFnvFactory[K](),
40+
NewIntFactory[K](),
41+
NewIsMatchFactory[K](),
42+
NewLogFactory[K](),
43+
NewParseJSONFactory[K](),
44+
NewSHA1Factory[K](),
45+
NewSHA256Factory[K](),
46+
NewSpanIDFactory[K](),
47+
NewSplitFactory[K](),
48+
NewSubstringFactory[K](),
49+
NewTraceIDFactory[K](),
50+
NewUUIDFactory[K](),
51+
}
52+
}

processor/transformprocessor/internal/common/functions.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,10 @@ import (
1010
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
1111
)
1212

13-
func Functions[K any]() map[string]ottl.Factory[K] {
14-
return ottl.CreateFactoryMap(
15-
ottlfuncs.NewTraceIDFactory[K](),
16-
ottlfuncs.NewSpanIDFactory[K](),
17-
ottlfuncs.NewIsMatchFactory[K](),
18-
ottlfuncs.NewConcatFactory[K](),
19-
ottlfuncs.NewSplitFactory[K](),
20-
ottlfuncs.NewIntFactory[K](),
21-
ottlfuncs.NewConvertCaseFactory[K](),
22-
ottlfuncs.NewParseJSONFactory[K](),
23-
ottlfuncs.NewSubstringFactory[K](),
24-
ottlfuncs.NewKeepKeysFactory[K](),
25-
ottlfuncs.NewSetFactory[K](),
26-
ottlfuncs.NewTruncateAllFactory[K](),
27-
ottlfuncs.NewLimitFactory[K](),
28-
ottlfuncs.NewReplaceMatchFactory[K](),
29-
ottlfuncs.NewReplaceAllMatchesFactory[K](),
30-
ottlfuncs.NewReplacePatternFactory[K](),
31-
ottlfuncs.NewReplaceAllPatternsFactory[K](),
32-
ottlfuncs.NewDeleteKeyFactory[K](),
33-
ottlfuncs.NewDeleteMatchingKeysFactory[K](),
34-
ottlfuncs.NewMergeMapsFactory[K](),
35-
ottlfuncs.NewLogFactory[K](),
36-
)
37-
}
38-
3913
func ResourceFunctions() map[string]ottl.Factory[ottlresource.TransformContext] {
40-
return Functions[ottlresource.TransformContext]()
14+
return ottlfuncs.StandardFuncs[ottlresource.TransformContext]()
4115
}
4216

4317
func ScopeFunctions() map[string]ottl.Factory[ottlscope.TransformContext] {
44-
return Functions[ottlscope.TransformContext]()
18+
return ottlfuncs.StandardFuncs[ottlscope.TransformContext]()
4519
}

processor/transformprocessor/internal/logs/functions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ package logs // import "github.com/open-telemetry/opentelemetry-collector-contri
66
import (
77
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
88
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllog"
9-
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common"
9+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
1010
)
1111

1212
func LogFunctions() map[string]ottl.Factory[ottllog.TransformContext] {
1313
// No logs-only functions yet.
14-
return common.Functions[ottllog.TransformContext]()
14+
return ottlfuncs.StandardFuncs[ottllog.TransformContext]()
1515
}

processor/transformprocessor/internal/logs/functions_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"github.com/stretchr/testify/require"
1111

1212
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllog"
13-
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common"
13+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
1414
)
1515

1616
func Test_LogFunctions(t *testing.T) {
17-
expected := common.Functions[ottllog.TransformContext]()
17+
expected := ottlfuncs.StandardFuncs[ottllog.TransformContext]()
1818
actual := LogFunctions()
1919
require.Equal(t, len(expected), len(actual))
2020
for k := range actual {

processor/transformprocessor/internal/metrics/functions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
88
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoint"
99
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
10-
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common"
10+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
1111
)
1212

1313
func DataPointFunctions() map[string]ottl.Factory[ottldatapoint.TransformContext] {
14-
functions := common.Functions[ottldatapoint.TransformContext]()
14+
functions := ottlfuncs.StandardFuncs[ottldatapoint.TransformContext]()
1515

1616
datapointFunctions := ottl.CreateFactoryMap[ottldatapoint.TransformContext](
1717
newConvertSumToGaugeFactory(),
@@ -28,5 +28,5 @@ func DataPointFunctions() map[string]ottl.Factory[ottldatapoint.TransformContext
2828
}
2929

3030
func MetricFunctions() map[string]ottl.Factory[ottlmetric.TransformContext] {
31-
return common.Functions[ottlmetric.TransformContext]()
31+
return ottlfuncs.StandardFuncs[ottlmetric.TransformContext]()
3232
}

processor/transformprocessor/internal/metrics/functions_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111

1212
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoint"
1313
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
14-
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common"
14+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
1515
)
1616

1717
func Test_DataPointFunctions(t *testing.T) {
18-
expected := common.Functions[ottldatapoint.TransformContext]()
18+
expected := ottlfuncs.StandardFuncs[ottldatapoint.TransformContext]()
1919
expected["convert_sum_to_gauge"] = newConvertSumToGaugeFactory()
2020
expected["convert_gauge_to_sum"] = newConvertGaugeToSumFactory()
2121
expected["convert_summary_sum_val_to_sum"] = newConvertSummarySumValToSumFactory()
@@ -30,7 +30,7 @@ func Test_DataPointFunctions(t *testing.T) {
3030
}
3131

3232
func Test_MetricFunctions(t *testing.T) {
33-
expected := common.Functions[ottlmetric.TransformContext]()
33+
expected := ottlfuncs.StandardFuncs[ottlmetric.TransformContext]()
3434
actual := MetricFunctions()
3535
require.Equal(t, len(expected), len(actual))
3636
for k := range actual {

processor/transformprocessor/internal/traces/functions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
88
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlspan"
99
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlspanevent"
10-
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common"
10+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
1111
)
1212

1313
func SpanFunctions() map[string]ottl.Factory[ottlspan.TransformContext] {
1414
// No trace-only functions yet.
15-
return common.Functions[ottlspan.TransformContext]()
15+
return ottlfuncs.StandardFuncs[ottlspan.TransformContext]()
1616
}
1717

1818
func SpanEventFunctions() map[string]ottl.Factory[ottlspanevent.TransformContext] {
1919
// No trace-only functions yet.
20-
return common.Functions[ottlspanevent.TransformContext]()
20+
return ottlfuncs.StandardFuncs[ottlspanevent.TransformContext]()
2121
}

processor/transformprocessor/internal/traces/functions_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111

1212
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlspan"
1313
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlspanevent"
14-
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common"
14+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
1515
)
1616

1717
func Test_SpanFunctions(t *testing.T) {
18-
expected := common.Functions[ottlspan.TransformContext]()
18+
expected := ottlfuncs.StandardFuncs[ottlspan.TransformContext]()
1919
actual := SpanFunctions()
2020
require.Equal(t, len(expected), len(actual))
2121
for k := range actual {
@@ -24,7 +24,7 @@ func Test_SpanFunctions(t *testing.T) {
2424
}
2525

2626
func Test_SpanEventFunctions(t *testing.T) {
27-
expected := common.Functions[ottlspanevent.TransformContext]()
27+
expected := ottlfuncs.StandardFuncs[ottlspanevent.TransformContext]()
2828
actual := SpanEventFunctions()
2929
require.Equal(t, len(expected), len(actual))
3030
for k := range actual {

0 commit comments

Comments
 (0)