Skip to content

Commit

Permalink
[processor/transform] Fix panic when transforming span events (open-t…
Browse files Browse the repository at this point in the history
…elemetry#16622)

* Fix bug

* changelog
  • Loading branch information
TylerHelmuth committed Dec 5, 2022
1 parent 14da010 commit 05bcb42
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .chloggen/tp-fix-spanevent-bug.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: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: processor/transform

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix issue where collector would panic under certain conditions if the transformprocessor was configured to transform span events.

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

# (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:
2 changes: 1 addition & 1 deletion processor/transformprocessor/internal/common/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s spanEventStatements) ConsumeTraces(ctx context.Context, td ptrace.Traces
span := spans.At(k)
spanEvents := span.Events()
for n := 0; n < spanEvents.Len(); n++ {
tCtx := ottlspanevent.NewTransformContext(spanEvents.At(k), span, sspans.Scope(), rspans.Resource())
tCtx := ottlspanevent.NewTransformContext(spanEvents.At(n), span, sspans.Scope(), rspans.Resource())
for _, statement := range s {
_, _, err := statement.Execute(ctx, tCtx)
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions processor/transformprocessor/internal/traces/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,36 @@ func Test_ProcessTraces_TraceContext(t *testing.T) {
}
}

func Test_ProcessTraces_SpanEventContext(t *testing.T) {
tests := []struct {
statement string
want func(td ptrace.Traces)
}{
{
statement: `set(attributes["test"], "pass") where name == "eventA"`,
want: func(td ptrace.Traces) {
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Events().At(0).Attributes().PutStr("test", "pass")
},
},
}

for _, tt := range tests {
t.Run(tt.statement, func(t *testing.T) {
td := constructTraces()
processor, err := NewProcessor(nil, []common.ContextStatements{{Context: "spanevent", Statements: []string{tt.statement}}}, componenttest.NewNopTelemetrySettings())
assert.NoError(t, err)

_, err = processor.ProcessTraces(context.Background(), td)
assert.NoError(t, err)

exTd := constructTraces()
tt.want(exTd)

assert.Equal(t, exTd, td)
})
}
}

func Test_ProcessTraces_MixContext(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -617,6 +647,8 @@ func fillSpanOne(span ptrace.Span) {
status := span.Status()
status.SetCode(ptrace.StatusCodeError)
status.SetMessage("status-cancelled")
event := span.Events().AppendEmpty()
event.SetName("eventA")
}

func fillSpanTwo(span ptrace.Span) {
Expand All @@ -635,4 +667,6 @@ func fillSpanTwo(span ptrace.Span) {
status := span.Status()
status.SetCode(ptrace.StatusCodeError)
status.SetMessage("status-cancelled")
event := span.Events().AppendEmpty()
event.SetName("eventB")
}

0 comments on commit 05bcb42

Please sign in to comment.