diff --git a/pkg/ottl/contexts/ottldatapoints/datapoints.go b/pkg/ottl/contexts/ottldatapoints/datapoints.go index 692e20857e1a..88ab0dc9cd68 100644 --- a/pkg/ottl/contexts/ottldatapoints/datapoints.go +++ b/pkg/ottl/contexts/ottldatapoints/datapoints.go @@ -19,6 +19,7 @@ import ( "fmt" "time" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pmetric" @@ -67,6 +68,10 @@ func (ctx TransformContext) GetMetrics() pmetric.MetricSlice { return ctx.metrics } +func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings) ottl.Parser[TransformContext] { + return ottl.NewParser[TransformContext](functions, parsePath, parseEnum, telemetrySettings) +} + var symbolTable = map[ottl.EnumSymbol]ottl.Enum{ "AGGREGATION_TEMPORALITY_UNSPECIFIED": ottl.Enum(pmetric.MetricAggregationTemporalityUnspecified), "AGGREGATION_TEMPORALITY_DELTA": ottl.Enum(pmetric.MetricAggregationTemporalityDelta), @@ -81,7 +86,7 @@ var symbolTable = map[ottl.EnumSymbol]ottl.Enum{ "METRIC_DATA_TYPE_SUMMARY": ottl.Enum(pmetric.MetricTypeSummary), } -func ParseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) { +func parseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) { if val != nil { if enum, ok := symbolTable[*val]; ok { return &enum, nil @@ -91,7 +96,7 @@ func ParseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) { return nil, fmt.Errorf("enum symbol not provided") } -func ParsePath(val *ottl.Path) (ottl.GetSetter[TransformContext], error) { +func parsePath(val *ottl.Path) (ottl.GetSetter[TransformContext], error) { if val != nil && len(val.Fields) > 0 { return newPathGetSetter(val.Fields) } diff --git a/pkg/ottl/contexts/ottldatapoints/datapoints_test.go b/pkg/ottl/contexts/ottldatapoints/datapoints_test.go index 6a9bc5ab3be5..6095234ba96a 100644 --- a/pkg/ottl/contexts/ottldatapoints/datapoints_test.go +++ b/pkg/ottl/contexts/ottldatapoints/datapoints_test.go @@ -1622,7 +1622,7 @@ func Test_ParseEnum(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - actual, err := ParseEnum((*ottl.EnumSymbol)(ottltest.Strp(tt.name))) + actual, err := parseEnum((*ottl.EnumSymbol)(ottltest.Strp(tt.name))) assert.NoError(t, err) assert.Equal(t, *actual, tt.want) }) @@ -1645,7 +1645,7 @@ func Test_ParseEnum_False(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - actual, err := ParseEnum(tt.enumSymbol) + actual, err := parseEnum(tt.enumSymbol) assert.Error(t, err) assert.Nil(t, actual) }) diff --git a/pkg/ottl/contexts/ottllogs/logs.go b/pkg/ottl/contexts/ottllogs/logs.go index a7af924093ba..64259e2bfa01 100644 --- a/pkg/ottl/contexts/ottllogs/logs.go +++ b/pkg/ottl/contexts/ottllogs/logs.go @@ -20,6 +20,7 @@ import ( "fmt" "time" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/plog" @@ -56,6 +57,10 @@ func (ctx TransformContext) GetResource() pcommon.Resource { return ctx.resource } +func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings) ottl.Parser[TransformContext] { + return ottl.NewParser[TransformContext](functions, parsePath, parseEnum, telemetrySettings) +} + var symbolTable = map[ottl.EnumSymbol]ottl.Enum{ "SEVERITY_NUMBER_UNSPECIFIED": ottl.Enum(plog.SeverityNumberUndefined), "SEVERITY_NUMBER_TRACE": ottl.Enum(plog.SeverityNumberTrace), @@ -84,7 +89,7 @@ var symbolTable = map[ottl.EnumSymbol]ottl.Enum{ "SEVERITY_NUMBER_FATAL4": ottl.Enum(plog.SeverityNumberFatal4), } -func ParseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) { +func parseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) { if val != nil { if enum, ok := symbolTable[*val]; ok { return &enum, nil @@ -94,7 +99,7 @@ func ParseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) { return nil, fmt.Errorf("enum symbol not provided") } -func ParsePath(val *ottl.Path) (ottl.GetSetter[TransformContext], error) { +func parsePath(val *ottl.Path) (ottl.GetSetter[TransformContext], error) { if val != nil && len(val.Fields) > 0 { return newPathGetSetter(val.Fields) } diff --git a/pkg/ottl/contexts/ottllogs/logs_test.go b/pkg/ottl/contexts/ottllogs/logs_test.go index 4fea3d4c7870..c5dbe2331a06 100644 --- a/pkg/ottl/contexts/ottllogs/logs_test.go +++ b/pkg/ottl/contexts/ottllogs/logs_test.go @@ -572,7 +572,7 @@ func Test_ParseEnum(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - actual, err := ParseEnum((*ottl.EnumSymbol)(ottltest.Strp(tt.name))) + actual, err := parseEnum((*ottl.EnumSymbol)(ottltest.Strp(tt.name))) assert.NoError(t, err) assert.Equal(t, *actual, tt.want) }) @@ -595,7 +595,7 @@ func Test_ParseEnum_False(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - actual, err := ParseEnum(tt.enumSymbol) + actual, err := parseEnum(tt.enumSymbol) assert.Error(t, err) assert.Nil(t, actual) }) diff --git a/pkg/ottl/contexts/ottltraces/traces.go b/pkg/ottl/contexts/ottltraces/traces.go index 67dff7b824fc..4a5da5ef8891 100644 --- a/pkg/ottl/contexts/ottltraces/traces.go +++ b/pkg/ottl/contexts/ottltraces/traces.go @@ -20,6 +20,7 @@ import ( "fmt" "time" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/ptrace" "go.opentelemetry.io/otel/trace" @@ -57,6 +58,10 @@ func (ctx TransformContext) GetResource() pcommon.Resource { return ctx.resource } +func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings) ottl.Parser[TransformContext] { + return ottl.NewParser[TransformContext](functions, parsePath, parseEnum, telemetrySettings) +} + var symbolTable = map[ottl.EnumSymbol]ottl.Enum{ "SPAN_KIND_UNSPECIFIED": ottl.Enum(ptrace.SpanKindUnspecified), "SPAN_KIND_INTERNAL": ottl.Enum(ptrace.SpanKindInternal), @@ -69,7 +74,7 @@ var symbolTable = map[ottl.EnumSymbol]ottl.Enum{ "STATUS_CODE_ERROR": ottl.Enum(ptrace.StatusCodeError), } -func ParseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) { +func parseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) { if val != nil { if enum, ok := symbolTable[*val]; ok { return &enum, nil @@ -79,7 +84,7 @@ func ParseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) { return nil, fmt.Errorf("enum symbol not provided") } -func ParsePath(val *ottl.Path) (ottl.GetSetter[TransformContext], error) { +func parsePath(val *ottl.Path) (ottl.GetSetter[TransformContext], error) { if val != nil && len(val.Fields) > 0 { return newPathGetSetter(val.Fields) } diff --git a/pkg/ottl/contexts/ottltraces/traces_test.go b/pkg/ottl/contexts/ottltraces/traces_test.go index 0f8d80eb79e3..2857fa629c34 100644 --- a/pkg/ottl/contexts/ottltraces/traces_test.go +++ b/pkg/ottl/contexts/ottltraces/traces_test.go @@ -641,7 +641,7 @@ func Test_ParseEnum(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - actual, err := ParseEnum((*ottl.EnumSymbol)(ottltest.Strp(tt.name))) + actual, err := parseEnum((*ottl.EnumSymbol)(ottltest.Strp(tt.name))) assert.NoError(t, err) assert.Equal(t, *actual, tt.want) }) @@ -664,7 +664,7 @@ func Test_ParseEnum_False(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - actual, err := ParseEnum(tt.enumSymbol) + actual, err := parseEnum(tt.enumSymbol) assert.Error(t, err) assert.Nil(t, actual) }) diff --git a/processor/routingprocessor/logs.go b/processor/routingprocessor/logs.go index c047af7117aa..bc4fda181fc3 100644 --- a/processor/routingprocessor/logs.go +++ b/processor/routingprocessor/logs.go @@ -25,7 +25,6 @@ import ( "go.uber.org/multierr" "go.uber.org/zap" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllogs" "github.com/open-telemetry/opentelemetry-collector-contrib/processor/routingprocessor/internal/common" ) @@ -50,12 +49,7 @@ func newLogProcessor(settings component.TelemetrySettings, config config.Process cfg.Table, cfg.DefaultExporters, settings, - ottl.NewParser[ottllogs.TransformContext]( - common.Functions[ottllogs.TransformContext](), - ottllogs.ParsePath, - ottllogs.ParseEnum, - settings, - ), + ottllogs.NewParser(common.Functions[ottllogs.TransformContext](), settings), ), extractor: newExtractor(cfg.FromAttribute, settings.Logger), } diff --git a/processor/routingprocessor/metrics.go b/processor/routingprocessor/metrics.go index 9e5c62b32790..5d730ad296b2 100644 --- a/processor/routingprocessor/metrics.go +++ b/processor/routingprocessor/metrics.go @@ -25,7 +25,6 @@ import ( "go.uber.org/multierr" "go.uber.org/zap" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoints" "github.com/open-telemetry/opentelemetry-collector-contrib/processor/routingprocessor/internal/common" ) @@ -50,12 +49,7 @@ func newMetricProcessor(settings component.TelemetrySettings, config config.Proc cfg.Table, cfg.DefaultExporters, settings, - ottl.NewParser[ottldatapoints.TransformContext]( - common.Functions[ottldatapoints.TransformContext](), - ottldatapoints.ParsePath, - ottldatapoints.ParseEnum, - settings, - ), + ottldatapoints.NewParser(common.Functions[ottldatapoints.TransformContext](), settings), ), extractor: newExtractor(cfg.FromAttribute, settings.Logger), } diff --git a/processor/routingprocessor/traces.go b/processor/routingprocessor/traces.go index 3ea1a152e816..84269dcfc550 100644 --- a/processor/routingprocessor/traces.go +++ b/processor/routingprocessor/traces.go @@ -25,7 +25,6 @@ import ( "go.uber.org/multierr" "go.uber.org/zap" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottltraces" "github.com/open-telemetry/opentelemetry-collector-contrib/processor/routingprocessor/internal/common" ) @@ -50,12 +49,7 @@ func newTracesProcessor(settings component.TelemetrySettings, config config.Proc cfg.Table, cfg.DefaultExporters, settings, - ottl.NewParser[ottltraces.TransformContext]( - common.Functions[ottltraces.TransformContext](), - ottltraces.ParsePath, - ottltraces.ParseEnum, - settings, - ), + ottltraces.NewParser(common.Functions[ottltraces.TransformContext](), settings), ), extractor: newExtractor(cfg.FromAttribute, settings.Logger), } diff --git a/processor/transformprocessor/config.go b/processor/transformprocessor/config.go index 6b9cc1db7fbc..7f2d76d90a06 100644 --- a/processor/transformprocessor/config.go +++ b/processor/transformprocessor/config.go @@ -20,7 +20,6 @@ import ( "go.uber.org/multierr" "go.uber.org/zap" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoints" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllogs" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottltraces" @@ -41,34 +40,19 @@ var _ config.Processor = (*Config)(nil) func (c *Config) Validate() error { var errors error - ottltracesp := ottl.NewParser[ottltraces.TransformContext]( - traces.Functions(), - ottltraces.ParsePath, - ottltraces.ParseEnum, - component.TelemetrySettings{Logger: zap.NewNop()}, - ) + ottltracesp := ottltraces.NewParser(traces.Functions(), component.TelemetrySettings{Logger: zap.NewNop()}) _, err := ottltracesp.ParseStatements(c.Traces.Queries) if err != nil { errors = multierr.Append(errors, err) } - ottlmetricsp := ottl.NewParser[ottldatapoints.TransformContext]( - metrics.Functions(), - ottldatapoints.ParsePath, - ottldatapoints.ParseEnum, - component.TelemetrySettings{Logger: zap.NewNop()}, - ) + ottlmetricsp := ottldatapoints.NewParser(metrics.Functions(), component.TelemetrySettings{Logger: zap.NewNop()}) _, err = ottlmetricsp.ParseStatements(c.Metrics.Queries) if err != nil { errors = multierr.Append(errors, err) } - ottllogsp := ottl.NewParser[ottllogs.TransformContext]( - logs.Functions(), - ottllogs.ParsePath, - ottllogs.ParseEnum, - component.TelemetrySettings{Logger: zap.NewNop()}, - ) + ottllogsp := ottllogs.NewParser(logs.Functions(), component.TelemetrySettings{Logger: zap.NewNop()}) _, err = ottllogsp.ParseStatements(c.Logs.Queries) if err != nil { errors = multierr.Append(errors, err) diff --git a/processor/transformprocessor/internal/logs/processor.go b/processor/transformprocessor/internal/logs/processor.go index 2e203fb4f0b3..32f075a421b2 100644 --- a/processor/transformprocessor/internal/logs/processor.go +++ b/processor/transformprocessor/internal/logs/processor.go @@ -19,7 +19,6 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/pdata/plog" - "go.uber.org/zap" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllogs" @@ -27,23 +26,16 @@ import ( type Processor struct { queries []ottl.Statement[ottllogs.TransformContext] - logger *zap.Logger } func NewProcessor(statements []string, functions map[string]interface{}, settings component.TelemetrySettings) (*Processor, error) { - ottlp := ottl.NewParser( - functions, - ottllogs.ParsePath, - ottllogs.ParseEnum, - settings, - ) + ottlp := ottllogs.NewParser(functions, settings) queries, err := ottlp.ParseStatements(statements) if err != nil { return nil, err } return &Processor{ queries: queries, - logger: settings.Logger, }, nil } diff --git a/processor/transformprocessor/internal/metrics/processor.go b/processor/transformprocessor/internal/metrics/processor.go index 21e5aefb99af..5d558b0817c0 100644 --- a/processor/transformprocessor/internal/metrics/processor.go +++ b/processor/transformprocessor/internal/metrics/processor.go @@ -20,7 +20,6 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pmetric" - "go.uber.org/zap" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoints" @@ -28,23 +27,16 @@ import ( type Processor struct { queries []ottl.Statement[ottldatapoints.TransformContext] - logger *zap.Logger } func NewProcessor(statements []string, functions map[string]interface{}, settings component.TelemetrySettings) (*Processor, error) { - ottlp := ottl.NewParser( - functions, - ottldatapoints.ParsePath, - ottldatapoints.ParseEnum, - settings, - ) + ottlp := ottldatapoints.NewParser(functions, settings) queries, err := ottlp.ParseStatements(statements) if err != nil { return nil, err } return &Processor{ queries: queries, - logger: settings.Logger, }, nil } diff --git a/processor/transformprocessor/internal/traces/processor.go b/processor/transformprocessor/internal/traces/processor.go index 1c826df93967..b97523d09f67 100644 --- a/processor/transformprocessor/internal/traces/processor.go +++ b/processor/transformprocessor/internal/traces/processor.go @@ -19,7 +19,6 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/pdata/ptrace" - "go.uber.org/zap" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottltraces" @@ -27,23 +26,16 @@ import ( type Processor struct { queries []ottl.Statement[ottltraces.TransformContext] - logger *zap.Logger } func NewProcessor(statements []string, functions map[string]interface{}, settings component.TelemetrySettings) (*Processor, error) { - ottlp := ottl.NewParser( - functions, - ottltraces.ParsePath, - ottltraces.ParseEnum, - settings, - ) + ottlp := ottltraces.NewParser(functions, settings) queries, err := ottlp.ParseStatements(statements) if err != nil { return nil, err } return &Processor{ queries: queries, - logger: settings.Logger, }, nil } diff --git a/unreleased/ottl-parser.yaml b/unreleased/ottl-parser.yaml new file mode 100644 index 000000000000..96679a5ee787 --- /dev/null +++ b/unreleased/ottl-parser.yaml @@ -0,0 +1,11 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: pkg/ottl + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Simplify parser creation logic. + +# One or more tracking issues related to the change +issues: [14601]