Skip to content

Commit

Permalink
pkg/ottl: Simplify parser creation logic
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Sep 29, 2022
1 parent e4695ee commit 8f6530d
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 79 deletions.
9 changes: 7 additions & 2 deletions pkg/ottl/contexts/ottldatapoints/datapoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"

Expand Down Expand Up @@ -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),
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ottl/contexts/ottldatapoints/datapoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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)
})
Expand Down
9 changes: 7 additions & 2 deletions pkg/ottl/contexts/ottllogs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ottl/contexts/ottllogs/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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)
})
Expand Down
9 changes: 7 additions & 2 deletions pkg/ottl/contexts/ottltraces/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ottl/contexts/ottltraces/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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)
})
Expand Down
8 changes: 1 addition & 7 deletions processor/routingprocessor/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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),
}
Expand Down
8 changes: 1 addition & 7 deletions processor/routingprocessor/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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),
}
Expand Down
8 changes: 1 addition & 7 deletions processor/routingprocessor/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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),
}
Expand Down
22 changes: 3 additions & 19 deletions processor/transformprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
10 changes: 1 addition & 9 deletions processor/transformprocessor/internal/logs/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,23 @@ 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"
)

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
}

Expand Down
10 changes: 1 addition & 9 deletions processor/transformprocessor/internal/metrics/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,23 @@ 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"
)

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
}

Expand Down
10 changes: 1 addition & 9 deletions processor/transformprocessor/internal/traces/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,23 @@ 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"
)

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
}

Expand Down
11 changes: 11 additions & 0 deletions unreleased/ottl-parser.yaml
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 8f6530d

Please sign in to comment.