From 5800676b918732f85236ca287569f6f21d4eb446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraci=20Paix=C3=A3o=20Kr=C3=B6hling?= Date: Tue, 5 Sep 2023 09:10:06 -0300 Subject: [PATCH] [processor/routing] Properly create new pdata instances MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 #26462 Signed-off-by: Juraci Paixão Kröhling --- processor/routingprocessor/traces.go | 4 +-- processor/routingprocessor/traces_test.go | 36 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/processor/routingprocessor/traces.go b/processor/routingprocessor/traces.go index 2a370c3c3044..f0f004c76ce6 100644 --- a/processor/routingprocessor/traces.go +++ b/processor/routingprocessor/traces.go @@ -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(), ) diff --git a/processor/routingprocessor/traces_test.go b/processor/routingprocessor/traces_test.go index d42a71dc89c8..24ebb3944390 100644 --- a/processor/routingprocessor/traces_test.go +++ b/processor/routingprocessor/traces_test.go @@ -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{