Skip to content

Commit

Permalink
[pkg/ottl] Move debug log into Statement.Execute
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Nov 19, 2024
1 parent 5ce01a3 commit e32c11f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
22 changes: 13 additions & 9 deletions pkg/ottl/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (
// Statement holds a top level Statement for processing telemetry data. A Statement is a combination of a function
// invocation and the boolean expression to match telemetry for invoking the function.
type Statement[K any] struct {
function Expr[K]
condition BoolExpr[K]
origText string
function Expr[K]
condition BoolExpr[K]
origText string
telemetrySettings component.TelemetrySettings
}

// Execute is a function that will execute the statement's function if the statement's condition is met.
Expand All @@ -29,6 +30,9 @@ type Statement[K any] struct {
// In addition, the functions return value is always returned.
func (s *Statement[K]) Execute(ctx context.Context, tCtx K) (any, bool, error) {
condition, err := s.condition.Eval(ctx, tCtx)
defer func() {
s.telemetrySettings.Logger.Debug("TransformContext after statement execution", zap.String("statement", s.origText), zap.Bool("condition matched", condition), zap.Any("TransformContext", tCtx))
}()
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -150,9 +154,10 @@ func (p *Parser[K]) ParseStatement(statement string) (*Statement[K], error) {
return nil, err
}
return &Statement[K]{
function: function,
condition: expression,
origText: statement,
function: function,
condition: expression,
origText: statement,
telemetrySettings: p.telemetrySettings,
}, nil
}

Expand Down Expand Up @@ -332,10 +337,9 @@ func NewStatementSequence[K any](statements []*Statement[K], telemetrySettings c
// When the ErrorMode of the StatementSequence is `ignore`, errors are logged and execution continues to the next statement.
// When the ErrorMode of the StatementSequence is `silent`, errors are not logged and execution continues to the next statement.
func (s *StatementSequence[K]) Execute(ctx context.Context, tCtx K) error {
s.telemetrySettings.Logger.Debug("initial TransformContext", zap.Any("TransformContext", tCtx))
s.telemetrySettings.Logger.Debug("initial TransformContext before executing StatementSequence", zap.Any("TransformContext", tCtx))
for _, statement := range s.statements {
_, condition, err := statement.Execute(ctx, tCtx)
s.telemetrySettings.Logger.Debug("TransformContext after statement execution", zap.String("statement", statement.origText), zap.Bool("condition matched", condition), zap.Any("TransformContext", tCtx))
_, _, err := statement.Execute(ctx, tCtx)
if err != nil {
if s.errorMode == PropagateError {
err = fmt.Errorf("failed to execute statement: %v, %w", statement.origText, err)
Expand Down
10 changes: 6 additions & 4 deletions pkg/ottl/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2382,8 +2382,9 @@ func Test_Statement_Execute(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
statement := Statement[any]{
condition: BoolExpr[any]{tt.condition},
function: Expr[any]{exprFunc: tt.function},
condition: BoolExpr[any]{tt.condition},
function: Expr[any]{exprFunc: tt.function},
telemetrySettings: componenttest.NewNopTelemetrySettings(),
}

result, condition, err := statement.Execute(context.Background(), nil)
Expand Down Expand Up @@ -2497,8 +2498,9 @@ func Test_Statements_Execute_Error(t *testing.T) {
statements := StatementSequence[any]{
statements: []*Statement[any]{
{
condition: BoolExpr[any]{tt.condition},
function: Expr[any]{exprFunc: tt.function},
condition: BoolExpr[any]{tt.condition},
function: Expr[any]{exprFunc: tt.function},
telemetrySettings: componenttest.NewNopTelemetrySettings(),
},
},
errorMode: tt.errorMode,
Expand Down

0 comments on commit e32c11f

Please sign in to comment.