Skip to content

Commit

Permalink
[processor/routing] Properly create new pdata instances (#26464)
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 #26462

Signed-off-by: Juraci Paixão Kröhling <[email protected]>

---------

Signed-off-by: Juraci Paixão Kröhling <[email protected]>
  • Loading branch information
jpkrohling authored Sep 6, 2023
1 parent db5fd4d commit d0e400d
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 7 deletions.
27 changes: 27 additions & 0 deletions .chloggen/26464-routingprocessor-fix.yaml
Original file line number Diff line number Diff line change
@@ -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: 'bug_fix'

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: When using attributes instead of resource attributes, the routing processor would crash the collector. This does not affect the connector version of this component.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [26462]

# (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: [user]
4 changes: 2 additions & 2 deletions processor/routingprocessor/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func (p *logProcessor) route(ctx context.Context, l plog.Logs) error {
for i := 0; i < l.ResourceLogs().Len(); i++ {
rlogs := l.ResourceLogs().At(i)
ltx := ottllog.NewTransformContext(
plog.LogRecord{},
pcommon.InstrumentationScope{},
plog.NewLogRecord(),
pcommon.NewInstrumentationScope(),
rlogs.Resource(),
)

Expand Down
41 changes: 41 additions & 0 deletions processor/routingprocessor/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,47 @@ func TestLogsAreCorrectlySplitPerResourceAttributeWithOTTL(t *testing.T) {
})
}

// see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/26462
func TestLogsAttributeWithOTTLDoesNotCauseCrash(t *testing.T) {
// prepare
defaultExp := &mockLogsExporter{}
firstExp := &mockLogsExporter{}

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

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

l := plog.NewLogs()

rl := l.ResourceLogs().AppendEmpty()
rl.Resource().Attributes().PutInt("value", 1)
rl.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()

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

// test
// before #26464, this would panic
require.NoError(t, exp.ConsumeLogs(context.Background(), l))

// verify
assert.Len(t, defaultExp.AllLogs(), 1)
assert.Len(t, firstExp.AllLogs(), 0)
}

type mockLogsExporter struct {
mockComponent
consumertest.LogsSink
Expand Down
6 changes: 3 additions & 3 deletions processor/routingprocessor/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ func (p *metricsProcessor) route(ctx context.Context, tm pmetric.Metrics) error
rmetrics := tm.ResourceMetrics().At(i)
mtx := ottldatapoint.NewTransformContext(
nil,
pmetric.Metric{},
pmetric.MetricSlice{},
pcommon.InstrumentationScope{},
pmetric.NewMetric(),
pmetric.NewMetricSlice(),
pcommon.NewInstrumentationScope(),
rmetrics.Resource(),
)

Expand Down
43 changes: 43 additions & 0 deletions processor/routingprocessor/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,46 @@ func TestMetricsAreCorrectlySplitPerResourceAttributeRoutingWithOTTL(t *testing.
assert.Equal(t, attr.Double(), float64(-1.0))
})
}

// see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/26462
func TestMetricsAttributeWithOTTLDoesNotCauseCrash(t *testing.T) {
// prepare
defaultExp := &mockMetricsExporter{}
firstExp := &mockMetricsExporter{}

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

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

m := pmetric.NewMetrics()

rm := m.ResourceMetrics().AppendEmpty()
rm.Resource().Attributes().PutInt("value", 1)
metric := rm.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty()
metric.SetEmptyGauge()
metric.SetName("cpu")

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

// test
// before #26464, this would panic
require.NoError(t, exp.ConsumeMetrics(context.Background(), m))

// verify
assert.Len(t, defaultExp.AllMetrics(), 1)
assert.Len(t, firstExp.AllMetrics(), 0)
}
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
42 changes: 42 additions & 0 deletions processor/routingprocessor/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,48 @@ func TestTracesAreCorrectlySplitPerResourceAttributeWithOTTL(t *testing.T) {
})
}

// see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/26462
func TestTracesAttributeWithOTTLDoesNotCauseCrash(t *testing.T) {
// prepare
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))

// test
// before #26464, this would panic
require.NoError(t, exp.ConsumeTraces(context.Background(), tr))

// verify
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 d0e400d

Please sign in to comment.