Skip to content

Commit

Permalink
[pkg/ottl] Change NewParser signature to have a list of Options. (#17394
Browse files Browse the repository at this point in the history
)

* [pkg/ottl] Change NewParser signature to have a list of Options.

* Add default enum parser that return error.
  • Loading branch information
kovrus authored Jan 23, 2023
1 parent de876bd commit 0801b5f
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 47 deletions.
16 changes: 16 additions & 0 deletions .chloggen/ottl-parser-signatiure-opts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# 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: Change signatures of ottl and all context Parsers to accept a list of Options.

# One or more tracking issues related to the change
issues: [13759]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
12 changes: 6 additions & 6 deletions pkg/ottl/boolean_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ func comparisonHelper(left any, right any, op string) *comparison {
}

func Test_newComparisonEvaluator(t *testing.T) {
p := NewParser(
p := NewParser[any](
defaultFunctionsForTests(),
testParsePath,
testParseEnum,
componenttest.NewNopTelemetrySettings(),
WithEnumParser[any](testParseEnum),
)

var tests = []struct {
Expand Down Expand Up @@ -132,11 +132,11 @@ func Test_newComparisonEvaluator(t *testing.T) {
}

func Test_newConditionEvaluator_invalid(t *testing.T) {
p := NewParser(
p := NewParser[any](
defaultFunctionsForTests(),
testParsePath,
testParseEnum,
component.TelemetrySettings{},
WithEnumParser[any](testParseEnum),
)

tests := []struct {
Expand Down Expand Up @@ -165,11 +165,11 @@ func Test_newConditionEvaluator_invalid(t *testing.T) {
}

func Test_newBooleanExpressionEvaluator(t *testing.T) {
p := NewParser(
p := NewParser[any](
defaultFunctionsForTests(),
testParsePath,
testParseEnum,
component.TelemetrySettings{},
WithEnumParser[any](testParseEnum),
)

tests := []struct {
Expand Down
26 changes: 13 additions & 13 deletions pkg/ottl/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func Test_compare(t *testing.T) {
for _, tt := range tests {
for _, op := range ops {
t.Run(fmt.Sprintf("%s %v", tt.name, op), func(t *testing.T) {
p := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
p := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
if got := p.compare(tt.a, tt.b, op); got != tt.want[op] {
t.Errorf("compare(%v, %v, %v) = %v, want %v", tt.a, tt.b, op, got, tt.want[op])
}
Expand All @@ -130,7 +130,7 @@ func Test_compare(t *testing.T) {
// The summary is that they're pretty fast; all the calls to compare are 12 ns/op or less on a 2019 intel
// mac pro laptop, and none of them have any allocations.
func BenchmarkCompareEQInt64(b *testing.B) {
testParser := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
testParser := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -139,7 +139,7 @@ func BenchmarkCompareEQInt64(b *testing.B) {
}

func BenchmarkCompareEQFloat(b *testing.B) {
testParser := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
testParser := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -148,7 +148,7 @@ func BenchmarkCompareEQFloat(b *testing.B) {
}

func BenchmarkCompareEQString(b *testing.B) {
testParser := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
testParser := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -157,7 +157,7 @@ func BenchmarkCompareEQString(b *testing.B) {
}

func BenchmarkCompareEQPString(b *testing.B) {
testParser := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
testParser := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -166,7 +166,7 @@ func BenchmarkCompareEQPString(b *testing.B) {
}

func BenchmarkCompareEQBytes(b *testing.B) {
testParser := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
testParser := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -175,7 +175,7 @@ func BenchmarkCompareEQBytes(b *testing.B) {
}

func BenchmarkCompareEQNil(b *testing.B) {
testParser := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
testParser := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -184,7 +184,7 @@ func BenchmarkCompareEQNil(b *testing.B) {
}

func BenchmarkCompareNEInt(b *testing.B) {
testParser := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
testParser := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -193,7 +193,7 @@ func BenchmarkCompareNEInt(b *testing.B) {
}

func BenchmarkCompareNEFloat(b *testing.B) {
testParser := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
testParser := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -202,7 +202,7 @@ func BenchmarkCompareNEFloat(b *testing.B) {
}

func BenchmarkCompareNEString(b *testing.B) {
testParser := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
testParser := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -211,7 +211,7 @@ func BenchmarkCompareNEString(b *testing.B) {
}

func BenchmarkCompareLTFloat(b *testing.B) {
testParser := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
testParser := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -220,7 +220,7 @@ func BenchmarkCompareLTFloat(b *testing.B) {
}

func BenchmarkCompareLTString(b *testing.B) {
testParser := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
testParser := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -229,7 +229,7 @@ func BenchmarkCompareLTString(b *testing.B) {
}

func BenchmarkCompareLTNil(b *testing.B) {
testParser := NewParser[interface{}](nil, nil, nil, componenttest.NewNopTelemetrySettings())
testParser := NewParser[interface{}](nil, nil, componenttest.NewNopTelemetrySettings())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand Down
15 changes: 13 additions & 2 deletions pkg/ottl/contexts/ottldatapoint/datapoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type TransformContext struct {
cache pcommon.Map
}

type Option func(*ottl.Parser[TransformContext])

func NewTransformContext(dataPoint interface{}, metric pmetric.Metric, metrics pmetric.MetricSlice, instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource) TransformContext {
return TransformContext{
dataPoint: dataPoint,
Expand Down Expand Up @@ -74,8 +76,17 @@ func (tCtx TransformContext) getCache() pcommon.Map {
return tCtx.cache
}

func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings) ottl.Parser[TransformContext] {
return ottl.NewParser[TransformContext](functions, parsePath, parseEnum, telemetrySettings)
func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings, options ...Option) ottl.Parser[TransformContext] {
p := ottl.NewParser[TransformContext](
functions,
parsePath,
telemetrySettings,
ottl.WithEnumParser[TransformContext](parseEnum),
)
for _, opt := range options {
opt(&p)
}
return p
}

var symbolTable = map[ottl.EnumSymbol]ottl.Enum{
Expand Down
15 changes: 13 additions & 2 deletions pkg/ottl/contexts/ottllog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type TransformContext struct {
cache pcommon.Map
}

type Option func(*ottl.Parser[TransformContext])

func NewTransformContext(logRecord plog.LogRecord, instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource) TransformContext {
return TransformContext{
logRecord: logRecord,
Expand All @@ -63,8 +65,17 @@ func (tCtx TransformContext) getCache() pcommon.Map {
return tCtx.cache
}

func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings) ottl.Parser[TransformContext] {
return ottl.NewParser[TransformContext](functions, parsePath, parseEnum, telemetrySettings)
func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings, options ...Option) ottl.Parser[TransformContext] {
p := ottl.NewParser[TransformContext](
functions,
parsePath,
telemetrySettings,
ottl.WithEnumParser[TransformContext](parseEnum),
)
for _, opt := range options {
opt(&p)
}
return p
}

var symbolTable = map[ottl.EnumSymbol]ottl.Enum{
Expand Down
15 changes: 13 additions & 2 deletions pkg/ottl/contexts/ottlmetric/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type TransformContext struct {
cache pcommon.Map
}

type Option func(*ottl.Parser[TransformContext])

func NewTransformContext(metric pmetric.Metric, instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource) TransformContext {
return TransformContext{
metric: metric,
Expand All @@ -62,8 +64,17 @@ func (tCtx TransformContext) getCache() pcommon.Map {
return tCtx.cache
}

func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings) ottl.Parser[TransformContext] {
return ottl.NewParser[TransformContext](functions, parsePath, parseEnum, telemetrySettings)
func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings, options ...Option) ottl.Parser[TransformContext] {
p := ottl.NewParser[TransformContext](
functions,
parsePath,
telemetrySettings,
ottl.WithEnumParser[TransformContext](parseEnum),
)
for _, opt := range options {
opt(&p)
}
return p
}

var symbolTable = ottlcommon.MetricSymbolTable
Expand Down
15 changes: 13 additions & 2 deletions pkg/ottl/contexts/ottlresource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type TransformContext struct {
cache pcommon.Map
}

type Option func(*ottl.Parser[TransformContext])

func NewTransformContext(resource pcommon.Resource) TransformContext {
return TransformContext{
resource: resource,
Expand All @@ -47,8 +49,17 @@ func (tCtx TransformContext) getCache() pcommon.Map {
return tCtx.cache
}

func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings) ottl.Parser[TransformContext] {
return ottl.NewParser[TransformContext](functions, parsePath, parseEnum, telemetrySettings)
func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings, options ...Option) ottl.Parser[TransformContext] {
p := ottl.NewParser[TransformContext](
functions,
parsePath,
telemetrySettings,
ottl.WithEnumParser[TransformContext](parseEnum),
)
for _, opt := range options {
opt(&p)
}
return p
}

func parseEnum(_ *ottl.EnumSymbol) (*ottl.Enum, error) {
Expand Down
15 changes: 13 additions & 2 deletions pkg/ottl/contexts/ottlscope/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type TransformContext struct {
cache pcommon.Map
}

type Option func(*ottl.Parser[TransformContext])

func NewTransformContext(instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource) TransformContext {
return TransformContext{
instrumentationScope: instrumentationScope,
Expand All @@ -54,8 +56,17 @@ func (tCtx TransformContext) getCache() pcommon.Map {
return tCtx.cache
}

func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings) ottl.Parser[TransformContext] {
return ottl.NewParser[TransformContext](functions, parsePath, parseEnum, telemetrySettings)
func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings, options ...Option) ottl.Parser[TransformContext] {
p := ottl.NewParser[TransformContext](
functions,
parsePath,
telemetrySettings,
ottl.WithEnumParser[TransformContext](parseEnum),
)
for _, opt := range options {
opt(&p)
}
return p
}

func parseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) {
Expand Down
15 changes: 13 additions & 2 deletions pkg/ottl/contexts/ottlspan/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type TransformContext struct {
cache pcommon.Map
}

type Option func(*ottl.Parser[TransformContext])

func NewTransformContext(span ptrace.Span, instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource) TransformContext {
return TransformContext{
span: span,
Expand All @@ -61,8 +63,17 @@ func (tCtx TransformContext) getCache() pcommon.Map {
return tCtx.cache
}

func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings) ottl.Parser[TransformContext] {
return ottl.NewParser[TransformContext](functions, parsePath, parseEnum, telemetrySettings)
func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings, options ...Option) ottl.Parser[TransformContext] {
p := ottl.NewParser[TransformContext](
functions,
parsePath,
telemetrySettings,
ottl.WithEnumParser[TransformContext](parseEnum),
)
for _, opt := range options {
opt(&p)
}
return p
}

func parseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) {
Expand Down
15 changes: 13 additions & 2 deletions pkg/ottl/contexts/ottlspanevent/span_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type TransformContext struct {
cache pcommon.Map
}

type Option func(*ottl.Parser[TransformContext])

func NewTransformContext(spanEvent ptrace.SpanEvent, span ptrace.Span, instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource) TransformContext {
return TransformContext{
spanEvent: spanEvent,
Expand Down Expand Up @@ -69,8 +71,17 @@ func (tCtx TransformContext) getCache() pcommon.Map {
return tCtx.cache
}

func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings) ottl.Parser[TransformContext] {
return ottl.NewParser[TransformContext](functions, parsePath, parseEnum, telemetrySettings)
func NewParser(functions map[string]interface{}, telemetrySettings component.TelemetrySettings, options ...Option) ottl.Parser[TransformContext] {
p := ottl.NewParser[TransformContext](
functions,
parsePath,
telemetrySettings,
ottl.WithEnumParser[TransformContext](parseEnum),
)
for _, opt := range options {
opt(&p)
}
return p
}

func parseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/ottl/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ func Test_newGetter(t *testing.T) {

functions := map[string]interface{}{"Hello": hello[interface{}]}

p := NewParser(
p := NewParser[any](
functions,
testParsePath,
testParseEnum,
component.TelemetrySettings{},
WithEnumParser[any](testParseEnum),
)

for _, tt := range tests {
Expand Down
Loading

0 comments on commit 0801b5f

Please sign in to comment.