Skip to content

Commit

Permalink
[processor/routing] Properly create new pdata instances
Browse files Browse the repository at this point in the history
This PR changes the routing processor so that it properly creates new instances of pdata.Span and InstrumentationScope, avoiding crashes later on due to nil pointers.

Fixes open-telemetry#26462

Signed-off-by: Juraci Paixão Kröhling <[email protected]>
  • Loading branch information
jpkrohling committed Sep 5, 2023
1 parent 930b421 commit 5800676
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions processor/routingprocessor/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (p *tracesProcessor) route(ctx context.Context, t ptrace.Traces) error {
for i := 0; i < t.ResourceSpans().Len(); i++ {
rspans := t.ResourceSpans().At(i)
stx := ottlspan.NewTransformContext(
ptrace.Span{},
pcommon.InstrumentationScope{},
ptrace.NewSpan(),
pcommon.NewInstrumentationScope(),
rspans.Resource(),
)

Expand Down
36 changes: 36 additions & 0 deletions processor/routingprocessor/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,42 @@ func TestTracesAreCorrectlySplitPerResourceAttributeWithOTTL(t *testing.T) {
})
}

func TestAttributeWithOTTLDoesNotCauseCrash(t *testing.T) {
defaultExp := &mockTracesExporter{}
firstExp := &mockTracesExporter{}

host := newMockHost(map[component.DataType]map[component.ID]component.Component{
component.DataTypeTraces: {
component.NewID("otlp"): defaultExp,
component.NewIDWithName("otlp", "1"): firstExp,
},
})

exp, err := newTracesProcessor(noopTelemetrySettings, &Config{
DefaultExporters: []string{"otlp"},
Table: []RoutingTableItem{
{
Statement: `route() where attributes["value"] > 0`,
Exporters: []string{"otlp/1"},
},
},
})
require.NoError(t, err)

tr := ptrace.NewTraces()
rl := tr.ResourceSpans().AppendEmpty()
rl.Resource().Attributes().PutInt("value", 1)
span := rl.ScopeSpans().AppendEmpty().Spans().AppendEmpty()
span.SetName("span")

require.NoError(t, exp.Start(context.Background(), host))
require.NoError(t, exp.ConsumeTraces(context.Background(), tr))

assert.Len(t, defaultExp.AllTraces(), 1)
assert.Len(t, firstExp.AllTraces(), 0)

}

func TestTraceProcessorCapabilities(t *testing.T) {
// prepare
config := &Config{
Expand Down

0 comments on commit 5800676

Please sign in to comment.