| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package processorhelper |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"sync" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
"go.opentelemetry.io/otel/attribute" |
| 15 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 16 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 17 |
|
| 18 |
"go.opentelemetry.io/collector/component" |
| 19 |
"go.opentelemetry.io/collector/component/componenttest" |
| 20 |
"go.opentelemetry.io/collector/consumer" |
| 21 |
"go.opentelemetry.io/collector/consumer/consumertest" |
| 22 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 23 |
"go.opentelemetry.io/collector/processor/processorhelper/internal/metadatatest" |
| 24 |
"go.opentelemetry.io/collector/processor/processortest" |
| 25 |
) |
| 26 |
|
| 27 |
var testTracesCfg = struct{}{} |
| 28 |
|
| 29 |
func TestNewTraces(t *testing.T) { |
| 30 |
tp, err := NewTraces(context.Background(), processortest.NewNopSettings(processortest.NopType), &testTracesCfg, consumertest.NewNop(), newTestTProcessor(nil)) |
| 31 |
require.NoError(t, err) |
| 32 |
|
| 33 |
assert.True(t, tp.Capabilities().MutatesData) |
| 34 |
assert.NoError(t, tp.Start(context.Background(), componenttest.NewNopHost())) |
| 35 |
assert.NoError(t, tp.ConsumeTraces(context.Background(), ptrace.NewTraces())) |
| 36 |
assert.NoError(t, tp.Shutdown(context.Background())) |
| 37 |
} |
| 38 |
|
| 39 |
func TestNewTraces_WithOptions(t *testing.T) { |
| 40 |
want := errors.New("my_error") |
| 41 |
tp, err := NewTraces(context.Background(), processortest.NewNopSettings(processortest.NopType), &testTracesCfg, consumertest.NewNop(), newTestTProcessor(nil), |
| 42 |
WithStart(func(context.Context, component.Host) error { return want }), |
| 43 |
WithShutdown(func(context.Context) error { return want }), |
| 44 |
WithCapabilities(consumer.Capabilities{MutatesData: false})) |
| 45 |
require.NoError(t, err) |
| 46 |
|
| 47 |
assert.Equal(t, want, tp.Start(context.Background(), componenttest.NewNopHost())) |
| 48 |
assert.Equal(t, want, tp.Shutdown(context.Background())) |
| 49 |
assert.False(t, tp.Capabilities().MutatesData) |
| 50 |
} |
| 51 |
|
| 52 |
func TestNewTraces_NilRequiredFields(t *testing.T) { |
| 53 |
_, err := NewTraces(context.Background(), processortest.NewNopSettings(processortest.NopType), &testTracesCfg, consumertest.NewNop(), nil) |
| 54 |
assert.Error(t, err) |
| 55 |
} |
| 56 |
|
| 57 |
func TestNewTraces_ProcessTraceError(t *testing.T) { |
| 58 |
want := errors.New("my_error") |
| 59 |
tp, err := NewTraces(context.Background(), processortest.NewNopSettings(processortest.NopType), &testTracesCfg, consumertest.NewNop(), newTestTProcessor(want)) |
| 60 |
require.NoError(t, err) |
| 61 |
assert.Equal(t, want, tp.ConsumeTraces(context.Background(), ptrace.NewTraces())) |
| 62 |
} |
| 63 |
|
| 64 |
func TestNewTraces_ProcessTracesErrSkipProcessingData(t *testing.T) { |
| 65 |
tp, err := NewTraces(context.Background(), processortest.NewNopSettings(processortest.NopType), &testTracesCfg, consumertest.NewNop(), newTestTProcessor(ErrSkipProcessingData)) |
| 66 |
require.NoError(t, err) |
| 67 |
assert.NoError(t, tp.ConsumeTraces(context.Background(), ptrace.NewTraces())) |
| 68 |
} |
| 69 |
|
| 70 |
func newTestTProcessor(retError error) ProcessTracesFunc { |
| 71 |
return func(_ context.Context, td ptrace.Traces) (ptrace.Traces, error) { |
| 72 |
return td, retError |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestTracesConcurrency(t *testing.T) { |
| 77 |
tracesFunc := func(_ context.Context, td ptrace.Traces) (ptrace.Traces, error) { |
| 78 |
return td, nil |
| 79 |
} |
| 80 |
|
| 81 |
incomingTraces := ptrace.NewTraces() |
| 82 |
incomingSpans := incomingTraces.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans() |
| 83 |
|
| 84 |
// Add 4 records to the incoming |
| 85 |
incomingSpans.AppendEmpty() |
| 86 |
incomingSpans.AppendEmpty() |
| 87 |
incomingSpans.AppendEmpty() |
| 88 |
incomingSpans.AppendEmpty() |
| 89 |
|
| 90 |
mp, err := NewTraces(context.Background(), processortest.NewNopSettings(processortest.NopType), &testLogsCfg, consumertest.NewNop(), tracesFunc) |
| 91 |
require.NoError(t, err) |
| 92 |
assert.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost())) |
| 93 |
|
| 94 |
var wg sync.WaitGroup |
| 95 |
for range 10 { |
| 96 |
wg.Add(1) |
| 97 |
go func() { |
| 98 |
defer wg.Done() |
| 99 |
for range 10000 { |
| 100 |
assert.NoError(t, mp.ConsumeTraces(context.Background(), incomingTraces)) |
| 101 |
} |
| 102 |
}() |
| 103 |
} |
| 104 |
wg.Wait() |
| 105 |
assert.NoError(t, mp.Shutdown(context.Background())) |
| 106 |
} |
| 107 |
|
| 108 |
func TestTraces_RecordInOut(t *testing.T) { |
| 109 |
// Regardless of how many spans are ingested, emit just one |
| 110 |
mockAggregate := func(_ context.Context, _ ptrace.Traces) (ptrace.Traces, error) { |
| 111 |
td := ptrace.NewTraces() |
| 112 |
td.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty() |
| 113 |
return td, nil |
| 114 |
} |
| 115 |
|
| 116 |
incomingTraces := ptrace.NewTraces() |
| 117 |
incomingSpans := incomingTraces.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans() |
| 118 |
|
| 119 |
// Add 4 records to the incoming |
| 120 |
incomingSpans.AppendEmpty() |
| 121 |
incomingSpans.AppendEmpty() |
| 122 |
incomingSpans.AppendEmpty() |
| 123 |
incomingSpans.AppendEmpty() |
| 124 |
|
| 125 |
tel := componenttest.NewTelemetry() |
| 126 |
tp, err := NewTraces(context.Background(), newSettings(tel), &testLogsCfg, consumertest.NewNop(), mockAggregate) |
| 127 |
require.NoError(t, err) |
| 128 |
|
| 129 |
assert.NoError(t, tp.Start(context.Background(), componenttest.NewNopHost())) |
| 130 |
assert.NoError(t, tp.ConsumeTraces(context.Background(), incomingTraces)) |
| 131 |
assert.NoError(t, tp.Shutdown(context.Background())) |
| 132 |
|
| 133 |
metadatatest.AssertEqualProcessorIncomingItems(t, tel, |
| 134 |
[]metricdata.DataPoint[int64]{ |
| 135 |
{ |
| 136 |
Value: 4, |
| 137 |
Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "traces")), |
| 138 |
}, |
| 139 |
}, metricdatatest.IgnoreTimestamp()) |
| 140 |
metadatatest.AssertEqualProcessorOutgoingItems(t, tel, |
| 141 |
[]metricdata.DataPoint[int64]{ |
| 142 |
{ |
| 143 |
Value: 1, |
| 144 |
Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "traces")), |
| 145 |
}, |
| 146 |
}, metricdatatest.IgnoreTimestamp()) |
| 147 |
} |
| 148 |
|
| 149 |
func TestTraces_RecordIn_ErrorOut(t *testing.T) { |
| 150 |
// Regardless of input, return error |
| 151 |
mockErr := func(_ context.Context, _ ptrace.Traces) (ptrace.Traces, error) { |
| 152 |
return ptrace.NewTraces(), errors.New("fake") |
| 153 |
} |
| 154 |
|
| 155 |
incomingTraces := ptrace.NewTraces() |
| 156 |
incomingSpans := incomingTraces.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans() |
| 157 |
|
| 158 |
// Add 4 records to the incoming |
| 159 |
incomingSpans.AppendEmpty() |
| 160 |
incomingSpans.AppendEmpty() |
| 161 |
incomingSpans.AppendEmpty() |
| 162 |
incomingSpans.AppendEmpty() |
| 163 |
|
| 164 |
tel := componenttest.NewTelemetry() |
| 165 |
tp, err := NewTraces(context.Background(), newSettings(tel), &testLogsCfg, consumertest.NewNop(), mockErr) |
| 166 |
require.NoError(t, err) |
| 167 |
|
| 168 |
require.NoError(t, tp.Start(context.Background(), componenttest.NewNopHost())) |
| 169 |
require.Error(t, tp.ConsumeTraces(context.Background(), incomingTraces)) |
| 170 |
require.NoError(t, tp.Shutdown(context.Background())) |
| 171 |
|
| 172 |
metadatatest.AssertEqualProcessorIncomingItems(t, tel, |
| 173 |
[]metricdata.DataPoint[int64]{ |
| 174 |
{ |
| 175 |
Value: 4, |
| 176 |
Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "traces")), |
| 177 |
}, |
| 178 |
}, metricdatatest.IgnoreTimestamp()) |
| 179 |
metadatatest.AssertEqualProcessorOutgoingItems(t, tel, |
| 180 |
[]metricdata.DataPoint[int64]{ |
| 181 |
{ |
| 182 |
Value: 0, |
| 183 |
Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "traces")), |
| 184 |
}, |
| 185 |
}, metricdatatest.IgnoreTimestamp()) |
| 186 |
} |
| 187 |
|
| 188 |
func TestTraces_ProcessInternalDuration(t *testing.T) { |
| 189 |
mockAggregate := func(_ context.Context, _ ptrace.Traces) (ptrace.Traces, error) { |
| 190 |
td := ptrace.NewTraces() |
| 191 |
td.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty() |
| 192 |
return td, nil |
| 193 |
} |
| 194 |
|
| 195 |
incomingTraces := ptrace.NewTraces() |
| 196 |
|
| 197 |
tel := componenttest.NewTelemetry() |
| 198 |
tp, err := NewTraces(context.Background(), newSettings(tel), &testLogsCfg, consumertest.NewNop(), mockAggregate) |
| 199 |
require.NoError(t, err) |
| 200 |
|
| 201 |
assert.NoError(t, tp.Start(context.Background(), componenttest.NewNopHost())) |
| 202 |
assert.NoError(t, tp.ConsumeTraces(context.Background(), incomingTraces)) |
| 203 |
assert.NoError(t, tp.Shutdown(context.Background())) |
| 204 |
|
| 205 |
metadatatest.AssertEqualProcessorInternalDuration(t, tel, |
| 206 |
[]metricdata.HistogramDataPoint[float64]{ |
| 207 |
{ |
| 208 |
Count: 1, |
| 209 |
BucketCounts: []uint64{1}, |
| 210 |
Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "traces")), |
| 211 |
}, |
| 212 |
}, metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue()) |
| 213 |
} |