forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processors_test.go
255 lines (229 loc) · 7.52 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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Skip tests on Windows temporarily, see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/11451
//go:build !windows
// +build !windows
package components
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/processor/attributesprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourceprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/spanprocessor"
)
func TestDefaultProcessors(t *testing.T) {
allFactories, err := Components()
require.NoError(t, err)
procFactories := allFactories.Processors
tests := []struct {
processor component.Type
getConfigFn getProcessorConfigFn
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",
},
}
assert.Len(t, tests, len(procFactories), "All processors MUST be added to lifecycle tests")
for _, tt := range tests {
t.Run(string(tt.processor), func(t *testing.T) {
factory, ok := procFactories[tt.processor]
require.True(t, ok)
assert.Equal(t, tt.processor, factory.Type())
verifyProcessorShutdown(t, factory, tt.getConfigFn)
if !tt.skipLifecycle {
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 := []createProcessorFn{
wrapCreateLogsProc(factory),
wrapCreateTracesProc(factory),
wrapCreateMetricsProc(factory),
}
for _, createFn := range createFns {
firstExp, err := createFn(ctx, processorCreationSet, getConfigFn())
if errors.Is(err, component.ErrDataTypeIsNotSupported) {
continue
}
require.NoError(t, err)
require.NoError(t, firstExp.Start(ctx, host))
require.NoError(t, firstExp.Shutdown(ctx))
secondExp, err := createFn(ctx, processorCreationSet, getConfigFn())
require.NoError(t, err)
require.NoError(t, secondExp.Start(ctx, host))
require.NoError(t, secondExp.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())
}
}