Skip to content

Commit

Permalink
Add OTTL Condition parser
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Nov 17, 2023
1 parent 8e09b17 commit 7b09b1b
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 184 deletions.
27 changes: 27 additions & 0 deletions .chloggen/ottl-condition-parser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# 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: Replace `Statements` struct with `StatementCollection` interface

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [29294]

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
13 changes: 7 additions & 6 deletions connector/countconnector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func TestConfigErrors(t *testing.T) {
},
},
},
expect: fmt.Sprintf("spans condition: metric %q: unable to parse OTTL statement", defaultMetricNameSpans),
expect: fmt.Sprintf("spans condition: metric %q: unable to parse OTTL condition", defaultMetricNameSpans),
},
{
name: "invalid_condition_spanevent",
Expand All @@ -425,7 +425,7 @@ func TestConfigErrors(t *testing.T) {
},
},
},
expect: fmt.Sprintf("spanevents condition: metric %q: unable to parse OTTL statement", defaultMetricNameSpanEvents),
expect: fmt.Sprintf("spanevents condition: metric %q: unable to parse OTTL condition", defaultMetricNameSpanEvents),
},
{
name: "invalid_condition_metric",
Expand All @@ -437,7 +437,7 @@ func TestConfigErrors(t *testing.T) {
},
},
},
expect: fmt.Sprintf("metrics condition: metric %q: unable to parse OTTL statement", defaultMetricNameMetrics),
expect: fmt.Sprintf("metrics condition: metric %q: unable to parse OTTL condition", defaultMetricNameMetrics),
},
{
name: "invalid_condition_datapoint",
Expand All @@ -449,7 +449,7 @@ func TestConfigErrors(t *testing.T) {
},
},
},
expect: fmt.Sprintf("datapoints condition: metric %q: unable to parse OTTL statement", defaultMetricNameDataPoints),
expect: fmt.Sprintf("datapoints condition: metric %q: unable to parse OTTL condition", defaultMetricNameDataPoints),
},
{
name: "invalid_condition_log",
Expand All @@ -461,15 +461,16 @@ func TestConfigErrors(t *testing.T) {
},
},
},
expect: fmt.Sprintf("logs condition: metric %q: unable to parse OTTL statement", defaultMetricNameLogs),
expect: fmt.Sprintf("logs condition: metric %q: unable to parse OTTL condition", defaultMetricNameLogs),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.input.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), tc.expect)
errText := err.Error()
assert.Contains(t, errText, tc.expect)
})
}
}
50 changes: 18 additions & 32 deletions internal/filter/filterottl/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ func NewBoolExprForSpan(conditions []string, functions map[string]ottl.Factory[o
if _, ok := functions[match.Name()]; !ok {
functions[match.Name()] = match
}
statmentsStr := conditionsToStatements(conditions)
parser, err := ottlspan.NewParser(functions, set)
if err != nil {
return nil, err
}
statements, err := parser.ParseStatements(statmentsStr)
parsedConditions, err := parser.ParseConditions(conditions)
if err != nil {
return nil, err
}
s := ottlspan.NewStatements(statements, set, ottlspan.WithErrorMode(errorMode))
return &s, nil
cs := ottlspan.NewConditionSequence(parsedConditions, errorMode, set)
return &cs, nil
}

// NewBoolExprForSpanEvent creates a BoolExpr[ottlspanevent.TransformContext] that will return true if any of the given OTTL conditions evaluate to true.
Expand All @@ -45,17 +44,16 @@ func NewBoolExprForSpanEvent(conditions []string, functions map[string]ottl.Fact
if _, ok := functions[match.Name()]; !ok {
functions[match.Name()] = match
}
statmentsStr := conditionsToStatements(conditions)
parser, err := ottlspanevent.NewParser(functions, set)
if err != nil {
return nil, err
}
statements, err := parser.ParseStatements(statmentsStr)
parsedConditions, err := parser.ParseConditions(conditions)
if err != nil {
return nil, err
}
s := ottlspanevent.NewStatements(statements, set, ottlspanevent.WithErrorMode(errorMode))
return &s, nil
cs := ottlspanevent.NewConditionSequence(parsedConditions, errorMode, set)
return &cs, nil
}

// NewBoolExprForMetric creates a BoolExpr[ottlmetric.TransformContext] that will return true if any of the given OTTL conditions evaluate to true.
Expand All @@ -66,17 +64,16 @@ func NewBoolExprForMetric(conditions []string, functions map[string]ottl.Factory
if _, ok := functions[match.Name()]; !ok {
functions[match.Name()] = match
}
statmentsStr := conditionsToStatements(conditions)
parser, err := ottlmetric.NewParser(functions, set)
if err != nil {
return nil, err
}
statements, err := parser.ParseStatements(statmentsStr)
parsedConditions, err := parser.ParseConditions(conditions)
if err != nil {
return nil, err
}
s := ottlmetric.NewStatements(statements, set, ottlmetric.WithErrorMode(errorMode))
return &s, nil
cs := ottlmetric.NewConditionSequence(parsedConditions, errorMode, set)
return &cs, nil
}

// NewBoolExprForDataPoint creates a BoolExpr[ottldatapoint.TransformContext] that will return true if any of the given OTTL conditions evaluate to true.
Expand All @@ -87,17 +84,16 @@ func NewBoolExprForDataPoint(conditions []string, functions map[string]ottl.Fact
if _, ok := functions[match.Name()]; !ok {
functions[match.Name()] = match
}
statmentsStr := conditionsToStatements(conditions)
parser, err := ottldatapoint.NewParser(functions, set)
if err != nil {
return nil, err
}
statements, err := parser.ParseStatements(statmentsStr)
parsedConditions, err := parser.ParseConditions(conditions)
if err != nil {
return nil, err
}
s := ottldatapoint.NewStatements(statements, set, ottldatapoint.WithErrorMode(errorMode))
return &s, nil
cs := ottldatapoint.NewConditionSequence(parsedConditions, errorMode, set)
return &cs, nil
}

// NewBoolExprForLog creates a BoolExpr[ottllog.TransformContext] that will return true if any of the given OTTL conditions evaluate to true.
Expand All @@ -108,17 +104,16 @@ func NewBoolExprForLog(conditions []string, functions map[string]ottl.Factory[ot
if _, ok := functions[match.Name()]; !ok {
functions[match.Name()] = match
}
statmentsStr := conditionsToStatements(conditions)
parser, err := ottllog.NewParser(functions, set)
if err != nil {
return nil, err
}
statements, err := parser.ParseStatements(statmentsStr)
parsedConditions, err := parser.ParseConditions(conditions)
if err != nil {
return nil, err
}
s := ottllog.NewStatements(statements, set, ottllog.WithErrorMode(errorMode))
return &s, nil
cs := ottllog.NewConditionSequence(parsedConditions, errorMode, set)
return &cs, nil
}

// NewBoolExprForResource creates a BoolExpr[ottlresource.TransformContext] that will return true if any of the given OTTL conditions evaluate to true.
Expand All @@ -129,23 +124,14 @@ func NewBoolExprForResource(conditions []string, functions map[string]ottl.Facto
if _, ok := functions[match.Name()]; !ok {
functions[match.Name()] = match
}
statmentsStr := conditionsToStatements(conditions)
parser, err := ottlresource.NewParser(functions, set)
if err != nil {
return nil, err
}
statements, err := parser.ParseStatements(statmentsStr)
parsedConditions, err := parser.ParseConditions(conditions)
if err != nil {
return nil, err
}
s := ottlresource.NewStatements(statements, set, ottlresource.WithErrorMode(errorMode))
return &s, nil
}

func conditionsToStatements(conditions []string) []string {
statements := make([]string, len(conditions))
for i, condition := range conditions {
statements[i] = "match() where " + condition
}
return statements
cs := ottlresource.NewConditionSequence(parsedConditions, errorMode, set)
return &cs, nil
}
16 changes: 4 additions & 12 deletions pkg/ottl/contexts/ottldatapoint/datapoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,12 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
return p, nil
}

type StatementsOption func(*ottl.Statements[TransformContext])

func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
return func(s *ottl.Statements[TransformContext]) {
ottl.WithErrorMode[TransformContext](errorMode)(s)
}
func NewStatementSequence(statements []*ottl.Statement[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.StatementSequenceOption[TransformContext]) ottl.StatementSequence[TransformContext] {
return ottl.NewStatementSequence(statements, errorMode, telemetrySettings, options...)
}

func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
s := ottl.NewStatements(statements, telemetrySettings)
for _, op := range options {
op(&s)
}
return s
func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
}

var symbolTable = map[ottl.EnumSymbol]ottl.Enum{
Expand Down
16 changes: 4 additions & 12 deletions pkg/ottl/contexts/ottllog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,12 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
return p, nil
}

type StatementsOption func(*ottl.Statements[TransformContext])

func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
return func(s *ottl.Statements[TransformContext]) {
ottl.WithErrorMode[TransformContext](errorMode)(s)
}
func NewStatementSequence(statements []*ottl.Statement[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.StatementSequenceOption[TransformContext]) ottl.StatementSequence[TransformContext] {
return ottl.NewStatementSequence(statements, errorMode, telemetrySettings, options...)
}

func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
s := ottl.NewStatements(statements, telemetrySettings)
for _, op := range options {
op(&s)
}
return s
func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
}

var symbolTable = map[ottl.EnumSymbol]ottl.Enum{
Expand Down
16 changes: 4 additions & 12 deletions pkg/ottl/contexts/ottlmetric/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,12 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
return p, err
}

type StatementsOption func(*ottl.Statements[TransformContext])

func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
return func(s *ottl.Statements[TransformContext]) {
ottl.WithErrorMode[TransformContext](errorMode)(s)
}
func NewStatementSequence(statements []*ottl.Statement[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.StatementSequenceOption[TransformContext]) ottl.StatementSequence[TransformContext] {
return ottl.NewStatementSequence(statements, errorMode, telemetrySettings, options...)
}

func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
s := ottl.NewStatements(statements, telemetrySettings)
for _, op := range options {
op(&s)
}
return s
func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
}

var symbolTable = internal.MetricSymbolTable
Expand Down
16 changes: 4 additions & 12 deletions pkg/ottl/contexts/ottlresource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,12 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
return p, nil
}

type StatementsOption func(*ottl.Statements[TransformContext])

func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
return func(s *ottl.Statements[TransformContext]) {
ottl.WithErrorMode[TransformContext](errorMode)(s)
}
func NewStatementSequence(statements []*ottl.Statement[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.StatementSequenceOption[TransformContext]) ottl.StatementSequence[TransformContext] {
return ottl.NewStatementSequence(statements, errorMode, telemetrySettings, options...)
}

func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
s := ottl.NewStatements(statements, telemetrySettings)
for _, op := range options {
op(&s)
}
return s
func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
}

func parseEnum(_ *ottl.EnumSymbol) (*ottl.Enum, error) {
Expand Down
16 changes: 4 additions & 12 deletions pkg/ottl/contexts/ottlscope/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,12 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
return p, nil
}

type StatementsOption func(*ottl.Statements[TransformContext])

func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
return func(s *ottl.Statements[TransformContext]) {
ottl.WithErrorMode[TransformContext](errorMode)(s)
}
func NewStatementSequence(statements []*ottl.Statement[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.StatementSequenceOption[TransformContext]) ottl.StatementSequence[TransformContext] {
return ottl.NewStatementSequence(statements, errorMode, telemetrySettings, options...)
}

func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
s := ottl.NewStatements(statements, telemetrySettings)
for _, op := range options {
op(&s)
}
return s
func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
}

func parseEnum(_ *ottl.EnumSymbol) (*ottl.Enum, error) {
Expand Down
16 changes: 4 additions & 12 deletions pkg/ottl/contexts/ottlspan/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,12 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
return p, nil
}

type StatementsOption func(*ottl.Statements[TransformContext])

func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
return func(s *ottl.Statements[TransformContext]) {
ottl.WithErrorMode[TransformContext](errorMode)(s)
}
func NewStatementSequence(statements []*ottl.Statement[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.StatementSequenceOption[TransformContext]) ottl.StatementSequence[TransformContext] {
return ottl.NewStatementSequence(statements, errorMode, telemetrySettings, options...)
}

func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
s := ottl.NewStatements(statements, telemetrySettings)
for _, op := range options {
op(&s)
}
return s
func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
}

func parseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) {
Expand Down
16 changes: 4 additions & 12 deletions pkg/ottl/contexts/ottlspanevent/span_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,12 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
return p, nil
}

type StatementsOption func(*ottl.Statements[TransformContext])

func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
return func(s *ottl.Statements[TransformContext]) {
ottl.WithErrorMode[TransformContext](errorMode)(s)
}
func NewStatementSequence(statements []*ottl.Statement[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.StatementSequenceOption[TransformContext]) ottl.StatementSequence[TransformContext] {
return ottl.NewStatementSequence(statements, errorMode, telemetrySettings, options...)
}

func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
s := ottl.NewStatements(statements, telemetrySettings)
for _, op := range options {
op(&s)
}
return s
func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
}

func parseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) {
Expand Down
Loading

0 comments on commit 7b09b1b

Please sign in to comment.