Skip to content

Commit 2d4d3e7

Browse files
TylerHelmuthzeck-ops
authored andcommitted
[pkg/ottl] Move debug log into Statement.Execute (open-telemetry#36456)
#### Description @tdarwin noticed that the routingconnector wasnt emitting ottl logs when debug logging was enabled. This turned out to be because we added the debug statement to `StatementSequence.Execute` but the routingconnector uses `Statement.Execute`, but it only works with single statements. This PR moves the debug log into the `Statement.Execute` function so that more components benefit from the debug logs. <!--Describe what testing was performed and which tests were added.--> #### Testing Tested locally using this config: ```yaml receivers: otlpjsonfile: include: - /Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json connectors: routing: default_pipelines: [traces/a] table: - statement: route() where attributes["service.name"] == "checkpoint-test" pipelines: [traces/b] processors: transform: error_mode: ignore trace_statements: - context: resource statements: - set(attributes["test"], "pass") exporters: debug/a: debug/b: service: telemetry: logs: level: debug pipelines: traces: receivers: - otlpjsonfile exporters: - routing traces/a: receivers: - routing exporters: - debug/a traces/b: receivers: - routing processors: - transform exporters: - debug/b ``` The collector spit out these logs: ``` ❯ ./bin/otelcontribcol_darwin_arm64 --config ./local/config.yaml 2024-11-19T12:29:41.671-0700 info [email protected]/service.go:166 Setting up own telemetry... 2024-11-19T12:29:41.671-0700 info telemetry/metrics.go:70 Serving metrics {"address": "localhost:8888", "metrics level": "Normal"} 2024-11-19T12:29:41.672-0700 info builders/builders.go:26 Development component. May change in the future. {"kind": "exporter", "data_type": "traces", "name": "debug/b"} 2024-11-19T12:29:41.672-0700 debug builders/builders.go:24 Alpha component. May change in the future. {"kind": "processor", "name": "transform", "pipeline": "traces/b"} 2024-11-19T12:29:41.672-0700 info builders/builders.go:26 Development component. May change in the future. {"kind": "exporter", "data_type": "traces", "name": "debug/a"} 2024-11-19T12:29:41.672-0700 debug builders/builders.go:24 Alpha component. May change in the future. {"kind": "connector", "name": "routing", "exporter_in_pipeline": "traces", "receiver_in_pipeline": "traces"} 2024-11-19T12:29:41.673-0700 debug builders/builders.go:24 Alpha component. May change in the future. {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces"} 2024-11-19T12:29:41.673-0700 info [email protected]/service.go:238 Starting otelcontribcol... {"Version": "0.114.0-dev", "NumCPU": 10} 2024-11-19T12:29:41.673-0700 info extensions/extensions.go:39 Starting extensions... 2024-11-19T12:29:41.673-0700 info [email protected]/service.go:261 Everything is ready. Begin running and processing data. 2024-11-19T12:29:41.875-0700 debug fileconsumer/file.go:125 matched files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:41.875-0700 debug fileconsumer/file.go:157 Consuming files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:42.076-0700 debug fileconsumer/file.go:125 matched files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:42.076-0700 debug fileconsumer/file.go:157 Consuming files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:42.275-0700 debug fileconsumer/file.go:125 matched files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:42.275-0700 debug fileconsumer/file.go:157 Consuming files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:42.474-0700 debug fileconsumer/file.go:125 matched files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:42.474-0700 debug fileconsumer/file.go:157 Consuming files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:42.675-0700 debug fileconsumer/file.go:125 matched files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:42.675-0700 debug fileconsumer/file.go:157 Consuming files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:42.875-0700 debug fileconsumer/file.go:125 matched files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:42.875-0700 debug fileconsumer/file.go:157 Consuming files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:43.076-0700 debug fileconsumer/file.go:125 matched files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:43.076-0700 debug fileconsumer/file.go:157 Consuming files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:43.274-0700 debug fileconsumer/file.go:125 matched files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:43.274-0700 debug fileconsumer/file.go:157 Consuming files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:43.475-0700 debug fileconsumer/file.go:125 matched files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:43.475-0700 debug fileconsumer/file.go:157 Consuming files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:43.675-0700 debug fileconsumer/file.go:125 matched files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:43.675-0700 debug fileconsumer/file.go:157 Consuming files {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "paths": ["/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"]} 2024-11-19T12:29:44.474-0700 info fileconsumer/file.go:265 Started watching file {"kind": "receiver", "name": "otlpjsonfile", "data_type": "traces", "component": "fileconsumer", "path": "/Users/tylerhelmuth/projects/opentelemetry-collector-contrib/local/test.json"} 2024-11-19T12:29:44.475-0700 debug [email protected]/parser.go:34 TransformContext after statement execution {"kind": "connector", "name": "routing", "exporter_in_pipeline": "traces", "receiver_in_pipeline": "traces", "statement": "route() where attributes[\"service.name\"] == \"checkpoint-test\"", "condition matched": true, "TransformContext": {"resource": {"attributes": {"service.name": "checkpoint-test", "platform.env": "prd"}, "dropped_attribute_count": 0}, "cache": {}}} 2024-11-19T12:29:44.475-0700 debug [email protected]/parser.go:340 initial TransformContext before executing StatementSequence {"kind": "processor", "name": "transform", "pipeline": "traces/b", "TransformContext": {"resource": {"attributes": {"service.name": "checkpoint-test", "platform.env": "prd"}, "dropped_attribute_count": 0}, "cache": {}}} 2024-11-19T12:29:44.475-0700 debug [email protected]/parser.go:34 TransformContext after statement execution {"kind": "processor", "name": "transform", "pipeline": "traces/b", "statement": "set(attributes[\"test\"], \"pass\")", "condition matched": true, "TransformContext": {"resource": {"attributes": {"service.name": "checkpoint-test", "platform.env": "prd", "test": "pass"}, "dropped_attribute_count": 0}, "cache": {}}} 2024-11-19T12:29:44.475-0700 info Traces {"kind": "exporter", "data_type": "traces", "name": "debug/b", "resource spans": 1, "spans": 1} ^C2024-11-19T12:29:48.931-0700 info [email protected]/collector.go:328 Received signal from OS {"signal": "interrupt"} 2024-11-19T12:29:48.931-0700 info [email protected]/service.go:303 Starting shutdown... 2024-11-19T12:29:48.931-0700 info extensions/extensions.go:66 Stopping extensions... 2024-11-19T12:29:48.931-0700 info [email protected]/service.go:317 Shutdown complete. ```
1 parent 142074d commit 2d4d3e7

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

.chloggen/ottl-improve-telemetry.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Move debug log to `Statement.Execute` so that components using it instead of `StatementSequence` also get debug logs.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [36456]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

pkg/ottl/parser.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import (
1818
// Statement holds a top level Statement for processing telemetry data. A Statement is a combination of a function
1919
// invocation and the boolean expression to match telemetry for invoking the function.
2020
type Statement[K any] struct {
21-
function Expr[K]
22-
condition BoolExpr[K]
23-
origText string
21+
function Expr[K]
22+
condition BoolExpr[K]
23+
origText string
24+
telemetrySettings component.TelemetrySettings
2425
}
2526

2627
// 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 {
2930
// In addition, the functions return value is always returned.
3031
func (s *Statement[K]) Execute(ctx context.Context, tCtx K) (any, bool, error) {
3132
condition, err := s.condition.Eval(ctx, tCtx)
33+
defer func() {
34+
if s.telemetrySettings.Logger != nil {
35+
s.telemetrySettings.Logger.Debug("TransformContext after statement execution", zap.String("statement", s.origText), zap.Bool("condition matched", condition), zap.Any("TransformContext", tCtx))
36+
}
37+
}()
3238
if err != nil {
3339
return nil, false, err
3440
}
@@ -150,9 +156,10 @@ func (p *Parser[K]) ParseStatement(statement string) (*Statement[K], error) {
150156
return nil, err
151157
}
152158
return &Statement[K]{
153-
function: function,
154-
condition: expression,
155-
origText: statement,
159+
function: function,
160+
condition: expression,
161+
origText: statement,
162+
telemetrySettings: p.telemetrySettings,
156163
}, nil
157164
}
158165

@@ -332,10 +339,9 @@ func NewStatementSequence[K any](statements []*Statement[K], telemetrySettings c
332339
// When the ErrorMode of the StatementSequence is `ignore`, errors are logged and execution continues to the next statement.
333340
// When the ErrorMode of the StatementSequence is `silent`, errors are not logged and execution continues to the next statement.
334341
func (s *StatementSequence[K]) Execute(ctx context.Context, tCtx K) error {
335-
s.telemetrySettings.Logger.Debug("initial TransformContext", zap.Any("TransformContext", tCtx))
342+
s.telemetrySettings.Logger.Debug("initial TransformContext before executing StatementSequence", zap.Any("TransformContext", tCtx))
336343
for _, statement := range s.statements {
337-
_, condition, err := statement.Execute(ctx, tCtx)
338-
s.telemetrySettings.Logger.Debug("TransformContext after statement execution", zap.String("statement", statement.origText), zap.Bool("condition matched", condition), zap.Any("TransformContext", tCtx))
344+
_, _, err := statement.Execute(ctx, tCtx)
339345
if err != nil {
340346
if s.errorMode == PropagateError {
341347
err = fmt.Errorf("failed to execute statement: %v, %w", statement.origText, err)

pkg/ottl/parser_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,8 +2382,9 @@ func Test_Statement_Execute(t *testing.T) {
23822382
for _, tt := range tests {
23832383
t.Run(tt.name, func(t *testing.T) {
23842384
statement := Statement[any]{
2385-
condition: BoolExpr[any]{tt.condition},
2386-
function: Expr[any]{exprFunc: tt.function},
2385+
condition: BoolExpr[any]{tt.condition},
2386+
function: Expr[any]{exprFunc: tt.function},
2387+
telemetrySettings: componenttest.NewNopTelemetrySettings(),
23872388
}
23882389

23892390
result, condition, err := statement.Execute(context.Background(), nil)
@@ -2497,8 +2498,9 @@ func Test_Statements_Execute_Error(t *testing.T) {
24972498
statements := StatementSequence[any]{
24982499
statements: []*Statement[any]{
24992500
{
2500-
condition: BoolExpr[any]{tt.condition},
2501-
function: Expr[any]{exprFunc: tt.function},
2501+
condition: BoolExpr[any]{tt.condition},
2502+
function: Expr[any]{exprFunc: tt.function},
2503+
telemetrySettings: componenttest.NewNopTelemetrySettings(),
25022504
},
25032505
},
25042506
errorMode: tt.errorMode,

0 commit comments

Comments
 (0)