Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/ottl] Convert Statement to an interface #14869

Merged
merged 8 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/ottl-eval-interface.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: Changed Statement to be an interface with an `Execute` function.

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

# (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:
7 changes: 0 additions & 7 deletions pkg/ottl/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,6 @@ type Field struct {
MapKey *string `parser:"( '[' @String ']' )?"`
}

// Statement holds a top level Statement for processing telemetry data. A Statement is a combination of a function
// invocation and the expression to match telemetry for invoking the function.
type Statement[K any] struct {
Function ExprFunc[K]
Condition boolExpressionEvaluator[K]
}

// byteSlice type for capturing byte slices
type byteSlice []byte

Expand Down
27 changes: 24 additions & 3 deletions pkg/ottl/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ type Parser[K any] struct {
telemetrySettings component.TelemetrySettings
}

// Statement is an interface that represents a parsed OTTL Statement
type Statement[K any] interface {
// Execute is a function that will execute the statement's function if the statement's condition is met.
// Returns true if the function was run, returns false otherwise.
// If the statement contains no condition, the function will run and true will be returned.
Execute(ctx K) bool
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
}

type standardStatement[K any] struct {
function ExprFunc[K]
condition boolExpressionEvaluator[K]
}

func (s standardStatement[K]) Execute(ctx K) bool {
condition := s.condition(ctx)
if condition {
s.function(ctx)
}
return condition
}

func NewParser[K any](functions map[string]interface{}, pathParser PathExpressionParser[K], enumParser EnumParser, telemetrySettings component.TelemetrySettings) Parser[K] {
return Parser[K]{
functions: functions,
Expand Down Expand Up @@ -56,9 +77,9 @@ func (p *Parser[K]) ParseStatements(statements []string) ([]Statement[K], error)
errors = multierr.Append(errors, err)
continue
}
parsedStatements = append(parsedStatements, Statement[K]{
Function: function,
Condition: expression,
parsedStatements = append(parsedStatements, standardStatement[K]{
function: function,
condition: expression,
})
}

Expand Down
4 changes: 2 additions & 2 deletions processor/routingprocessor/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ func (p *logProcessor) route(ctx context.Context, l plog.Logs) error {

matchCount := len(p.router.routes)
for key, route := range p.router.routes {
if !route.expression.Condition(ltx) {
isMatch := route.expression.Execute(ltx)
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
if !isMatch {
matchCount--
continue
}
route.expression.Function(ltx)
p.group(key, groups, route.exporters, rlogs)
}

Expand Down
4 changes: 2 additions & 2 deletions processor/routingprocessor/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func (p *metricsProcessor) route(ctx context.Context, tm pmetric.Metrics) error

matchCount := len(p.router.routes)
for key, route := range p.router.routes {
if !route.expression.Condition(mtx) {
isMatch := route.expression.Execute(mtx)
if !isMatch {
matchCount--
continue
}
route.expression.Function(mtx)
p.group(key, groups, route.exporters, rmetrics)
}

Expand Down
4 changes: 2 additions & 2 deletions processor/routingprocessor/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ func (p *tracesProcessor) route(ctx context.Context, t ptrace.Traces) error {

matchCount := len(p.router.routes)
for key, route := range p.router.routes {
if !route.expression.Condition(stx) {
isMatch := route.expression.Execute(stx)
if !isMatch {
matchCount--
continue
}
route.expression.Function(stx)
p.group(key, groups, route.exporters, rspans)
}

Expand Down
4 changes: 1 addition & 3 deletions processor/transformprocessor/internal/logs/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ func (p *Processor) ProcessLogs(_ context.Context, td plog.Logs) (plog.Logs, err
for k := 0; k < logs.Len(); k++ {
ctx := ottllogs.NewTransformContext(logs.At(k), slogs.Scope(), rlogs.Resource())
for _, statement := range p.statements {
if statement.Condition(ctx) {
statement.Function(ctx)
}
statement.Execute(ctx)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ func (p *Processor) handleSummaryDataPoints(dps pmetric.SummaryDataPointSlice, m

func (p *Processor) callFunctions(ctx ottldatapoints.TransformContext) {
for _, statement := range p.statements {
if statement.Condition(ctx) {
statement.Function(ctx)
}
statement.Execute(ctx)
}
}
4 changes: 1 addition & 3 deletions processor/transformprocessor/internal/traces/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ func (p *Processor) ProcessTraces(_ context.Context, td ptrace.Traces) (ptrace.T
for k := 0; k < spans.Len(); k++ {
ctx := ottltraces.NewTransformContext(spans.At(k), sspan.Scope(), rspans.Resource())
for _, statement := range p.statements {
if statement.Condition(ctx) {
statement.Function(ctx)
}
statement.Execute(ctx)
}
}
}
Expand Down