forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
278 lines (268 loc) · 8.49 KB
/
config_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otelcol
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/zap/zapcore"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/service"
"go.opentelemetry.io/collector/service/pipelines"
"go.opentelemetry.io/collector/service/telemetry"
)
var (
errInvalidRecvConfig = errors.New("invalid receiver config")
errInvalidExpConfig = errors.New("invalid exporter config")
errInvalidProcConfig = errors.New("invalid processor config")
errInvalidConnConfig = errors.New("invalid connector config")
errInvalidExtConfig = errors.New("invalid extension config")
)
type errConfig struct {
validateErr error
}
func (c *errConfig) Validate() error {
return c.validateErr
}
func TestConfigValidate(t *testing.T) {
var testCases = []struct {
name string // test case name (also file name containing config yaml)
cfgFn func() *Config
expected error
}{
{
name: "valid",
cfgFn: generateConfig,
expected: nil,
},
{
name: "custom-service-telemetrySettings-encoding",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Service.Telemetry.Logs.Encoding = "json"
return cfg
},
expected: nil,
},
{
name: "missing-exporters",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Exporters = nil
return cfg
},
expected: errMissingExporters,
},
{
name: "missing-receivers",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Receivers = nil
return cfg
},
expected: errMissingReceivers,
},
{
name: "invalid-extension-reference",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Service.Extensions = append(cfg.Service.Extensions, component.NewIDWithName("nop", "2"))
return cfg
},
expected: errors.New(`service::extensions: references extension "nop/2" which is not configured`),
},
{
name: "invalid-receiver-reference",
cfgFn: func() *Config {
cfg := generateConfig()
pipe := cfg.Service.Pipelines[component.NewID("traces")]
pipe.Receivers = append(pipe.Receivers, component.NewIDWithName("nop", "2"))
return cfg
},
expected: errors.New(`service::pipelines::traces: references receiver "nop/2" which is not configured`),
},
{
name: "invalid-processor-reference",
cfgFn: func() *Config {
cfg := generateConfig()
pipe := cfg.Service.Pipelines[component.NewID("traces")]
pipe.Processors = append(pipe.Processors, component.NewIDWithName("nop", "2"))
return cfg
},
expected: errors.New(`service::pipelines::traces: references processor "nop/2" which is not configured`),
},
{
name: "invalid-exporter-reference",
cfgFn: func() *Config {
cfg := generateConfig()
pipe := cfg.Service.Pipelines[component.NewID("traces")]
pipe.Exporters = append(pipe.Exporters, component.NewIDWithName("nop", "2"))
return cfg
},
expected: errors.New(`service::pipelines::traces: references exporter "nop/2" which is not configured`),
},
{
name: "invalid-receiver-config",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Receivers[component.NewID("nop")] = &errConfig{
validateErr: errInvalidRecvConfig,
}
return cfg
},
expected: fmt.Errorf(`receivers::nop: %w`, errInvalidRecvConfig),
},
{
name: "invalid-exporter-config",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Exporters[component.NewID("nop")] = &errConfig{
validateErr: errInvalidExpConfig,
}
return cfg
},
expected: fmt.Errorf(`exporters::nop: %w`, errInvalidExpConfig),
},
{
name: "invalid-processor-config",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Processors[component.NewID("nop")] = &errConfig{
validateErr: errInvalidProcConfig,
}
return cfg
},
expected: fmt.Errorf(`processors::nop: %w`, errInvalidProcConfig),
},
{
name: "invalid-extension-config",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Extensions[component.NewID("nop")] = &errConfig{
validateErr: errInvalidExtConfig,
}
return cfg
},
expected: fmt.Errorf(`extensions::nop: %w`, errInvalidExtConfig),
},
{
name: "invalid-connector-config",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Connectors[component.NewIDWithName("nop", "conn")] = &errConfig{
validateErr: errInvalidConnConfig,
}
return cfg
},
expected: fmt.Errorf(`connectors::nop/conn: %w`, errInvalidConnConfig),
},
{
name: "ambiguous-connector-name-as-receiver",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Receivers[component.NewID("nop/2")] = &errConfig{}
cfg.Connectors[component.NewID("nop/2")] = &errConfig{}
pipe := cfg.Service.Pipelines[component.NewID("traces")]
pipe.Receivers = append(pipe.Receivers, component.NewIDWithName("nop", "2"))
pipe.Exporters = append(pipe.Exporters, component.NewIDWithName("nop", "2"))
return cfg
},
expected: errors.New(`connectors::nop/2: ambiguous ID: Found both "nop/2" receiver and "nop/2" connector. Change one of the components' IDs to eliminate ambiguity (e.g. rename "nop/2" connector to "nop/2/connector")`),
},
{
name: "ambiguous-connector-name-as-exporter",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Exporters[component.NewID("nop/2")] = &errConfig{}
cfg.Connectors[component.NewID("nop/2")] = &errConfig{}
pipe := cfg.Service.Pipelines[component.NewID("traces")]
pipe.Receivers = append(pipe.Receivers, component.NewIDWithName("nop", "2"))
pipe.Exporters = append(pipe.Exporters, component.NewIDWithName("nop", "2"))
return cfg
},
expected: errors.New(`connectors::nop/2: ambiguous ID: Found both "nop/2" exporter and "nop/2" connector. Change one of the components' IDs to eliminate ambiguity (e.g. rename "nop/2" connector to "nop/2/connector")`),
},
{
name: "invalid-connector-reference-as-receiver",
cfgFn: func() *Config {
cfg := generateConfig()
pipe := cfg.Service.Pipelines[component.NewID("traces")]
pipe.Receivers = append(pipe.Receivers, component.NewIDWithName("nop", "conn2"))
return cfg
},
expected: errors.New(`service::pipelines::traces: references receiver "nop/conn2" which is not configured`),
},
{
name: "invalid-connector-reference-as-receiver",
cfgFn: func() *Config {
cfg := generateConfig()
pipe := cfg.Service.Pipelines[component.NewID("traces")]
pipe.Exporters = append(pipe.Exporters, component.NewIDWithName("nop", "conn2"))
return cfg
},
expected: errors.New(`service::pipelines::traces: references exporter "nop/conn2" which is not configured`),
},
{
name: "invalid-service-config",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Service.Pipelines = nil
return cfg
},
expected: fmt.Errorf(`service::pipelines config validation failed: %w`, errors.New(`service must have at least one pipeline`)),
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
cfg := test.cfgFn()
assert.Equal(t, test.expected, cfg.Validate())
})
}
}
func generateConfig() *Config {
return &Config{
Receivers: map[component.ID]component.Config{
component.NewID("nop"): &errConfig{},
},
Exporters: map[component.ID]component.Config{
component.NewID("nop"): &errConfig{},
},
Processors: map[component.ID]component.Config{
component.NewID("nop"): &errConfig{},
},
Connectors: map[component.ID]component.Config{
component.NewIDWithName("nop", "conn"): &errConfig{},
},
Extensions: map[component.ID]component.Config{
component.NewID("nop"): &errConfig{},
},
Service: service.Config{
Telemetry: telemetry.Config{
Logs: telemetry.LogsConfig{
Level: zapcore.DebugLevel,
Development: true,
Encoding: "console",
DisableCaller: true,
DisableStacktrace: true,
OutputPaths: []string{"stderr", "./output-logs"},
ErrorOutputPaths: []string{"stderr", "./error-output-logs"},
InitialFields: map[string]any{"fieldKey": "filed-value"},
},
Metrics: telemetry.MetricsConfig{
Level: configtelemetry.LevelNormal,
Address: ":8080",
},
},
Extensions: []component.ID{component.NewID("nop")},
Pipelines: pipelines.Config{
component.NewID("traces"): {
Receivers: []component.ID{component.NewID("nop")},
Processors: []component.ID{component.NewID("nop")},
Exporters: []component.ID{component.NewID("nop")},
},
},
},
}
}