diff --git a/.chloggen/ottl-improve-telemetry.yaml b/.chloggen/ottl-improve-telemetry.yaml new file mode 100644 index 000000000000..ce3b070712ae --- /dev/null +++ b/.chloggen/ottl-improve-telemetry.yaml @@ -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: 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: Move debug log to `Statement.Execute` so that components using it instead of `StatementSequence` also get debug logs. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [36456] + +# (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: [] diff --git a/pkg/ottl/parser.go b/pkg/ottl/parser.go index 4856279181d1..fade87d2982d 100644 --- a/pkg/ottl/parser.go +++ b/pkg/ottl/parser.go @@ -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. @@ -29,6 +30,11 @@ 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() { + if s.telemetrySettings.Logger != nil { + 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 } @@ -150,9 +156,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 } @@ -332,10 +339,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) diff --git a/pkg/ottl/parser_test.go b/pkg/ottl/parser_test.go index 9e2e09a10e5f..726f531bfb81 100644 --- a/pkg/ottl/parser_test.go +++ b/pkg/ottl/parser_test.go @@ -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) @@ -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,