forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processors_test.go
290 lines (266 loc) · 8.55 KB
/
processors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Skip tests on Windows temporarily, see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/11451
//go:build !windows
// +build !windows
package main
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/memorylimiterprocessor"
"go.opentelemetry.io/collector/processor/processortest"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/attraction"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/attributesprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/remoteobserverprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourceprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/spanprocessor"
)
func TestDefaultProcessors(t *testing.T) {
t.Parallel()
allFactories, err := components()
require.NoError(t, err)
procFactories := allFactories.Processors
tests := []struct {
getConfigFn getProcessorConfigFn
processor component.Type
skipLifecycle bool
}{
{
processor: "attributes",
getConfigFn: func() component.Config {
cfg := procFactories["attributes"].CreateDefaultConfig().(*attributesprocessor.Config)
cfg.Actions = []attraction.ActionKeyValue{
{Key: "attribute1", Action: attraction.INSERT, Value: 123},
}
return cfg
},
},
{
processor: "batch",
},
{
processor: "datadog",
skipLifecycle: true, // requires external exporters to be configured to route data
},
{
processor: "deltatorate",
},
{
processor: "filter",
},
{
processor: "groupbyattrs",
},
{
processor: "groupbytrace",
},
{
processor: "k8sattributes",
skipLifecycle: true, // Requires a k8s API to communicate with
},
{
processor: "memory_limiter",
getConfigFn: func() component.Config {
cfg := procFactories["memory_limiter"].CreateDefaultConfig().(*memorylimiterprocessor.Config)
cfg.CheckInterval = 100 * time.Millisecond
cfg.MemoryLimitMiB = 1024 * 1024
return cfg
},
},
{
processor: "metricstransform",
},
{
processor: "experimental_metricsgeneration",
},
{
processor: "probabilistic_sampler",
},
{
processor: "resourcedetection",
},
{
processor: "resource",
getConfigFn: func() component.Config {
cfg := procFactories["resource"].CreateDefaultConfig().(*resourceprocessor.Config)
cfg.AttributesActions = []attraction.ActionKeyValue{
{Key: "attribute1", Action: attraction.INSERT, Value: 123},
}
return cfg
},
},
{
processor: "routing",
skipLifecycle: true, // Requires external exporters to be configured to route data
},
{
processor: "span",
getConfigFn: func() component.Config {
cfg := procFactories["span"].CreateDefaultConfig().(*spanprocessor.Config)
cfg.Rename.FromAttributes = []string{"test-key"}
return cfg
},
},
{
processor: "servicegraph",
skipLifecycle: true,
},
{
processor: "spanmetrics",
skipLifecycle: true, // Requires a running exporter to convert data to/from
},
{
processor: "cumulativetodelta",
},
{
processor: "tail_sampling",
},
{
processor: "transform",
},
{
processor: "redaction",
},
{
processor: "remoteobserver",
getConfigFn: func() component.Config {
cfg := procFactories["remoteobserver"].CreateDefaultConfig().(*remoteobserverprocessor.Config)
cfg.Endpoint = "localhost:0"
return cfg
},
},
{
processor: "sumologic",
},
}
assert.Equal(t, len(procFactories), len(tests), "All processors must be added to lifecycle tests")
for _, tt := range tests {
t.Run(string(tt.processor), func(t *testing.T) {
factory := procFactories[tt.processor]
assert.Equal(t, tt.processor, factory.Type())
t.Run("shutdown", func(t *testing.T) {
verifyProcessorShutdown(t, factory, tt.getConfigFn)
})
t.Run("lifecycle", func(t *testing.T) {
if tt.skipLifecycle {
t.SkipNow()
}
verifyProcessorLifecycle(t, factory, tt.getConfigFn)
})
})
}
}
// getProcessorConfigFn is used customize the configuration passed to the verification.
// This is used to change ports or provide values required but not provided by the
// default configuration.
type getProcessorConfigFn func() component.Config
// verifyProcessorLifecycle is used to test if a processor type can handle the typical
// lifecycle of a component. The getConfigFn parameter only need to be specified if
// the test can't be done with the default configuration for the component.
func verifyProcessorLifecycle(t *testing.T, factory processor.Factory, getConfigFn getProcessorConfigFn) {
ctx := context.Background()
host := newAssertNoErrorHost(t)
processorCreationSet := processortest.NewNopCreateSettings()
if getConfigFn == nil {
getConfigFn = factory.CreateDefaultConfig
}
createFns := map[component.DataType]createProcessorFn{
component.DataTypeLogs: wrapCreateLogsProc(factory),
component.DataTypeTraces: wrapCreateTracesProc(factory),
component.DataTypeMetrics: wrapCreateMetricsProc(factory),
}
for i := 0; i < 2; i++ {
procs := make(map[component.DataType]component.Component)
for dataType, createFn := range createFns {
proc, err := createFn(ctx, processorCreationSet, getConfigFn())
if errors.Is(err, component.ErrDataTypeIsNotSupported) {
continue
}
require.NoError(t, err)
procs[dataType] = proc
require.NoError(t, proc.Start(ctx, host))
}
for dataType, proc := range procs {
assert.NotPanics(t, func() {
switch dataType {
case component.DataTypeLogs:
logsProc := proc.(processor.Logs)
logs := testdata.GenerateLogsManyLogRecordsSameResource(2)
if !logsProc.Capabilities().MutatesData {
logs.MarkReadOnly()
}
assert.NoError(t, logsProc.ConsumeLogs(ctx, logs))
case component.DataTypeMetrics:
metricsProc := proc.(processor.Metrics)
metrics := testdata.GenerateMetricsTwoMetrics()
if !metricsProc.Capabilities().MutatesData {
metrics.MarkReadOnly()
}
assert.NoError(t, metricsProc.ConsumeMetrics(ctx, metrics))
case component.DataTypeTraces:
tracesProc := proc.(processor.Traces)
traces := testdata.GenerateTracesTwoSpansSameResource()
if !tracesProc.Capabilities().MutatesData {
traces.MarkReadOnly()
}
assert.NoError(t, tracesProc.ConsumeTraces(ctx, traces))
}
})
require.NoError(t, proc.Shutdown(ctx))
}
}
}
// verifyProcessorShutdown is used to test if a processor type can be shutdown without being started first.
// We disregard errors being returned by shutdown, we're just making sure the processors don't panic.
func verifyProcessorShutdown(tb testing.TB, factory processor.Factory, getConfigFn getProcessorConfigFn) {
ctx := context.Background()
processorCreationSet := processortest.NewNopCreateSettings()
if getConfigFn == nil {
getConfigFn = factory.CreateDefaultConfig
}
createFns := []createProcessorFn{
wrapCreateLogsProc(factory),
wrapCreateTracesProc(factory),
wrapCreateMetricsProc(factory),
}
for _, createFn := range createFns {
p, err := createFn(ctx, processorCreationSet, getConfigFn())
if errors.Is(err, component.ErrDataTypeIsNotSupported) {
continue
}
if p == nil {
continue
}
assert.NotPanics(tb, func() {
_ = p.Shutdown(ctx)
})
}
}
type createProcessorFn func(
ctx context.Context,
set processor.CreateSettings,
cfg component.Config,
) (component.Component, error)
func wrapCreateLogsProc(factory processor.Factory) createProcessorFn {
return func(ctx context.Context, set processor.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateLogsProcessor(ctx, set, cfg, consumertest.NewNop())
}
}
func wrapCreateMetricsProc(factory processor.Factory) createProcessorFn {
return func(ctx context.Context, set processor.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateMetricsProcessor(ctx, set, cfg, consumertest.NewNop())
}
}
func wrapCreateTracesProc(factory processor.Factory) createProcessorFn {
return func(ctx context.Context, set processor.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateTracesProcessor(ctx, set, cfg, consumertest.NewNop())
}
}