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] Add condition parser #14783

Closed
Closed
Show file tree
Hide file tree
Changes from all 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-condition-parser.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: 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: Adds a ParseConditions function that allows parsing ottl statements that contain only conditions.

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

# (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:
24 changes: 12 additions & 12 deletions pkg/ottl/boolean_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"fmt"
)

// boolExpressionEvaluator is a function that returns the result.
type boolExpressionEvaluator[K any] func(ctx K) bool
// BoolExpressionEvaluator is a function that returns the result.
type BoolExpressionEvaluator[K any] func(ctx K) bool

func alwaysTrue[K any](K) bool {
return true
Expand All @@ -30,8 +30,8 @@ func alwaysFalse[K any](K) bool {
}

// builds a function that returns a short-circuited result of ANDing
// boolExpressionEvaluator funcs
func andFuncs[K any](funcs []boolExpressionEvaluator[K]) boolExpressionEvaluator[K] {
// BoolExpressionEvaluator funcs
func andFuncs[K any](funcs []BoolExpressionEvaluator[K]) BoolExpressionEvaluator[K] {
return func(ctx K) bool {
for _, f := range funcs {
if !f(ctx) {
Expand All @@ -43,8 +43,8 @@ func andFuncs[K any](funcs []boolExpressionEvaluator[K]) boolExpressionEvaluator
}

// builds a function that returns a short-circuited result of ORing
// boolExpressionEvaluator funcs
func orFuncs[K any](funcs []boolExpressionEvaluator[K]) boolExpressionEvaluator[K] {
// BoolExpressionEvaluator funcs
func orFuncs[K any](funcs []BoolExpressionEvaluator[K]) BoolExpressionEvaluator[K] {
return func(ctx K) bool {
for _, f := range funcs {
if f(ctx) {
Expand All @@ -55,7 +55,7 @@ func orFuncs[K any](funcs []boolExpressionEvaluator[K]) boolExpressionEvaluator[
}
}

func (p *Parser[K]) newComparisonEvaluator(comparison *comparison) (boolExpressionEvaluator[K], error) {
func (p *Parser[K]) newComparisonEvaluator(comparison *comparison) (BoolExpressionEvaluator[K], error) {
if comparison == nil {
return alwaysTrue[K], nil
}
Expand All @@ -77,15 +77,15 @@ func (p *Parser[K]) newComparisonEvaluator(comparison *comparison) (boolExpressi

}

func (p *Parser[K]) newBooleanExpressionEvaluator(expr *booleanExpression) (boolExpressionEvaluator[K], error) {
func (p *Parser[K]) newBooleanExpressionEvaluator(expr *booleanExpression) (BoolExpressionEvaluator[K], error) {
if expr == nil {
return alwaysTrue[K], nil
}
f, err := p.newBooleanTermEvaluator(expr.Left)
if err != nil {
return nil, err
}
funcs := []boolExpressionEvaluator[K]{f}
funcs := []BoolExpressionEvaluator[K]{f}
for _, rhs := range expr.Right {
f, err := p.newBooleanTermEvaluator(rhs.Term)
if err != nil {
Expand All @@ -97,15 +97,15 @@ func (p *Parser[K]) newBooleanExpressionEvaluator(expr *booleanExpression) (bool
return orFuncs(funcs), nil
}

func (p *Parser[K]) newBooleanTermEvaluator(term *term) (boolExpressionEvaluator[K], error) {
func (p *Parser[K]) newBooleanTermEvaluator(term *term) (BoolExpressionEvaluator[K], error) {
if term == nil {
return alwaysTrue[K], nil
}
f, err := p.newBooleanValueEvaluator(term.Left)
if err != nil {
return nil, err
}
funcs := []boolExpressionEvaluator[K]{f}
funcs := []BoolExpressionEvaluator[K]{f}
for _, rhs := range term.Right {
f, err := p.newBooleanValueEvaluator(rhs.Value)
if err != nil {
Expand All @@ -117,7 +117,7 @@ func (p *Parser[K]) newBooleanTermEvaluator(term *term) (boolExpressionEvaluator
return andFuncs(funcs), nil
}

func (p *Parser[K]) newBooleanValueEvaluator(value *booleanValue) (boolExpressionEvaluator[K], error) {
func (p *Parser[K]) newBooleanValueEvaluator(value *booleanValue) (BoolExpressionEvaluator[K], error) {
if value == nil {
return alwaysTrue[K], nil
}
Expand Down
16 changes: 7 additions & 9 deletions pkg/ottl/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ import (
"github.com/alecthomas/participle/v2/lexer"
)

// parsedStatement represents a parsed statement. It is the entry point into the statement DSL.
type parsedStatement struct {
// transformationStatement represents a transformation statement. It is the entry point into the statement DSL.
type transformationStatement struct {
Invocation invocation `parser:"@@"`
WhereClause *booleanExpression `parser:"( 'where' @@ )?"`
}

// conditionStatement represents a condition statement.
type conditionStatement struct {
BooleanExpression *booleanExpression `parser:"@@"`
}

// booleanValue represents something that evaluates to a boolean --
// either an equality or inequality, explicit true or false, or
// a parenthesized subexpression.
Expand Down Expand Up @@ -151,13 +156,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
73 changes: 53 additions & 20 deletions pkg/ottl/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,63 @@ import (
"go.uber.org/multierr"
)

// 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]
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
}

type Parser[K any] struct {
functions map[string]interface{}
pathParser PathExpressionParser[K]
enumParser EnumParser
telemetrySettings component.TelemetrySettings
functions map[string]interface{}
pathParser PathExpressionParser[K]
enumParser EnumParser
telemetrySettings component.TelemetrySettings
transformationParser *participle.Parser[transformationStatement]
conditionParser *participle.Parser[conditionStatement]
}

func NewParser[K any](functions map[string]interface{}, pathParser PathExpressionParser[K], enumParser EnumParser, telemetrySettings component.TelemetrySettings) Parser[K] {
return Parser[K]{
functions: functions,
pathParser: pathParser,
enumParser: enumParser,
telemetrySettings: telemetrySettings,
functions: functions,
pathParser: pathParser,
enumParser: enumParser,
telemetrySettings: telemetrySettings,
transformationParser: newParser[transformationStatement](),
conditionParser: newParser[conditionStatement](),
}
}

func (p *Parser[K]) ParseConditions(rawStatements []string) ([]BoolExpressionEvaluator[K], error) {
var statements []BoolExpressionEvaluator[K]
var errors error

for _, statement := range rawStatements {
parsed, err := parseStatement(p.conditionParser, statement)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
expression, err := p.newBooleanExpressionEvaluator(parsed.BooleanExpression)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
statements = append(statements, expression)
}

if errors != nil {
return nil, errors
}
return statements, nil
}

func (p *Parser[K]) ParseStatements(statements []string) ([]Statement[K], error) {
var parsedStatements []Statement[K]
func (p *Parser[K]) ParseStatements(rawStatements []string) ([]Statement[K], error) {
var statements []Statement[K]
var errors error

for _, statement := range statements {
parsed, err := parseStatement(statement)
for _, statement := range rawStatements {
parsed, err := parseStatement(p.transformationParser, statement)
if err != nil {
errors = multierr.Append(errors, err)
continue
Expand All @@ -56,7 +91,7 @@ func (p *Parser[K]) ParseStatements(statements []string) ([]Statement[K], error)
errors = multierr.Append(errors, err)
continue
}
parsedStatements = append(parsedStatements, Statement[K]{
statements = append(statements, Statement[K]{
Function: function,
Condition: expression,
})
Expand All @@ -65,24 +100,22 @@ func (p *Parser[K]) ParseStatements(statements []string) ([]Statement[K], error)
if errors != nil {
return nil, errors
}
return parsedStatements, nil
return statements, nil
}

var parser = newParser()

func parseStatement(raw string) (*parsedStatement, error) {
func parseStatement[G transformationStatement | conditionStatement](parser *participle.Parser[G], raw string) (*G, error) {
parsed, err := parser.ParseString("", raw)
if err != nil {
return nil, err
}
return parsed, nil
}

// newParser returns a parser that can be used to read a string into a parsedStatement. An error will be returned if the string
// newParser returns a parser that can be used to read a string into a transformationStatement. An error will be returned if the string
// is not formatted for the DSL.
func newParser() *participle.Parser[parsedStatement] {
func newParser[K any]() *participle.Parser[K] {
lex := buildLexer()
parser, err := participle.Build[parsedStatement](
parser, err := participle.Build[K](
participle.Lexer(lex),
participle.Unquote("String"),
participle.Elide("whitespace"),
Expand Down
Loading