Debian Codegraph @

otelconf/x/config_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package x
5
6 import (
7 "encoding/json"
8 "fmt"
9 "os"
10 "path/filepath"
11 "testing"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 lognoop "go.opentelemetry.io/otel/log/noop"
16 metricnoop "go.opentelemetry.io/otel/metric/noop"
17 "go.opentelemetry.io/otel/propagation"
18 sdklog "go.opentelemetry.io/otel/sdk/log"
19 sdkmetric "go.opentelemetry.io/otel/sdk/metric"
20 sdktrace "go.opentelemetry.io/otel/sdk/trace"
21 tracenoop "go.opentelemetry.io/otel/trace/noop"
22 "go.yaml.in/yaml/v3"
23 )
24
25 func TestUnmarshalPushMetricExporterInvalidData(t *testing.T) {
26 cl := PushMetricExporter{}
27 err := cl.UnmarshalJSON([]byte(`{:2000}`))
28 assert.ErrorIs(t, err, newErrUnmarshal(&PushMetricExporter{}))
29
30 cl = PushMetricExporter{}
31 err = yaml.Unmarshal([]byte("console: !!str str"), &cl)
32 assert.ErrorIs(t, err, newErrUnmarshal(&PushMetricExporter{}))
33 }
34
35 func TestUnmarshalLogRecordExporterInvalidData(t *testing.T) {
36 cl := LogRecordExporter{}
37 err := cl.UnmarshalJSON([]byte(`{:2000}`))
38 assert.ErrorIs(t, err, newErrUnmarshal(&LogRecordExporter{}))
39
40 cl = LogRecordExporter{}
41 err = cl.UnmarshalJSON([]byte(`{"console":2000}`))
42 assert.ErrorIs(t, err, newErrUnmarshal(&ConsoleExporter{}))
43
44 cl = LogRecordExporter{}
45 err = yaml.Unmarshal([]byte("console: !!str str"), &cl)
46 assert.ErrorIs(t, err, newErrUnmarshal(&LogRecordExporter{}))
47 }
48
49 func TestUnmarshalSpanExporterInvalidData(t *testing.T) {
50 cl := SpanExporter{}
51 err := cl.UnmarshalJSON([]byte(`{:2000}`))
52 assert.ErrorIs(t, err, newErrUnmarshal(&SpanExporter{}))
53
54 cl = SpanExporter{}
55 err = cl.UnmarshalJSON([]byte(`{"console":2000}`))
56 assert.ErrorIs(t, err, newErrUnmarshal(&ConsoleExporter{}))
57
58 cl = SpanExporter{}
59 err = yaml.Unmarshal([]byte("console: !!str str"), &cl)
60 assert.ErrorIs(t, err, newErrUnmarshal(&SpanExporter{}))
61 }
62
63 func TestUnmarshalTextMapPropagator(t *testing.T) {
64 for _, tt := range []struct {
65 name string
66 yamlConfig []byte
67 jsonConfig []byte
68 wantErrT error
69 wantTextMapPropagator TextMapPropagator
70 }{
71 {
72 name: "valid with b3 propagator",
73 jsonConfig: []byte(`{"b3":{}}`),
74 yamlConfig: []byte("b3: {}\n"),
75 wantTextMapPropagator: TextMapPropagator{B3: B3Propagator{}},
76 },
77 {
78 name: "valid with all propagators",
79 jsonConfig: []byte(`{"b3":{},"b3multi":{},"baggage":{},"jaeger":{},"ottrace":{},"tracecontext":{}}`),
80 yamlConfig: []byte("b3: {}\nb3multi: {}\nbaggage: {}\njaeger: {}\nottrace: {}\ntracecontext: {}\n"),
81 wantTextMapPropagator: TextMapPropagator{
82 B3: B3Propagator{},
83 B3Multi: B3MultiPropagator{},
84 Baggage: BaggagePropagator{},
85 Jaeger: JaegerPropagator{},
86 Ottrace: OpenTracingPropagator{},
87 Tracecontext: TraceContextPropagator{},
88 },
89 },
90 {
91 name: "valid with all propagators nil",
92 jsonConfig: []byte(`{"b3":null,"b3multi":null,"baggage":null,"jaeger":null,"ottrace":null,"tracecontext":null}`),
93 yamlConfig: []byte("b3:\nb3multi:\nbaggage:\njaeger:\nottrace:\ntracecontext:\n"),
94 wantTextMapPropagator: TextMapPropagator{
95 B3: B3Propagator{},
96 B3Multi: B3MultiPropagator{},
97 Baggage: BaggagePropagator{},
98 Jaeger: JaegerPropagator{},
99 Ottrace: OpenTracingPropagator{},
100 Tracecontext: TraceContextPropagator{},
101 },
102 },
103 {
104 name: "invalid b3 data",
105 jsonConfig: []byte(`{"b3":2000}`),
106 yamlConfig: []byte("b3: !!str str"),
107 wantErrT: newErrUnmarshal(&TextMapPropagator{}),
108 },
109 {
110 name: "invalid b3multi data",
111 jsonConfig: []byte(`{"b3multi":2000}`),
112 yamlConfig: []byte("b3multi: !!str str"),
113 wantErrT: newErrUnmarshal(&TextMapPropagator{}),
114 },
115 {
116 name: "invalid baggage data",
117 jsonConfig: []byte(`{"baggage":2000}`),
118 yamlConfig: []byte("baggage: !!str str"),
119 wantErrT: newErrUnmarshal(&TextMapPropagator{}),
120 },
121 {
122 name: "invalid jaeger data",
123 jsonConfig: []byte(`{"jaeger":2000}`),
124 yamlConfig: []byte("jaeger: !!str str"),
125 wantErrT: newErrUnmarshal(&TextMapPropagator{}),
126 },
127 {
128 name: "invalid ottrace data",
129 jsonConfig: []byte(`{"ottrace":2000}`),
130 yamlConfig: []byte("ottrace: !!str str"),
131 wantErrT: newErrUnmarshal(&TextMapPropagator{}),
132 },
133 {
134 name: "invalid tracecontext data",
135 jsonConfig: []byte(`{"tracecontext":2000}`),
136 yamlConfig: []byte("tracecontext: !!str str"),
137 wantErrT: newErrUnmarshal(&TextMapPropagator{}),
138 },
139 } {
140 t.Run(tt.name, func(t *testing.T) {
141 cl := TextMapPropagator{}
142 err := cl.UnmarshalJSON(tt.jsonConfig)
143 assert.ErrorIs(t, err, tt.wantErrT)
144 assert.Equal(t, tt.wantTextMapPropagator, cl)
145
146 cl = TextMapPropagator{}
147 err = yaml.Unmarshal(tt.yamlConfig, &cl)
148 assert.ErrorIs(t, err, tt.wantErrT)
149 assert.Equal(t, tt.wantTextMapPropagator, cl)
150 })
151 }
152 }
153
154 func TestUnmarshalSimpleLogRecordProcessor(t *testing.T) {
155 for _, tt := range []struct {
156 name string
157 yamlConfig []byte
158 jsonConfig []byte
159 wantErrT error
160 wantExporter LogRecordExporter
161 }{
162 {
163 name: "valid with console exporter",
164 jsonConfig: []byte(`{"exporter":{"console":{}}}`),
165 yamlConfig: []byte("exporter:\n console: {}"),
166 wantExporter: LogRecordExporter{Console: ConsoleExporter{}},
167 },
168 {
169 name: "valid with null console exporter",
170 jsonConfig: []byte(`{"exporter":{"console":null}}`),
171 yamlConfig: []byte("exporter:\n console:\n"),
172 wantExporter: LogRecordExporter{Console: ConsoleExporter{}},
173 },
174 {
175 name: "missing required exporter field",
176 jsonConfig: []byte(`{}`),
177 yamlConfig: []byte("{}"),
178 wantErrT: newErrRequired(&SimpleLogRecordProcessor{}, "exporter"),
179 },
180 {
181 name: "invalid data",
182 jsonConfig: []byte(`{:2000}`),
183 yamlConfig: []byte("exporter:\n console: []"),
184 wantErrT: newErrUnmarshal(&SimpleLogRecordProcessor{}),
185 },
186 } {
187 t.Run(tt.name, func(t *testing.T) {
188 cl := SimpleLogRecordProcessor{}
189 err := cl.UnmarshalJSON(tt.jsonConfig)
190 assert.ErrorIs(t, err, tt.wantErrT)
191 assert.Equal(t, tt.wantExporter, cl.Exporter)
192
193 cl = SimpleLogRecordProcessor{}
194 err = yaml.Unmarshal(tt.yamlConfig, &cl)
195 assert.ErrorIs(t, err, tt.wantErrT)
196 assert.Equal(t, tt.wantExporter, cl.Exporter)
197 })
198 }
199 }
200
201 func TestUnmarshalSimpleSpanProcessor(t *testing.T) {
202 for _, tt := range []struct {
203 name string
204 yamlConfig []byte
205 jsonConfig []byte
206 wantErrT error
207 wantExporter SpanExporter
208 }{
209 {
210 name: "valid with null console exporter",
211 jsonConfig: []byte(`{"exporter":{"console":null}}`),
212 yamlConfig: []byte("exporter:\n console:\n"),
213 wantExporter: SpanExporter{Console: ConsoleExporter{}},
214 },
215 {
216 name: "valid with console exporter",
217 jsonConfig: []byte(`{"exporter":{"console":{}}}`),
218 yamlConfig: []byte("exporter:\n console: {}"),
219 wantExporter: SpanExporter{Console: ConsoleExporter{}},
220 },
221 {
222 name: "missing required exporter field",
223 jsonConfig: []byte(`{}`),
224 yamlConfig: []byte("{}"),
225 wantErrT: newErrRequired(&SimpleSpanProcessor{}, "exporter"),
226 },
227 {
228 name: "invalid data",
229 jsonConfig: []byte(`{:2000}`),
230 yamlConfig: []byte("exporter:\n console: []"),
231 wantErrT: newErrUnmarshal(&SimpleSpanProcessor{}),
232 },
233 } {
234 t.Run(tt.name, func(t *testing.T) {
235 cl := SimpleSpanProcessor{}
236 err := cl.UnmarshalJSON(tt.jsonConfig)
237 assert.ErrorIs(t, err, tt.wantErrT)
238 assert.Equal(t, tt.wantExporter, cl.Exporter)
239
240 cl = SimpleSpanProcessor{}
241 err = yaml.Unmarshal(tt.yamlConfig, &cl)
242 assert.ErrorIs(t, err, tt.wantErrT)
243 assert.Equal(t, tt.wantExporter, cl.Exporter)
244 })
245 }
246 }
247
248 func TestUnmarshalBatchLogRecordProcessor(t *testing.T) {
249 for _, tt := range []struct {
250 name string
251 yamlConfig []byte
252 jsonConfig []byte
253 wantErrT error
254 wantExporter LogRecordExporter
255 }{
256 {
257 name: "valid with console exporter",
258 jsonConfig: []byte(`{"exporter":{"console":{}}}`),
259 yamlConfig: []byte("exporter:\n console: {}"),
260 wantExporter: LogRecordExporter{Console: ConsoleExporter{}},
261 },
262 {
263 name: "valid with null console exporter",
264 jsonConfig: []byte(`{"exporter":{"console":null}}`),
265 yamlConfig: []byte("exporter:\n console:\n"),
266 wantExporter: LogRecordExporter{Console: ConsoleExporter{}},
267 },
268 {
269 name: "valid with all fields positive",
270 jsonConfig: []byte(`{"exporter":{"console":{}},"export_timeout":5000,"max_export_batch_size":512,"max_queue_size":2048,"schedule_delay":1000}`),
271 yamlConfig: []byte("exporter:\n console: {}\nexport_timeout: 5000\nmax_export_batch_size: 512\nmax_queue_size: 2048\nschedule_delay: 1000"),
272 wantExporter: LogRecordExporter{Console: ConsoleExporter{}},
273 },
274 {
275 name: "valid with zero export_timeout",
276 jsonConfig: []byte(`{"exporter":{"console":{}},"export_timeout":0}`),
277 yamlConfig: []byte("exporter:\n console: {}\nexport_timeout: 0"),
278 wantExporter: LogRecordExporter{Console: ConsoleExporter{}},
279 },
280 {
281 name: "valid with zero schedule_delay",
282 jsonConfig: []byte(`{"exporter":{"console":{}},"schedule_delay":0}`),
283 yamlConfig: []byte("exporter:\n console: {}\nschedule_delay: 0"),
284 wantExporter: LogRecordExporter{Console: ConsoleExporter{}},
285 },
286 {
287 name: "missing required exporter field",
288 jsonConfig: []byte(`{}`),
289 yamlConfig: []byte("{}"),
290 wantErrT: newErrRequired(&BatchLogRecordProcessor{}, "exporter"),
291 },
292 {
293 name: "invalid data",
294 jsonConfig: []byte(`{:2000}`),
295 yamlConfig: []byte("exporter:\n console: {}\nexport_timeout: !!str str"),
296 wantErrT: newErrUnmarshal(&BatchLogRecordProcessor{}),
297 },
298 {
299 name: "invalid export_timeout negative",
300 jsonConfig: []byte(`{"exporter":{"console":{}},"export_timeout":-1}`),
301 yamlConfig: []byte("exporter:\n console: {}\nexport_timeout: -1"),
302 wantErrT: newErrGreaterOrEqualZero("export_timeout"),
303 },
304 {
305 name: "invalid max_export_batch_size zero",
306 jsonConfig: []byte(`{"exporter":{"console":{}},"max_export_batch_size":0}`),
307 yamlConfig: []byte("exporter:\n console: {}\nmax_export_batch_size: 0"),
308 wantErrT: newErrGreaterThanZero("max_export_batch_size"),
309 },
310 {
311 name: "invalid max_export_batch_size negative",
312 jsonConfig: []byte(`{"exporter":{"console":{}},"max_export_batch_size":-1}`),
313 yamlConfig: []byte("exporter:\n console: {}\nmax_export_batch_size: -1"),
314 wantErrT: newErrGreaterThanZero("max_export_batch_size"),
315 },
316 {
317 name: "invalid max_queue_size zero",
318 jsonConfig: []byte(`{"exporter":{"console":{}},"max_queue_size":0}`),
319 yamlConfig: []byte("exporter:\n console: {}\nmax_queue_size: 0"),
320 wantErrT: newErrGreaterThanZero("max_queue_size"),
321 },
322 {
323 name: "invalid max_queue_size negative",
324 jsonConfig: []byte(`{"exporter":{"console":{}},"max_queue_size":-1}`),
325 yamlConfig: []byte("exporter:\n console: {}\nmax_queue_size: -1"),
326 wantErrT: newErrGreaterThanZero("max_queue_size"),
327 },
328 {
329 name: "invalid schedule_delay negative",
330 jsonConfig: []byte(`{"exporter":{"console":{}},"schedule_delay":-1}`),
331 yamlConfig: []byte("exporter:\n console: {}\nschedule_delay: -1"),
332 wantErrT: newErrGreaterOrEqualZero("schedule_delay"),
333 },
334 } {
335 t.Run(tt.name, func(t *testing.T) {
336 cl := BatchLogRecordProcessor{}
337 err := cl.UnmarshalJSON(tt.jsonConfig)
338 assert.ErrorIs(t, err, tt.wantErrT)
339 assert.Equal(t, tt.wantExporter, cl.Exporter)
340
341 cl = BatchLogRecordProcessor{}
342 err = yaml.Unmarshal(tt.yamlConfig, &cl)
343 assert.ErrorIs(t, err, tt.wantErrT)
344 assert.Equal(t, tt.wantExporter, cl.Exporter)
345 })
346 }
347 }
348
349 func TestNewSDK(t *testing.T) {
350 tests := []struct {
351 name string
352 cfg []ConfigurationOption
353 wantTracerProvider any
354 wantMeterProvider any
355 wantLoggerProvider any
356 wantPropagator any
357 wantErr error
358 wantShutdownErr error
359 }{
360 {
361 name: "no-configuration",
362 wantTracerProvider: tracenoop.NewTracerProvider(),
363 wantMeterProvider: metricnoop.NewMeterProvider(),
364 wantLoggerProvider: lognoop.NewLoggerProvider(),
365 wantPropagator: propagation.NewCompositeTextMapPropagator(),
366 },
367 {
368 name: "with-configuration",
369 cfg: []ConfigurationOption{
370 WithContext(t.Context()),
371 WithOpenTelemetryConfiguration(OpenTelemetryConfiguration{
372 TracerProvider: &TracerProvider{},
373 MeterProvider: &MeterProvider{},
374 LoggerProvider: &LoggerProvider{},
375 Propagator: &Propagator{},
376 }),
377 },
378 wantTracerProvider: &sdktrace.TracerProvider{},
379 wantMeterProvider: &sdkmetric.MeterProvider{},
380 wantLoggerProvider: &sdklog.LoggerProvider{},
381 wantPropagator: propagation.NewCompositeTextMapPropagator(),
382 },
383 {
384 name: "with-sdk-disabled",
385 cfg: []ConfigurationOption{
386 WithContext(t.Context()),
387 WithOpenTelemetryConfiguration(OpenTelemetryConfiguration{
388 Disabled: ptr(true),
389 TracerProvider: &TracerProvider{},
390 MeterProvider: &MeterProvider{},
391 LoggerProvider: &LoggerProvider{},
392 }),
393 },
394 wantTracerProvider: tracenoop.NewTracerProvider(),
395 wantMeterProvider: metricnoop.NewMeterProvider(),
396 wantLoggerProvider: lognoop.NewLoggerProvider(),
397 wantPropagator: propagation.NewCompositeTextMapPropagator(),
398 },
399 }
400 for _, tt := range tests {
401 sdk, err := NewSDK(tt.cfg...)
402 require.Equal(t, tt.wantErr, err)
403 assert.IsType(t, tt.wantTracerProvider, sdk.TracerProvider())
404 assert.IsType(t, tt.wantMeterProvider, sdk.MeterProvider())
405 assert.IsType(t, tt.wantLoggerProvider, sdk.LoggerProvider())
406 assert.IsType(t, tt.wantPropagator, sdk.Propagator())
407 require.Equal(t, tt.wantShutdownErr, sdk.Shutdown(t.Context()))
408 }
409 }
410
411 func TestNewSDKWithEnvVar(t *testing.T) {
412 cfg := []ConfigurationOption{
413 WithContext(t.Context()),
414 WithOpenTelemetryConfiguration(OpenTelemetryConfiguration{
415 TracerProvider: nil,
416 }),
417 }
418 // test a non existent file
419 t.Setenv(envVarConfigFile, filepath.Join("..", "testdata", "file_missing.yaml"))
420 _, err := NewSDK(cfg...)
421 require.Error(t, err)
422 // test a file that causes a parse error
423 t.Setenv(envVarConfigFile, filepath.Join("..", "testdata", "v1.0.0_invalid_nil_name.yaml"))
424 _, err = NewSDK(cfg...)
425 require.Error(t, err)
426 require.ErrorIs(t, err, newErrRequired(&NameStringValuePair{}, "name"))
427 // test a valid file, error is returned from the SDK instantiation
428 t.Setenv(envVarConfigFile, filepath.Join("..", "testdata", "v1.0.0.yaml"))
429 _, err = NewSDK(cfg...)
430 require.ErrorIs(t, err, newErrInvalid("otlp_file/development"))
431 }
432
433 var v10OpenTelemetryConfig = OpenTelemetryConfiguration{
434 Disabled: ptr(false),
435 FileFormat: "1.0-rc.2",
436 AttributeLimits: &AttributeLimits{
437 AttributeCountLimit: ptr(128),
438 AttributeValueLengthLimit: ptr(4096),
439 },
440 InstrumentationDevelopment: &ExperimentalInstrumentation{
441 Cpp: ExperimentalLanguageSpecificInstrumentation{
442 "example": map[string]any{
443 "property": "value",
444 },
445 },
446 Dotnet: ExperimentalLanguageSpecificInstrumentation{
447 "example": map[string]any{
448 "property": "value",
449 },
450 },
451 Erlang: ExperimentalLanguageSpecificInstrumentation{
452 "example": map[string]any{
453 "property": "value",
454 },
455 },
456 General: &ExperimentalGeneralInstrumentation{
457 Http: &ExperimentalHttpInstrumentation{
458 Client: &ExperimentalHttpClientInstrumentation{
459 RequestCapturedHeaders: []string{"Content-Type", "Accept"},
460 ResponseCapturedHeaders: []string{"Content-Type", "Content-Encoding"},
461 },
462 Server: &ExperimentalHttpServerInstrumentation{
463 RequestCapturedHeaders: []string{"Content-Type", "Accept"},
464 ResponseCapturedHeaders: []string{"Content-Type", "Content-Encoding"},
465 },
466 },
467 Peer: &ExperimentalPeerInstrumentation{
468 ServiceMapping: []ExperimentalPeerServiceMapping{
469 {Peer: "1.2.3.4", Service: "FooService"},
470 {Peer: "2.3.4.5", Service: "BarService"},
471 },
472 },
473 },
474 Go: ExperimentalLanguageSpecificInstrumentation{
475 "example": map[string]any{
476 "property": "value",
477 },
478 },
479 Java: ExperimentalLanguageSpecificInstrumentation{
480 "example": map[string]any{
481 "property": "value",
482 },
483 },
484 Js: ExperimentalLanguageSpecificInstrumentation{
485 "example": map[string]any{
486 "property": "value",
487 },
488 },
489 Php: ExperimentalLanguageSpecificInstrumentation{
490 "example": map[string]any{
491 "property": "value",
492 },
493 },
494 Python: ExperimentalLanguageSpecificInstrumentation{
495 "example": map[string]any{
496 "property": "value",
497 },
498 },
499 Ruby: ExperimentalLanguageSpecificInstrumentation{
500 "example": map[string]any{
501 "property": "value",
502 },
503 },
504 Rust: ExperimentalLanguageSpecificInstrumentation{
505 "example": map[string]any{
506 "property": "value",
507 },
508 },
509 Swift: ExperimentalLanguageSpecificInstrumentation{
510 "example": map[string]any{
511 "property": "value",
512 },
513 },
514 },
515 LogLevel: ptr(SeverityNumberInfo),
516 LoggerProvider: &LoggerProvider{
517 LoggerConfiguratorDevelopment: &ExperimentalLoggerConfigurator{
518 DefaultConfig: &ExperimentalLoggerConfig{
519 Disabled: ptr(true),
520 },
521 Loggers: []ExperimentalLoggerMatcherAndConfig{
522 {
523 Config: ExperimentalLoggerConfig{
524 Disabled: ptr(false),
525 },
526 Name: "io.opentelemetry.contrib.*",
527 },
528 },
529 },
530 Limits: &LogRecordLimits{
531 AttributeCountLimit: ptr(128),
532 AttributeValueLengthLimit: ptr(4096),
533 },
534 Processors: []LogRecordProcessor{
535 {
536 Batch: &BatchLogRecordProcessor{
537 ExportTimeout: ptr(30000),
538 Exporter: LogRecordExporter{
539 OTLPHttp: &OTLPHttpExporter{
540 Tls: &HttpTls{
541 CaFile: ptr("testdata/ca.crt"),
542 CertFile: ptr("testdata/client.crt"),
543 KeyFile: ptr("testdata/client.key"),
544 },
545 Compression: ptr("gzip"),
546 Encoding: ptr(OTLPHttpEncodingProtobuf),
547 Endpoint: ptr("http://localhost:4318/v1/logs"),
548 Headers: []NameStringValuePair{
549 {Name: "api-key", Value: ptr("1234")},
550 },
551 HeadersList: ptr("api-key=1234"),
552 Timeout: ptr(10000),
553 },
554 },
555 MaxExportBatchSize: ptr(512),
556 MaxQueueSize: ptr(2048),
557 ScheduleDelay: ptr(5000),
558 },
559 },
560 {
561 Batch: &BatchLogRecordProcessor{
562 Exporter: LogRecordExporter{
563 OTLPGrpc: &OTLPGrpcExporter{
564 Tls: &GrpcTls{
565 CaFile: ptr("testdata/ca.crt"),
566 CertFile: ptr("testdata/client.crt"),
567 KeyFile: ptr("testdata/client.key"),
568 Insecure: ptr(false),
569 },
570 Compression: ptr("gzip"),
571 Endpoint: ptr("http://localhost:4317"),
572 Headers: []NameStringValuePair{
573 {Name: "api-key", Value: ptr("1234")},
574 },
575 HeadersList: ptr("api-key=1234"),
576 Timeout: ptr(10000),
577 },
578 },
579 },
580 },
581 {
582 Batch: &BatchLogRecordProcessor{
583 Exporter: LogRecordExporter{
584 OTLPFileDevelopment: &ExperimentalOTLPFileExporter{
585 OutputStream: ptr("file:///var/log/logs.jsonl"),
586 },
587 },
588 },
589 },
590 {
591 Batch: &BatchLogRecordProcessor{
592 Exporter: LogRecordExporter{
593 OTLPFileDevelopment: &ExperimentalOTLPFileExporter{
594 OutputStream: ptr("stdout"),
595 },
596 },
597 },
598 },
599 {
600 Simple: &SimpleLogRecordProcessor{
601 Exporter: LogRecordExporter{
602 Console: ConsoleExporter{},
603 },
604 },
605 },
606 },
607 },
608 MeterProvider: &MeterProvider{
609 ExemplarFilter: ptr(ExemplarFilter("trace_based")),
610 MeterConfiguratorDevelopment: &ExperimentalMeterConfigurator{
611 DefaultConfig: &ExperimentalMeterConfig{
612 Disabled: ptr(true),
613 },
614 Meters: []ExperimentalMeterMatcherAndConfig{
615 {
616 Config: ExperimentalMeterConfig{
617 Disabled: ptr(false),
618 },
619 Name: "io.opentelemetry.contrib.*",
620 },
621 },
622 },
623 Readers: []MetricReader{
624 {
625 Pull: &PullMetricReader{
626 Producers: []MetricProducer{
627 {
628 Opencensus: OpenCensusMetricProducer{},
629 },
630 },
631 CardinalityLimits: &CardinalityLimits{
632 Default: ptr(2000),
633 Counter: ptr(2000),
634 Gauge: ptr(2000),
635 Histogram: ptr(2000),
636 ObservableCounter: ptr(2000),
637 ObservableGauge: ptr(2000),
638 ObservableUpDownCounter: ptr(2000),
639 UpDownCounter: ptr(2000),
640 },
641 Exporter: PullMetricExporter{
642 PrometheusDevelopment: &ExperimentalPrometheusMetricExporter{
643 Host: ptr("localhost"),
644 Port: ptr(9464),
645 TranslationStrategy: ptr(ExperimentalPrometheusTranslationStrategyUnderscoreEscapingWithSuffixes),
646 WithResourceConstantLabels: &IncludeExclude{
647 Excluded: []string{"service.attr1"},
648 Included: []string{"service*"},
649 },
650 WithoutScopeInfo: ptr(false),
651 },
652 },
653 },
654 },
655 {
656 Periodic: &PeriodicMetricReader{
657 Producers: []MetricProducer{
658 {
659 AdditionalProperties: map[string]any{
660 "prometheus": nil,
661 },
662 },
663 },
664 CardinalityLimits: &CardinalityLimits{
665 Default: ptr(2000),
666 Counter: ptr(2000),
667 Gauge: ptr(2000),
668 Histogram: ptr(2000),
669 ObservableCounter: ptr(2000),
670 ObservableGauge: ptr(2000),
671 ObservableUpDownCounter: ptr(2000),
672 UpDownCounter: ptr(2000),
673 },
674 Exporter: PushMetricExporter{
675 OTLPHttp: &OTLPHttpMetricExporter{
676 Tls: &HttpTls{
677 CaFile: ptr("testdata/ca.crt"),
678 CertFile: ptr("testdata/client.crt"),
679 KeyFile: ptr("testdata/client.key"),
680 },
681 Compression: ptr("gzip"),
682 DefaultHistogramAggregation: ptr(ExporterDefaultHistogramAggregationBase2ExponentialBucketHistogram),
683 Endpoint: ptr("http://localhost:4318/v1/metrics"),
684 Encoding: ptr(OTLPHttpEncodingProtobuf),
685 Headers: []NameStringValuePair{
686 {Name: "api-key", Value: ptr("1234")},
687 },
688 HeadersList: ptr("api-key=1234"),
689 TemporalityPreference: ptr(ExporterTemporalityPreferenceDelta),
690 Timeout: ptr(10000),
691 },
692 },
693 Interval: ptr(60000),
694 Timeout: ptr(30000),
695 },
696 },
697 {
698 Periodic: &PeriodicMetricReader{
699 Exporter: PushMetricExporter{
700 OTLPGrpc: &OTLPGrpcMetricExporter{
701 Tls: &GrpcTls{
702 CaFile: ptr("testdata/ca.crt"),
703 CertFile: ptr("testdata/client.crt"),
704 KeyFile: ptr("testdata/client.key"),
705 Insecure: ptr(false),
706 },
707 Compression: ptr("gzip"),
708 DefaultHistogramAggregation: ptr(ExporterDefaultHistogramAggregationBase2ExponentialBucketHistogram),
709 Endpoint: ptr("http://localhost:4317"),
710 Headers: []NameStringValuePair{
711 {Name: "api-key", Value: ptr("1234")},
712 },
713 HeadersList: ptr("api-key=1234"),
714 TemporalityPreference: ptr(ExporterTemporalityPreferenceDelta),
715 Timeout: ptr(10000),
716 },
717 },
718 },
719 },
720 {
721 Periodic: &PeriodicMetricReader{
722 Exporter: PushMetricExporter{
723 OTLPFileDevelopment: &ExperimentalOTLPFileMetricExporter{
724 OutputStream: ptr("file:///var/log/metrics.jsonl"),
725 DefaultHistogramAggregation: ptr(ExporterDefaultHistogramAggregationBase2ExponentialBucketHistogram),
726 TemporalityPreference: ptr(ExporterTemporalityPreferenceDelta),
727 },
728 },
729 },
730 },
731 {
732 Periodic: &PeriodicMetricReader{
733 Exporter: PushMetricExporter{
734 OTLPFileDevelopment: &ExperimentalOTLPFileMetricExporter{
735 OutputStream: ptr("stdout"),
736 DefaultHistogramAggregation: ptr(ExporterDefaultHistogramAggregationBase2ExponentialBucketHistogram),
737 TemporalityPreference: ptr(ExporterTemporalityPreferenceDelta),
738 },
739 },
740 },
741 },
742 {
743 Periodic: &PeriodicMetricReader{
744 Exporter: PushMetricExporter{
745 Console: &ConsoleMetricExporter{},
746 },
747 },
748 },
749 },
750 Views: []View{
751 {
752 Selector: ViewSelector{
753 InstrumentName: ptr("my-instrument"),
754 InstrumentType: ptr(InstrumentTypeHistogram),
755 MeterName: ptr("my-meter"),
756 MeterSchemaUrl: ptr("https://opentelemetry.io/schemas/1.16.0"),
757 MeterVersion: ptr("1.0.0"),
758 Unit: ptr("ms"),
759 },
760 Stream: ViewStream{
761 Aggregation: &Aggregation{
762 ExplicitBucketHistogram: &ExplicitBucketHistogramAggregation{
763 Boundaries: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000},
764 RecordMinMax: ptr(true),
765 },
766 },
767 AggregationCardinalityLimit: ptr(2000),
768 AttributeKeys: &IncludeExclude{
769 Included: []string{"key1", "key2"},
770 Excluded: []string{"key3"},
771 },
772 Description: ptr("new_description"),
773 Name: ptr("new_instrument_name"),
774 },
775 },
776 },
777 },
778 Propagator: &Propagator{
779 Composite: []TextMapPropagator{
780 {
781 Tracecontext: TraceContextPropagator{},
782 },
783 {
784 Baggage: BaggagePropagator{},
785 },
786 {
787 B3: B3Propagator{},
788 },
789 {
790 B3Multi: B3MultiPropagator{},
791 },
792 {
793 Jaeger: JaegerPropagator{},
794 },
795 {
796 Ottrace: OpenTracingPropagator{},
797 },
798 },
799 CompositeList: ptr("tracecontext,baggage,b3,b3multi,jaeger,ottrace,xray"),
800 },
801 Resource: &Resource{
802 Attributes: []AttributeNameValue{
803 {Name: "service.name", Value: "unknown_service"},
804 {Name: "string_key", Type: ptr(AttributeTypeString), Value: "value"},
805 {Name: "bool_key", Type: ptr(AttributeTypeBool), Value: true},
806 {Name: "int_key", Type: ptr(AttributeTypeInt), Value: 1},
807 {Name: "double_key", Type: ptr(AttributeTypeDouble), Value: 1.1},
808 {Name: "string_array_key", Type: ptr(AttributeTypeStringArray), Value: []any{"value1", "value2"}},
809 {Name: "bool_array_key", Type: ptr(AttributeTypeBoolArray), Value: []any{true, false}},
810 {Name: "int_array_key", Type: ptr(AttributeTypeIntArray), Value: []any{1, 2}},
811 {Name: "double_array_key", Type: ptr(AttributeTypeDoubleArray), Value: []any{1.1, 2.2}},
812 },
813 AttributesList: ptr("service.namespace=my-namespace,service.version=1.0.0"),
814 DetectionDevelopment: &ExperimentalResourceDetection{
815 Attributes: &IncludeExclude{
816 Excluded: []string{"process.command_args"},
817 Included: []string{"process.*"},
818 },
819 Detectors: []ExperimentalResourceDetector{
820 {Container: ExperimentalContainerResourceDetector{}},
821 {Host: ExperimentalHostResourceDetector{}},
822 {Process: ExperimentalProcessResourceDetector{}},
823 {Service: ExperimentalServiceResourceDetector{}},
824 },
825 },
826 },
827 TracerProvider: &TracerProvider{
828 TracerConfiguratorDevelopment: &ExperimentalTracerConfigurator{
829 DefaultConfig: &ExperimentalTracerConfig{
830 Disabled: ptr(true),
831 },
832 Tracers: []ExperimentalTracerMatcherAndConfig{
833 {
834 Config: ExperimentalTracerConfig{
835 Disabled: ptr(false),
836 },
837 Name: "io.opentelemetry.contrib.*",
838 },
839 },
840 },
841
842 Limits: &SpanLimits{
843 AttributeCountLimit: ptr(128),
844 AttributeValueLengthLimit: ptr(4096),
845 EventCountLimit: ptr(128),
846 EventAttributeCountLimit: ptr(128),
847 LinkCountLimit: ptr(128),
848 LinkAttributeCountLimit: ptr(128),
849 },
850 Processors: []SpanProcessor{
851 {
852 Batch: &BatchSpanProcessor{
853 ExportTimeout: ptr(30000),
854 Exporter: SpanExporter{
855 OTLPHttp: &OTLPHttpExporter{
856 Tls: &HttpTls{
857 CaFile: ptr("testdata/ca.crt"),
858 CertFile: ptr("testdata/client.crt"),
859 KeyFile: ptr("testdata/client.key"),
860 },
861 Compression: ptr("gzip"),
862 Encoding: ptr(OTLPHttpEncodingProtobuf),
863 Endpoint: ptr("http://localhost:4318/v1/traces"),
864 Headers: []NameStringValuePair{
865 {Name: "api-key", Value: ptr("1234")},
866 },
867 HeadersList: ptr("api-key=1234"),
868 Timeout: ptr(10000),
869 },
870 },
871 MaxExportBatchSize: ptr(512),
872 MaxQueueSize: ptr(2048),
873 ScheduleDelay: ptr(5000),
874 },
875 },
876 {
877 Batch: &BatchSpanProcessor{
878 Exporter: SpanExporter{
879 OTLPGrpc: &OTLPGrpcExporter{
880 Tls: &GrpcTls{
881 CaFile: ptr("testdata/ca.crt"),
882 CertFile: ptr("testdata/client.crt"),
883 KeyFile: ptr("testdata/client.key"),
884 Insecure: ptr(false),
885 },
886 Compression: ptr("gzip"),
887 Endpoint: ptr("http://localhost:4317"),
888 Headers: []NameStringValuePair{
889 {Name: "api-key", Value: ptr("1234")},
890 },
891 HeadersList: ptr("api-key=1234"),
892 Timeout: ptr(10000),
893 },
894 },
895 },
896 },
897 {
898 Batch: &BatchSpanProcessor{
899 Exporter: SpanExporter{
900 OTLPFileDevelopment: &ExperimentalOTLPFileExporter{
901 OutputStream: ptr("file:///var/log/traces.jsonl"),
902 },
903 },
904 },
905 },
906 {
907 Batch: &BatchSpanProcessor{
908 Exporter: SpanExporter{
909 OTLPFileDevelopment: &ExperimentalOTLPFileExporter{
910 OutputStream: ptr("stdout"),
911 },
912 },
913 },
914 },
915 {
916 Simple: &SimpleSpanProcessor{
917 Exporter: SpanExporter{
918 Console: ConsoleExporter{},
919 },
920 },
921 },
922 },
923 Sampler: &Sampler{
924 ParentBased: &ParentBasedSampler{
925 LocalParentNotSampled: &Sampler{
926 AlwaysOff: AlwaysOffSampler{},
927 },
928 LocalParentSampled: &Sampler{
929 AlwaysOn: AlwaysOnSampler{},
930 },
931 RemoteParentNotSampled: &Sampler{
932 AlwaysOff: AlwaysOffSampler{},
933 },
934 RemoteParentSampled: &Sampler{
935 AlwaysOn: AlwaysOnSampler{},
936 },
937 Root: &Sampler{
938 TraceIDRatioBased: &TraceIDRatioBasedSampler{
939 Ratio: ptr(0.0001),
940 },
941 },
942 },
943 },
944 },
945 }
946
947 var v100OpenTelemetryConfigEnvParsing = OpenTelemetryConfiguration{
948 Disabled: ptr(false),
949 FileFormat: "1.0",
950 LogLevel: ptr(SeverityNumberInfo),
951 AttributeLimits: &AttributeLimits{
952 AttributeCountLimit: ptr(128),
953 AttributeValueLengthLimit: ptr(4096),
954 },
955 Resource: &Resource{
956 Attributes: []AttributeNameValue{
957 {Name: "service.name", Value: "unknown_service"},
958 {Name: "string_key", Type: ptr(AttributeTypeString), Value: "value"},
959 {Name: "bool_key", Type: ptr(AttributeTypeBool), Value: true},
960 {Name: "int_key", Type: ptr(AttributeTypeInt), Value: 1},
961 {Name: "double_key", Type: ptr(AttributeTypeDouble), Value: 1.1},
962 {Name: "string_array_key", Type: ptr(AttributeTypeStringArray), Value: []any{"value1", "value2"}},
963 {Name: "bool_array_key", Type: ptr(AttributeTypeBoolArray), Value: []any{true, false}},
964 {Name: "int_array_key", Type: ptr(AttributeTypeIntArray), Value: []any{1, 2}},
965 {Name: "double_array_key", Type: ptr(AttributeTypeDoubleArray), Value: []any{1.1, 2.2}},
966 {Name: "string_value", Type: ptr(AttributeTypeString), Value: "value"},
967 {Name: "bool_value", Type: ptr(AttributeTypeBool), Value: true},
968 {Name: "int_value", Type: ptr(AttributeTypeInt), Value: 1},
969 {Name: "float_value", Type: ptr(AttributeTypeDouble), Value: 1.1},
970 {Name: "hex_value", Type: ptr(AttributeTypeInt), Value: int(48879)},
971 {Name: "quoted_string_value", Type: ptr(AttributeTypeString), Value: "value"},
972 {Name: "quoted_bool_value", Type: ptr(AttributeTypeString), Value: "true"},
973 {Name: "quoted_int_value", Type: ptr(AttributeTypeString), Value: "1"},
974 {Name: "quoted_float_value", Type: ptr(AttributeTypeString), Value: "1.1"},
975 {Name: "quoted_hex_value", Type: ptr(AttributeTypeString), Value: "0xbeef"},
976 {Name: "alternative_env_syntax", Type: ptr(AttributeTypeString), Value: "value"},
977 {Name: "invalid_map_value", Type: ptr(AttributeTypeString), Value: "value\nkey:value"},
978 {Name: "multiple_references_inject", Type: ptr(AttributeTypeString), Value: "foo value 1.1"},
979 {Name: "undefined_key", Type: ptr(AttributeTypeString), Value: nil},
980 {Name: "undefined_key_fallback", Type: ptr(AttributeTypeString), Value: "fallback"},
981 {Name: "env_var_in_key", Type: ptr(AttributeTypeString), Value: "value"},
982 {Name: "replace_me", Type: ptr(AttributeTypeString), Value: "${DO_NOT_REPLACE_ME}"},
983 {Name: "undefined_defaults_to_var", Type: ptr(AttributeTypeString), Value: "${STRING_VALUE}"},
984 {Name: "escaped_does_not_substitute", Type: ptr(AttributeTypeString), Value: "${STRING_VALUE}"},
985 {Name: "escaped_does_not_substitute_fallback", Type: ptr(AttributeTypeString), Value: "${STRING_VALUE:-fallback}"},
986 {Name: "escaped_and_substituted_fallback", Type: ptr(AttributeTypeString), Value: "${STRING_VALUE:-value}"},
987 {Name: "escaped_and_substituted", Type: ptr(AttributeTypeString), Value: "$value"},
988 {Name: "multiple_escaped_and_not_substituted", Type: ptr(AttributeTypeString), Value: "$${STRING_VALUE}"},
989 {Name: "undefined_key_with_escape_sequence_in_fallback", Type: ptr(AttributeTypeString), Value: "${UNDEFINED_KEY}"},
990 {Name: "value_with_escape", Type: ptr(AttributeTypeString), Value: "value$$"},
991 {Name: "escape_sequence", Type: ptr(AttributeTypeString), Value: "a $ b"},
992 {Name: "no_escape_sequence", Type: ptr(AttributeTypeString), Value: "a $ b"},
993 },
994 AttributesList: ptr("service.namespace=my-namespace,service.version=1.0.0"),
995 // Detectors: &Detectors{
996 // Attributes: &DetectorsAttributes{
997 // Excluded: []string{"process.command_args"},
998 // Included: []string{"process.*"},
999 // },
1000 // },
1001 SchemaUrl: ptr("https://opentelemetry.io/schemas/1.16.0"),
1002 },
1003 }
1004
1005 func TestParseFiles(t *testing.T) {
1006 tests := []struct {
1007 name string
1008 input string
1009 wantErr error
1010 wantType *OpenTelemetryConfiguration
1011 }{
1012 {
1013 name: "invalid nil name",
1014 input: "v1.0.0_invalid_nil_name",
1015 wantErr: newErrRequired(&NameStringValuePair{}, "name"),
1016 wantType: &OpenTelemetryConfiguration{},
1017 },
1018 {
1019 name: "invalid nil value",
1020 input: "v1.0.0_invalid_nil_value",
1021 wantErr: newErrRequired(&NameStringValuePair{}, "value"),
1022 wantType: &OpenTelemetryConfiguration{},
1023 },
1024 {
1025 name: "valid v0.2 config",
1026 input: "v0.2",
1027 wantErr: newErrUnmarshal(&OpenTelemetryConfiguration{}),
1028 wantType: &OpenTelemetryConfiguration{},
1029 },
1030 {
1031 name: "valid v0.3 config",
1032 input: "v0.3",
1033 wantErr: newErrUnmarshal(&TextMapPropagator{}),
1034 wantType: &OpenTelemetryConfiguration{},
1035 },
1036 {
1037 name: "valid v1.0.0 config",
1038 input: "v1.0.0",
1039 wantType: &v10OpenTelemetryConfig,
1040 },
1041 }
1042
1043 for _, tt := range tests {
1044 t.Run("yaml:"+tt.name, func(t *testing.T) {
1045 b, err := os.ReadFile(filepath.Join("..", "testdata", fmt.Sprintf("%s.yaml", tt.input)))
1046 require.NoError(t, err)
1047
1048 got, err := ParseYAML(b)
1049 require.ErrorIs(t, err, tt.wantErr)
1050 if tt.wantErr == nil {
1051 assert.Equal(t, tt.wantType, got)
1052 }
1053 })
1054 t.Run("json: "+tt.name, func(t *testing.T) {
1055 b, err := os.ReadFile(filepath.Join("..", "testdata", fmt.Sprintf("%s.json", tt.input)))
1056 require.NoError(t, err)
1057
1058 var got OpenTelemetryConfiguration
1059 err = json.Unmarshal(b, &got)
1060 require.ErrorIs(t, err, tt.wantErr)
1061 assert.Equal(t, tt.wantType, &got)
1062 })
1063 }
1064 }
1065
1066 func TestUnmarshalOpenTelemetryConfiguration(t *testing.T) {
1067 tests := []struct {
1068 name string
1069 jsonConfig []byte
1070 yamlConfig []byte
1071 wantErr error
1072 wantType OpenTelemetryConfiguration
1073 }{
1074 {
1075 name: "valid defaults config",
1076 jsonConfig: []byte(`{"file_format": "1.0"}`),
1077 yamlConfig: []byte("file_format: 1.0"),
1078 wantType: OpenTelemetryConfiguration{
1079 Disabled: ptr(false),
1080 FileFormat: "1.0",
1081 LogLevel: ptr(SeverityNumberInfo),
1082 },
1083 },
1084 {
1085 name: "invalid config missing required file_format",
1086 jsonConfig: []byte(`{"disabled": false}`),
1087 yamlConfig: []byte("disabled: false"),
1088 wantErr: newErrRequired(&OpenTelemetryConfiguration{}, "file_format"),
1089 },
1090 {
1091 name: "file_format invalid",
1092 jsonConfig: []byte(`{"file_format":[], "disabled": false}`),
1093 yamlConfig: []byte("file_format: []\ndisabled: false"),
1094 wantErr: newErrUnmarshal(&OpenTelemetryConfiguration{}),
1095 },
1096 {
1097 name: "invalid config",
1098 jsonConfig: []byte(`{"file_format": "yaml", "disabled": "notabool"}`),
1099 yamlConfig: []byte("file_format: []\ndisabled: notabool"),
1100 wantErr: newErrUnmarshal(&OpenTelemetryConfiguration{}),
1101 },
1102 {
1103 name: "invalid data",
1104 jsonConfig: []byte(`{:2000}`),
1105 yamlConfig: []byte("disabled: []\nconsole: {}\nfile_format: str"),
1106 wantErr: newErrUnmarshal(&OpenTelemetryConfiguration{}),
1107 },
1108 {
1109 name: "resource invalid",
1110 jsonConfig: []byte(`{"resource":[], "file_format": "1.0"}`),
1111 yamlConfig: []byte("resource: []\nfile_format: 1.0"),
1112 wantErr: newErrUnmarshal(&OpenTelemetryConfiguration{}),
1113 },
1114 {
1115 name: "attribute_limits invalid",
1116 jsonConfig: []byte(`{"attribute_limits":[], "file_format": "1.0"}`),
1117 yamlConfig: []byte("attribute_limits: []\nfile_format: 1.0"),
1118 wantErr: newErrUnmarshal(&OpenTelemetryConfiguration{}),
1119 },
1120 {
1121 name: "instrumentation invalid",
1122 jsonConfig: []byte(`{"instrumentation/development":[], "file_format": "1.0"}`),
1123 yamlConfig: []byte("instrumentation/development: []\nfile_format: 1.0"),
1124 wantErr: newErrUnmarshal(&OpenTelemetryConfiguration{}),
1125 },
1126 {
1127 name: "log_level invalid",
1128 jsonConfig: []byte(`{"log_level":[], "file_format": "1.0"}`),
1129 yamlConfig: []byte("log_level: []\nfile_format: 1.0"),
1130 wantErr: newErrUnmarshal(&OpenTelemetryConfiguration{}),
1131 },
1132 }
1133
1134 for _, tt := range tests {
1135 t.Run(tt.name, func(t *testing.T) {
1136 got := OpenTelemetryConfiguration{}
1137 err := got.UnmarshalJSON(tt.jsonConfig)
1138 assert.ErrorIs(t, err, tt.wantErr)
1139 assert.Equal(t, tt.wantType, got)
1140
1141 got = OpenTelemetryConfiguration{}
1142 err = yaml.Unmarshal(tt.yamlConfig, &got)
1143 assert.ErrorIs(t, err, tt.wantErr)
1144 assert.Equal(t, tt.wantType, got)
1145 })
1146 }
1147 }
1148
1149 func TestUnmarshalBatchSpanProcessor(t *testing.T) {
1150 for _, tt := range []struct {
1151 name string
1152 yamlConfig []byte
1153 jsonConfig []byte
1154 wantErrT error
1155 wantExporter SpanExporter
1156 }{
1157 {
1158 name: "valid with null console exporter",
1159 jsonConfig: []byte(`{"exporter":{"console":null}}`),
1160 yamlConfig: []byte("exporter:\n console:\n"),
1161 wantExporter: SpanExporter{Console: ConsoleExporter{}},
1162 },
1163 {
1164 name: "valid with console exporter",
1165 jsonConfig: []byte(`{"exporter":{"console":{}}}`),
1166 yamlConfig: []byte("exporter:\n console: {}"),
1167 wantExporter: SpanExporter{Console: ConsoleExporter{}},
1168 },
1169 {
1170 name: "valid with all fields positive",
1171 jsonConfig: []byte(`{"exporter":{"console":{}},"export_timeout":5000,"max_export_batch_size":512,"max_queue_size":2048,"schedule_delay":1000}`),
1172 yamlConfig: []byte("exporter:\n console: {}\nexport_timeout: 5000\nmax_export_batch_size: 512\nmax_queue_size: 2048\nschedule_delay: 1000"),
1173 wantExporter: SpanExporter{Console: ConsoleExporter{}},
1174 },
1175 {
1176 name: "valid with zero export_timeout",
1177 jsonConfig: []byte(`{"exporter":{"console":{}},"export_timeout":0}`),
1178 yamlConfig: []byte("exporter:\n console: {}\nexport_timeout: 0"),
1179 wantExporter: SpanExporter{Console: ConsoleExporter{}},
1180 },
1181 {
1182 name: "valid with zero schedule_delay",
1183 jsonConfig: []byte(`{"exporter":{"console":{}},"schedule_delay":0}`),
1184 yamlConfig: []byte("exporter:\n console: {}\nschedule_delay: 0"),
1185 wantExporter: SpanExporter{Console: ConsoleExporter{}},
1186 },
1187 {
1188 name: "missing required exporter field",
1189 jsonConfig: []byte(`{}`),
1190 yamlConfig: []byte("{}"),
1191 wantErrT: newErrRequired(&BatchSpanProcessor{}, "exporter"),
1192 },
1193 {
1194 name: "invalid data",
1195 jsonConfig: []byte(`{:2000}`),
1196 yamlConfig: []byte("exporter:\n console: {}\nexport_timeout: !!str str"),
1197 wantErrT: newErrUnmarshal(&BatchSpanProcessor{}),
1198 },
1199 {
1200 name: "invalid export_timeout negative",
1201 jsonConfig: []byte(`{"exporter":{"console":{}},"export_timeout":-1}`),
1202 yamlConfig: []byte("exporter:\n console: {}\nexport_timeout: -1"),
1203 wantErrT: newErrGreaterOrEqualZero("export_timeout"),
1204 },
1205 {
1206 name: "invalid max_export_batch_size zero",
1207 jsonConfig: []byte(`{"exporter":{"console":{}},"max_export_batch_size":0}`),
1208 yamlConfig: []byte("exporter:\n console: {}\nmax_export_batch_size: 0"),
1209 wantErrT: newErrGreaterThanZero("max_export_batch_size"),
1210 },
1211 {
1212 name: "invalid max_export_batch_size negative",
1213 jsonConfig: []byte(`{"exporter":{"console":{}},"max_export_batch_size":-1}`),
1214 yamlConfig: []byte("exporter:\n console: {}\nmax_export_batch_size: -1"),
1215 wantErrT: newErrGreaterThanZero("max_export_batch_size"),
1216 },
1217 {
1218 name: "invalid max_queue_size zero",
1219 jsonConfig: []byte(`{"exporter":{"console":{}},"max_queue_size":0}`),
1220 yamlConfig: []byte("exporter:\n console: {}\nmax_queue_size: 0"),
1221 wantErrT: newErrGreaterThanZero("max_queue_size"),
1222 },
1223 {
1224 name: "invalid max_queue_size negative",
1225 jsonConfig: []byte(`{"exporter":{"console":{}},"max_queue_size":-1}`),
1226 yamlConfig: []byte("exporter:\n console: {}\nmax_queue_size: -1"),
1227 wantErrT: newErrGreaterThanZero("max_queue_size"),
1228 },
1229 {
1230 name: "invalid schedule_delay negative",
1231 jsonConfig: []byte(`{"exporter":{"console":{}},"schedule_delay":-1}`),
1232 yamlConfig: []byte("exporter:\n console: {}\nschedule_delay: -1"),
1233 wantErrT: newErrGreaterOrEqualZero("schedule_delay"),
1234 },
1235 } {
1236 t.Run(tt.name, func(t *testing.T) {
1237 cl := BatchSpanProcessor{}
1238 err := cl.UnmarshalJSON(tt.jsonConfig)
1239 assert.ErrorIs(t, err, tt.wantErrT)
1240 assert.Equal(t, tt.wantExporter, cl.Exporter)
1241
1242 cl = BatchSpanProcessor{}
1243 err = yaml.Unmarshal(tt.yamlConfig, &cl)
1244 assert.ErrorIs(t, err, tt.wantErrT)
1245 assert.Equal(t, tt.wantExporter, cl.Exporter)
1246 })
1247 }
1248 }
1249
1250 func TestParseYAMLWithEnvironmentVariables(t *testing.T) {
1251 tests := []struct {
1252 name string
1253 input string
1254 wantErr error
1255 wantType any
1256 }{
1257 {
1258 name: "valid v1.0.0 config with env vars",
1259 input: "v1.0.0_env_var.yaml",
1260 wantType: &v100OpenTelemetryConfigEnvParsing,
1261 },
1262 }
1263
1264 t.Setenv("OTEL_SDK_DISABLED", "false")
1265 t.Setenv("OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT", "4096")
1266 t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf")
1267 t.Setenv("STRING_VALUE", "value")
1268 t.Setenv("BOOL_VALUE", "true")
1269 t.Setenv("INT_VALUE", "1")
1270 t.Setenv("FLOAT_VALUE", "1.1")
1271 t.Setenv("HEX_VALUE", "0xbeef") // A valid integer value (i.e. 3735928559) written in hexadecimal
1272 t.Setenv("INVALID_MAP_VALUE", "value\\nkey:value") // An invalid attempt to inject a map key into the YAML
1273 t.Setenv("ENV_VAR_IN_KEY", "env_var_in_key") // An env var in key
1274 t.Setenv("DO_NOT_REPLACE_ME", "Never use this value") // An unused environment variable
1275 t.Setenv("REPLACE_ME", "${DO_NOT_REPLACE_ME}") // A valid replacement text, used verbatim, not replaced with "Never use this value"
1276 t.Setenv("VALUE_WITH_ESCAPE", "value$$") // A valid replacement text, used verbatim, not replaced with "Never use this value"
1277 for _, tt := range tests {
1278 t.Run(tt.name, func(t *testing.T) {
1279 b, err := os.ReadFile(filepath.Join("..", "testdata", tt.input))
1280 require.NoError(t, err)
1281
1282 got, err := ParseYAML(b)
1283 if tt.wantErr != nil {
1284 require.Equal(t, tt.wantErr.Error(), err.Error())
1285 } else {
1286 require.NoError(t, err)
1287 assert.Equal(t, tt.wantType, got)
1288 }
1289 })
1290 }
1291 }
1292
1293 func TestUnmarshalPeriodicMetricReader(t *testing.T) {
1294 for _, tt := range []struct {
1295 name string
1296 yamlConfig []byte
1297 jsonConfig []byte
1298 wantErrT error
1299 wantExporter PushMetricExporter
1300 }{
1301 {
1302 name: "valid with null console exporter",
1303 jsonConfig: []byte(`{"exporter":{"console":null}}`),
1304 yamlConfig: []byte("exporter:\n console:\n"),
1305 wantExporter: PushMetricExporter{Console: &ConsoleMetricExporter{}},
1306 },
1307 {
1308 name: "valid with console exporter",
1309 jsonConfig: []byte(`{"exporter":{"console":{}}}`),
1310 yamlConfig: []byte("exporter:\n console: {}"),
1311 wantExporter: PushMetricExporter{Console: &ConsoleMetricExporter{}},
1312 },
1313 {
1314 name: "valid with all fields positive",
1315 jsonConfig: []byte(`{"exporter":{"console":{}},"timeout":5000,"interval":1000}`),
1316 yamlConfig: []byte("exporter:\n console: {}\ntimeout: 5000\ninterval: 1000"),
1317 wantExporter: PushMetricExporter{Console: &ConsoleMetricExporter{}},
1318 },
1319 {
1320 name: "valid with zero timeout",
1321 jsonConfig: []byte(`{"exporter":{"console":{}},"timeout":0}`),
1322 yamlConfig: []byte("exporter:\n console: {}\ntimeout: 0"),
1323 wantExporter: PushMetricExporter{Console: &ConsoleMetricExporter{}},
1324 },
1325 {
1326 name: "valid with zero interval",
1327 jsonConfig: []byte(`{"exporter":{"console":{}},"interval":0}`),
1328 yamlConfig: []byte("exporter:\n console: {}\ninterval: 0"),
1329 wantExporter: PushMetricExporter{Console: &ConsoleMetricExporter{}},
1330 },
1331 {
1332 name: "missing required exporter field",
1333 jsonConfig: []byte(`{}`),
1334 yamlConfig: []byte("{}"),
1335 wantErrT: newErrRequired(&PeriodicMetricReader{}, "exporter"),
1336 },
1337 {
1338 name: "invalid data",
1339 jsonConfig: []byte(`{:2000}`),
1340 yamlConfig: []byte("exporter:\n console: {}\ntimeout: !!str str"),
1341 wantErrT: newErrUnmarshal(&PeriodicMetricReader{}),
1342 },
1343 {
1344 name: "invalid timeout negative",
1345 jsonConfig: []byte(`{"exporter":{"console":{}},"timeout":-1}`),
1346 yamlConfig: []byte("exporter:\n console: {}\ntimeout: -1"),
1347 wantErrT: newErrGreaterOrEqualZero("timeout"),
1348 },
1349 {
1350 name: "invalid interval negative",
1351 jsonConfig: []byte(`{"exporter":{"console":{}},"interval":-1}`),
1352 yamlConfig: []byte("exporter:\n console: {}\ninterval: -1"),
1353 wantErrT: newErrGreaterOrEqualZero("interval"),
1354 },
1355 } {
1356 t.Run(tt.name, func(t *testing.T) {
1357 pmr := PeriodicMetricReader{}
1358 err := pmr.UnmarshalJSON(tt.jsonConfig)
1359 assert.ErrorIs(t, err, tt.wantErrT)
1360 assert.Equal(t, tt.wantExporter, pmr.Exporter)
1361
1362 pmr = PeriodicMetricReader{}
1363 err = yaml.Unmarshal(tt.yamlConfig, &pmr)
1364 assert.ErrorIs(t, err, tt.wantErrT)
1365 assert.Equal(t, tt.wantExporter, pmr.Exporter)
1366 })
1367 }
1368 }
1369
1370 func TestUnmarshalCardinalityLimits(t *testing.T) {
1371 for _, tt := range []struct {
1372 name string
1373 yamlConfig []byte
1374 jsonConfig []byte
1375 wantErrT error
1376 }{
1377 {
1378 name: "valid with all fields positive",
1379 jsonConfig: []byte(`{"counter":100,"default":200,"gauge":300,"histogram":400,"observable_counter":500,"observable_gauge":600,"observable_up_down_counter":700,"up_down_counter":800}`),
1380 yamlConfig: []byte("counter: 100\ndefault: 200\ngauge: 300\nhistogram: 400\nobservable_counter: 500\nobservable_gauge: 600\nobservable_up_down_counter: 700\nup_down_counter: 800"),
1381 },
1382 {
1383 name: "valid with single field",
1384 jsonConfig: []byte(`{"default":2000}`),
1385 yamlConfig: []byte("default: 2000"),
1386 },
1387 {
1388 name: "valid empty",
1389 jsonConfig: []byte(`{}`),
1390 yamlConfig: []byte("{}"),
1391 },
1392 {
1393 name: "invalid data",
1394 jsonConfig: []byte(`{:2000}`),
1395 yamlConfig: []byte("counter: !!str 2000"),
1396 wantErrT: newErrUnmarshal(&CardinalityLimits{}),
1397 },
1398 {
1399 name: "invalid counter zero",
1400 jsonConfig: []byte(`{"counter":0}`),
1401 yamlConfig: []byte("counter: 0"),
1402 wantErrT: newErrGreaterThanZero("counter"),
1403 },
1404 {
1405 name: "invalid counter negative",
1406 jsonConfig: []byte(`{"counter":-1}`),
1407 yamlConfig: []byte("counter: -1"),
1408 wantErrT: newErrGreaterThanZero("counter"),
1409 },
1410 {
1411 name: "invalid default zero",
1412 jsonConfig: []byte(`{"default":0}`),
1413 yamlConfig: []byte("default: 0"),
1414 wantErrT: newErrGreaterThanZero("default"),
1415 },
1416 {
1417 name: "invalid default negative",
1418 jsonConfig: []byte(`{"default":-1}`),
1419 yamlConfig: []byte("default: -1"),
1420 wantErrT: newErrGreaterThanZero("default"),
1421 },
1422 {
1423 name: "invalid gauge zero",
1424 jsonConfig: []byte(`{"gauge":0}`),
1425 yamlConfig: []byte("gauge: 0"),
1426 wantErrT: newErrGreaterThanZero("gauge"),
1427 },
1428 {
1429 name: "invalid gauge negative",
1430 jsonConfig: []byte(`{"gauge":-1}`),
1431 yamlConfig: []byte("gauge: -1"),
1432 wantErrT: newErrGreaterThanZero("gauge"),
1433 },
1434 {
1435 name: "invalid histogram zero",
1436 jsonConfig: []byte(`{"histogram":0}`),
1437 yamlConfig: []byte("histogram: 0"),
1438 wantErrT: newErrGreaterThanZero("histogram"),
1439 },
1440 {
1441 name: "invalid histogram negative",
1442 jsonConfig: []byte(`{"histogram":-1}`),
1443 yamlConfig: []byte("histogram: -1"),
1444 wantErrT: newErrGreaterThanZero("histogram"),
1445 },
1446 {
1447 name: "invalid observable_counter zero",
1448 jsonConfig: []byte(`{"observable_counter":0}`),
1449 yamlConfig: []byte("observable_counter: 0"),
1450 wantErrT: newErrGreaterThanZero("observable_counter"),
1451 },
1452 {
1453 name: "invalid observable_counter negative",
1454 jsonConfig: []byte(`{"observable_counter":-1}`),
1455 yamlConfig: []byte("observable_counter: -1"),
1456 wantErrT: newErrGreaterThanZero("observable_counter"),
1457 },
1458 {
1459 name: "invalid observable_gauge zero",
1460 jsonConfig: []byte(`{"observable_gauge":0}`),
1461 yamlConfig: []byte("observable_gauge: 0"),
1462 wantErrT: newErrGreaterThanZero("observable_gauge"),
1463 },
1464 {
1465 name: "invalid observable_gauge negative",
1466 jsonConfig: []byte(`{"observable_gauge":-1}`),
1467 yamlConfig: []byte("observable_gauge: -1"),
1468 wantErrT: newErrGreaterThanZero("observable_gauge"),
1469 },
1470 {
1471 name: "invalid observable_up_down_counter zero",
1472 jsonConfig: []byte(`{"observable_up_down_counter":0}`),
1473 yamlConfig: []byte("observable_up_down_counter: 0"),
1474 wantErrT: newErrGreaterThanZero("observable_up_down_counter"),
1475 },
1476 {
1477 name: "invalid observable_up_down_counter negative",
1478 jsonConfig: []byte(`{"observable_up_down_counter":-1}`),
1479 yamlConfig: []byte("observable_up_down_counter: -1"),
1480 wantErrT: newErrGreaterThanZero("observable_up_down_counter"),
1481 },
1482 {
1483 name: "invalid up_down_counter zero",
1484 jsonConfig: []byte(`{"up_down_counter":0}`),
1485 yamlConfig: []byte("up_down_counter: 0"),
1486 wantErrT: newErrGreaterThanZero("up_down_counter"),
1487 },
1488 {
1489 name: "invalid up_down_counter negative",
1490 jsonConfig: []byte(`{"up_down_counter":-1}`),
1491 yamlConfig: []byte("up_down_counter: -1"),
1492 wantErrT: newErrGreaterThanZero("up_down_counter"),
1493 },
1494 } {
1495 t.Run(tt.name, func(t *testing.T) {
1496 cl := CardinalityLimits{}
1497 err := cl.UnmarshalJSON(tt.jsonConfig)
1498 assert.ErrorIs(t, err, tt.wantErrT)
1499
1500 cl = CardinalityLimits{}
1501 err = yaml.Unmarshal(tt.yamlConfig, &cl)
1502 assert.ErrorIs(t, err, tt.wantErrT)
1503 })
1504 }
1505 }
1506
1507 func TestCreateHeadersConfig(t *testing.T) {
1508 tests := []struct {
1509 name string
1510 headers []NameStringValuePair
1511 headersList *string
1512 wantHeaders map[string]string
1513 wantErr error
1514 }{
1515 {
1516 name: "no headers",
1517 headers: []NameStringValuePair{},
1518 headersList: nil,
1519 wantHeaders: map[string]string{},
1520 },
1521 {
1522 name: "headerslist only",
1523 headers: []NameStringValuePair{},
1524 headersList: ptr("a=b,c=d"),
1525 wantHeaders: map[string]string{
1526 "a": "b",
1527 "c": "d",
1528 },
1529 },
1530 {
1531 name: "headers only",
1532 headers: []NameStringValuePair{
1533 {
1534 Name: "a",
1535 Value: ptr("b"),
1536 },
1537 {
1538 Name: "c",
1539 Value: ptr("d"),
1540 },
1541 },
1542 headersList: nil,
1543 wantHeaders: map[string]string{
1544 "a": "b",
1545 "c": "d",
1546 },
1547 },
1548 {
1549 name: "both headers and headerslist",
1550 headers: []NameStringValuePair{
1551 {
1552 Name: "a",
1553 Value: ptr("b"),
1554 },
1555 },
1556 headersList: ptr("c=d"),
1557 wantHeaders: map[string]string{
1558 "a": "b",
1559 "c": "d",
1560 },
1561 },
1562 {
1563 name: "headers supersedes headerslist",
1564 headers: []NameStringValuePair{
1565 {
1566 Name: "a",
1567 Value: ptr("b"),
1568 },
1569 {
1570 Name: "c",
1571 Value: ptr("override"),
1572 },
1573 },
1574 headersList: ptr("c=d"),
1575 wantHeaders: map[string]string{
1576 "a": "b",
1577 "c": "override",
1578 },
1579 },
1580 {
1581 name: "invalid headerslist",
1582 headersList: ptr("==="),
1583 wantErr: newErrInvalid("invalid headers_list"),
1584 },
1585 }
1586 for _, tt := range tests {
1587 t.Run(tt.name, func(t *testing.T) {
1588 headersMap, err := createHeadersConfig(tt.headers, tt.headersList)
1589 require.ErrorIs(t, err, tt.wantErr)
1590 require.Equal(t, tt.wantHeaders, headersMap)
1591 })
1592 }
1593 }
1594
1595 func TestUnmarshalSpanLimits(t *testing.T) {
1596 for _, tt := range []struct {
1597 name string
1598 yamlConfig []byte
1599 jsonConfig []byte
1600 wantErrT error
1601 }{
1602 {
1603 name: "valid with all fields positive",
1604 jsonConfig: []byte(`{"attribute_count_limit":100,"attribute_value_length_limit":200,"event_attribute_count_limit":300,"event_count_limit":400,"link_attribute_count_limit":500,"link_count_limit":600}`),
1605 yamlConfig: []byte("attribute_count_limit: 100\nattribute_value_length_limit: 200\nevent_attribute_count_limit: 300\nevent_count_limit: 400\nlink_attribute_count_limit: 500\nlink_count_limit: 600"),
1606 },
1607 {
1608 name: "valid with single field",
1609 jsonConfig: []byte(`{"attribute_value_length_limit":2000}`),
1610 yamlConfig: []byte("attribute_value_length_limit: 2000"),
1611 },
1612 {
1613 name: "valid empty",
1614 jsonConfig: []byte(`{}`),
1615 yamlConfig: []byte("{}"),
1616 },
1617 {
1618 name: "invalid data",
1619 jsonConfig: []byte(`{:2000}`),
1620 yamlConfig: []byte("attribute_count_limit: !!str 2000"),
1621 wantErrT: newErrUnmarshal(&SpanLimits{}),
1622 },
1623 {
1624 name: "invalid attribute_count_limit negative",
1625 jsonConfig: []byte(`{"attribute_count_limit":-1}`),
1626 yamlConfig: []byte("attribute_count_limit: -1"),
1627 wantErrT: newErrGreaterOrEqualZero("attribute_count_limit"),
1628 },
1629 {
1630 name: "invalid attribute_value_length_limit negative",
1631 jsonConfig: []byte(`{"attribute_value_length_limit":-1}`),
1632 yamlConfig: []byte("attribute_value_length_limit: -1"),
1633 wantErrT: newErrGreaterOrEqualZero("attribute_value_length_limit"),
1634 },
1635 {
1636 name: "invalid event_attribute_count_limit negative",
1637 jsonConfig: []byte(`{"event_attribute_count_limit":-1}`),
1638 yamlConfig: []byte("event_attribute_count_limit: -1"),
1639 wantErrT: newErrGreaterOrEqualZero("event_attribute_count_limit"),
1640 },
1641 {
1642 name: "invalid event_count_limit negative",
1643 jsonConfig: []byte(`{"event_count_limit":-1}`),
1644 yamlConfig: []byte("event_count_limit: -1"),
1645 wantErrT: newErrGreaterOrEqualZero("event_count_limit"),
1646 },
1647 {
1648 name: "invalid link_attribute_count_limit negative",
1649 jsonConfig: []byte(`{"link_attribute_count_limit":-1}`),
1650 yamlConfig: []byte("link_attribute_count_limit: -1"),
1651 wantErrT: newErrGreaterOrEqualZero("link_attribute_count_limit"),
1652 },
1653 {
1654 name: "invalid link_count_limit negative",
1655 jsonConfig: []byte(`{"link_count_limit":-1}`),
1656 yamlConfig: []byte("link_count_limit: -1"),
1657 wantErrT: newErrGreaterOrEqualZero("link_count_limit"),
1658 },
1659 } {
1660 t.Run(tt.name, func(t *testing.T) {
1661 cl := SpanLimits{}
1662 err := cl.UnmarshalJSON(tt.jsonConfig)
1663 assert.ErrorIs(t, err, tt.wantErrT)
1664
1665 cl = SpanLimits{}
1666 err = yaml.Unmarshal(tt.yamlConfig, &cl)
1667 assert.ErrorIs(t, err, tt.wantErrT)
1668 })
1669 }
1670 }
1671
1672 func TestUnmarshalOTLPHttpExporter(t *testing.T) {
1673 for _, tt := range []struct {
1674 name string
1675 yamlConfig []byte
1676 jsonConfig []byte
1677 wantErrT error
1678 wantExporter OTLPHttpExporter
1679 }{
1680 {
1681 name: "valid with exporter",
1682 jsonConfig: []byte(`{"endpoint":"localhost:4318"}`),
1683 yamlConfig: []byte("endpoint: localhost:4318\n"),
1684 wantExporter: OTLPHttpExporter{Endpoint: ptr("localhost:4318")},
1685 },
1686 {
1687 name: "missing required endpoint field",
1688 jsonConfig: []byte(`{}`),
1689 yamlConfig: []byte("{}"),
1690 wantErrT: newErrRequired(&OTLPHttpExporter{}, "endpoint"),
1691 },
1692 {
1693 name: "valid with zero timeout",
1694 jsonConfig: []byte(`{"endpoint":"localhost:4318", "timeout":0}`),
1695 yamlConfig: []byte("endpoint: localhost:4318\ntimeout: 0"),
1696 wantExporter: OTLPHttpExporter{Endpoint: ptr("localhost:4318"), Timeout: ptr(0)},
1697 },
1698 {
1699 name: "invalid data",
1700 jsonConfig: []byte(`{:2000}`),
1701 yamlConfig: []byte("endpoint: localhost:4318\ntimeout: !!str str"),
1702 wantErrT: newErrUnmarshal(&OTLPHttpExporter{}),
1703 },
1704 {
1705 name: "invalid timeout negative",
1706 jsonConfig: []byte(`{"endpoint":"localhost:4318", "timeout":-1}`),
1707 yamlConfig: []byte("endpoint: localhost:4318\ntimeout: -1"),
1708 wantErrT: newErrGreaterOrEqualZero("timeout"),
1709 },
1710 } {
1711 t.Run(tt.name, func(t *testing.T) {
1712 cl := OTLPHttpExporter{}
1713 err := cl.UnmarshalJSON(tt.jsonConfig)
1714 assert.ErrorIs(t, err, tt.wantErrT)
1715 assert.Equal(t, tt.wantExporter, cl)
1716
1717 cl = OTLPHttpExporter{}
1718 err = yaml.Unmarshal(tt.yamlConfig, &cl)
1719 assert.ErrorIs(t, err, tt.wantErrT)
1720 assert.Equal(t, tt.wantExporter, cl)
1721 })
1722 }
1723 }
1724
1725 func TestUnmarshalOTLPGrpcExporter(t *testing.T) {
1726 for _, tt := range []struct {
1727 name string
1728 yamlConfig []byte
1729 jsonConfig []byte
1730 wantErrT error
1731 wantExporter OTLPGrpcExporter
1732 }{
1733 {
1734 name: "valid with exporter",
1735 jsonConfig: []byte(`{"endpoint":"localhost:4318"}`),
1736 yamlConfig: []byte("endpoint: localhost:4318\n"),
1737 wantExporter: OTLPGrpcExporter{Endpoint: ptr("localhost:4318")},
1738 },
1739 {
1740 name: "missing required endpoint field",
1741 jsonConfig: []byte(`{}`),
1742 yamlConfig: []byte("{}"),
1743 wantErrT: newErrRequired(&OTLPGrpcExporter{}, "endpoint"),
1744 },
1745 {
1746 name: "valid with zero timeout",
1747 jsonConfig: []byte(`{"endpoint":"localhost:4318", "timeout":0}`),
1748 yamlConfig: []byte("endpoint: localhost:4318\ntimeout: 0"),
1749 wantExporter: OTLPGrpcExporter{Endpoint: ptr("localhost:4318"), Timeout: ptr(0)},
1750 },
1751 {
1752 name: "invalid data",
1753 jsonConfig: []byte(`{:2000}`),
1754 yamlConfig: []byte("endpoint: localhost:4318\ntimeout: !!str str"),
1755 wantErrT: newErrUnmarshal(&OTLPGrpcExporter{}),
1756 },
1757 {
1758 name: "invalid timeout negative",
1759 jsonConfig: []byte(`{"endpoint":"localhost:4318", "timeout":-1}`),
1760 yamlConfig: []byte("endpoint: localhost:4318\ntimeout: -1"),
1761 wantErrT: newErrGreaterOrEqualZero("timeout"),
1762 },
1763 } {
1764 t.Run(tt.name, func(t *testing.T) {
1765 cl := OTLPGrpcExporter{}
1766 err := cl.UnmarshalJSON(tt.jsonConfig)
1767 assert.ErrorIs(t, err, tt.wantErrT)
1768 assert.Equal(t, tt.wantExporter, cl)
1769
1770 cl = OTLPGrpcExporter{}
1771 err = yaml.Unmarshal(tt.yamlConfig, &cl)
1772 assert.ErrorIs(t, err, tt.wantErrT)
1773 assert.Equal(t, tt.wantExporter, cl)
1774 })
1775 }
1776 }
1777
1778 func TestUnmarshalOTLPHttpMetricExporter(t *testing.T) {
1779 for _, tt := range []struct {
1780 name string
1781 yamlConfig []byte
1782 jsonConfig []byte
1783 wantErrT error
1784 wantExporter OTLPHttpMetricExporter
1785 }{
1786 {
1787 name: "valid with exporter",
1788 jsonConfig: []byte(`{"endpoint":"localhost:4318"}`),
1789 yamlConfig: []byte("endpoint: localhost:4318\n"),
1790 wantExporter: OTLPHttpMetricExporter{Endpoint: ptr("localhost:4318")},
1791 },
1792 {
1793 name: "missing required endpoint field",
1794 jsonConfig: []byte(`{}`),
1795 yamlConfig: []byte("{}"),
1796 wantErrT: newErrRequired(&OTLPHttpMetricExporter{}, "endpoint"),
1797 },
1798 {
1799 name: "valid with zero timeout",
1800 jsonConfig: []byte(`{"endpoint":"localhost:4318", "timeout":0}`),
1801 yamlConfig: []byte("endpoint: localhost:4318\ntimeout: 0"),
1802 wantExporter: OTLPHttpMetricExporter{Endpoint: ptr("localhost:4318"), Timeout: ptr(0)},
1803 },
1804 {
1805 name: "invalid data",
1806 jsonConfig: []byte(`{:2000}`),
1807 yamlConfig: []byte("endpoint: localhost:4318\ntimeout: !!str str"),
1808 wantErrT: newErrUnmarshal(&OTLPHttpMetricExporter{}),
1809 },
1810 {
1811 name: "invalid timeout negative",
1812 jsonConfig: []byte(`{"endpoint":"localhost:4318", "timeout":-1}`),
1813 yamlConfig: []byte("endpoint: localhost:4318\ntimeout: -1"),
1814 wantErrT: newErrGreaterOrEqualZero("timeout"),
1815 },
1816 } {
1817 t.Run(tt.name, func(t *testing.T) {
1818 cl := OTLPHttpMetricExporter{}
1819 err := cl.UnmarshalJSON(tt.jsonConfig)
1820 assert.ErrorIs(t, err, tt.wantErrT)
1821 assert.Equal(t, tt.wantExporter, cl)
1822
1823 cl = OTLPHttpMetricExporter{}
1824 err = yaml.Unmarshal(tt.yamlConfig, &cl)
1825 assert.ErrorIs(t, err, tt.wantErrT)
1826 assert.Equal(t, tt.wantExporter, cl)
1827 })
1828 }
1829 }
1830
1831 func TestUnmarshalOTLPGrpcMetricExporter(t *testing.T) {
1832 for _, tt := range []struct {
1833 name string
1834 yamlConfig []byte
1835 jsonConfig []byte
1836 wantErrT error
1837 wantExporter OTLPGrpcMetricExporter
1838 }{
1839 {
1840 name: "valid with exporter",
1841 jsonConfig: []byte(`{"endpoint":"localhost:4318"}`),
1842 yamlConfig: []byte("endpoint: localhost:4318\n"),
1843 wantExporter: OTLPGrpcMetricExporter{Endpoint: ptr("localhost:4318")},
1844 },
1845 {
1846 name: "missing required endpoint field",
1847 jsonConfig: []byte(`{}`),
1848 yamlConfig: []byte("{}"),
1849 wantErrT: newErrRequired(&OTLPGrpcMetricExporter{}, "endpoint"),
1850 },
1851 {
1852 name: "valid with zero timeout",
1853 jsonConfig: []byte(`{"endpoint":"localhost:4318", "timeout":0}`),
1854 yamlConfig: []byte("endpoint: localhost:4318\ntimeout: 0"),
1855 wantExporter: OTLPGrpcMetricExporter{Endpoint: ptr("localhost:4318"), Timeout: ptr(0)},
1856 },
1857 {
1858 name: "invalid data",
1859 jsonConfig: []byte(`{:2000}`),
1860 yamlConfig: []byte("endpoint: localhost:4318\ntimeout: !!str str"),
1861 wantErrT: newErrUnmarshal(&OTLPGrpcMetricExporter{}),
1862 },
1863 {
1864 name: "invalid timeout negative",
1865 jsonConfig: []byte(`{"endpoint":"localhost:4318", "timeout":-1}`),
1866 yamlConfig: []byte("endpoint: localhost:4318\ntimeout: -1"),
1867 wantErrT: newErrGreaterOrEqualZero("timeout"),
1868 },
1869 } {
1870 t.Run(tt.name, func(t *testing.T) {
1871 cl := OTLPGrpcMetricExporter{}
1872 err := cl.UnmarshalJSON(tt.jsonConfig)
1873 assert.ErrorIs(t, err, tt.wantErrT)
1874 assert.Equal(t, tt.wantExporter, cl)
1875
1876 cl = OTLPGrpcMetricExporter{}
1877 err = yaml.Unmarshal(tt.yamlConfig, &cl)
1878 assert.ErrorIs(t, err, tt.wantErrT)
1879 assert.Equal(t, tt.wantExporter, cl)
1880 })
1881 }
1882 }
1883
1884 func TestUnmarshalAttributeNameValueType(t *testing.T) {
1885 for _, tt := range []struct {
1886 name string
1887 yamlConfig []byte
1888 jsonConfig []byte
1889 wantErrT error
1890 wantAttributeNameValue AttributeNameValue
1891 }{
1892 {
1893 name: "invalid data",
1894 jsonConfig: []byte(`{:2000}`),
1895 yamlConfig: []byte("name: []\nvalue: true\ntype: bool\n"),
1896 wantErrT: newErrUnmarshal(&AttributeNameValue{}),
1897 },
1898 {
1899 name: "missing required name field",
1900 jsonConfig: []byte(`{}`),
1901 yamlConfig: []byte("{}"),
1902 wantErrT: newErrRequired(&AttributeNameValue{}, "name"),
1903 },
1904 {
1905 name: "missing required value field",
1906 jsonConfig: []byte(`{"name":"test"}`),
1907 yamlConfig: []byte("name: test"),
1908 wantErrT: newErrRequired(&AttributeNameValue{}, "value"),
1909 },
1910 {
1911 name: "valid string value",
1912 jsonConfig: []byte(`{"name":"test", "value": "test-val", "type": "string"}`),
1913 yamlConfig: []byte("name: test\nvalue: test-val\ntype: string\n"),
1914 wantAttributeNameValue: AttributeNameValue{
1915 Name: "test",
1916 Value: "test-val",
1917 Type: ptr(AttributeTypeString),
1918 },
1919 },
1920 {
1921 name: "valid string_array value",
1922 jsonConfig: []byte(`{"name":"test", "value": ["test-val", "test-val-2"], "type": "string_array"}`),
1923 yamlConfig: []byte("name: test\nvalue: [test-val, test-val-2]\ntype: string_array\n"),
1924 wantAttributeNameValue: AttributeNameValue{
1925 Name: "test",
1926 Value: []any{"test-val", "test-val-2"},
1927 Type: ptr(AttributeTypeStringArray),
1928 },
1929 },
1930 {
1931 name: "valid bool value",
1932 jsonConfig: []byte(`{"name":"test", "value": true, "type": "bool"}`),
1933 yamlConfig: []byte("name: test\nvalue: true\ntype: bool\n"),
1934 wantAttributeNameValue: AttributeNameValue{
1935 Name: "test",
1936 Value: true,
1937 Type: ptr(AttributeTypeBool),
1938 },
1939 },
1940 {
1941 name: "valid string_array value",
1942 jsonConfig: []byte(`{"name":"test", "value": ["test-val", "test-val-2"], "type": "string_array"}`),
1943 yamlConfig: []byte("name: test\nvalue: [test-val, test-val-2]\ntype: string_array\n"),
1944 wantAttributeNameValue: AttributeNameValue{
1945 Name: "test",
1946 Value: []any{"test-val", "test-val-2"},
1947 Type: ptr(AttributeTypeStringArray),
1948 },
1949 },
1950 {
1951 name: "valid int value",
1952 jsonConfig: []byte(`{"name":"test", "value": 1, "type": "int"}`),
1953 yamlConfig: []byte("name: test\nvalue: 1\ntype: int\n"),
1954 wantAttributeNameValue: AttributeNameValue{
1955 Name: "test",
1956 Value: int(1),
1957 Type: ptr(AttributeTypeInt),
1958 },
1959 },
1960 {
1961 name: "valid int_array value",
1962 jsonConfig: []byte(`{"name":"test", "value": [1, 2], "type": "int_array"}`),
1963 yamlConfig: []byte("name: test\nvalue: [1, 2]\ntype: int_array\n"),
1964 wantAttributeNameValue: AttributeNameValue{
1965 Name: "test",
1966 Value: []any{1, 2},
1967 Type: ptr(AttributeTypeIntArray),
1968 },
1969 },
1970 {
1971 name: "valid double value",
1972 jsonConfig: []byte(`{"name":"test", "value": 1, "type": "double"}`),
1973 yamlConfig: []byte("name: test\nvalue: 1\ntype: double\n"),
1974 wantAttributeNameValue: AttributeNameValue{
1975 Name: "test",
1976 Value: float64(1),
1977 Type: ptr(AttributeTypeDouble),
1978 },
1979 },
1980 {
1981 name: "valid double_array value",
1982 jsonConfig: []byte(`{"name":"test", "value": [1, 2], "type": "double_array"}`),
1983 yamlConfig: []byte("name: test\nvalue: [1.0, 2.0]\ntype: double_array\n"),
1984 wantAttributeNameValue: AttributeNameValue{
1985 Name: "test",
1986 Value: []any{float64(1), float64(2)},
1987 Type: ptr(AttributeTypeDoubleArray),
1988 },
1989 },
1990 {
1991 name: "invalid type",
1992 jsonConfig: []byte(`{"name":"test", "value": 1, "type": "float"}`),
1993 yamlConfig: []byte("name: test\nvalue: 1\ntype: float\n"),
1994 wantErrT: newErrInvalid("unexpected value type"),
1995 },
1996 } {
1997 t.Run(tt.name, func(t *testing.T) {
1998 val := AttributeNameValue{}
1999 err := val.UnmarshalJSON(tt.jsonConfig)
2000 assert.ErrorIs(t, err, tt.wantErrT)
2001 assert.Equal(t, tt.wantAttributeNameValue, val)
2002
2003 val = AttributeNameValue{}
2004 err = yaml.Unmarshal(tt.yamlConfig, &val)
2005 assert.ErrorIs(t, err, tt.wantErrT)
2006 assert.Equal(t, tt.wantAttributeNameValue, val)
2007 })
2008 }
2009 }
2010
2011 func TestUnmarshalNameStringValuePairType(t *testing.T) {
2012 for _, tt := range []struct {
2013 name string
2014 yamlConfig []byte
2015 jsonConfig []byte
2016 wantErrT error
2017 wantNameStringValuePair NameStringValuePair
2018 }{
2019 {
2020 name: "invalid data",
2021 jsonConfig: []byte(`{:2000}`),
2022 yamlConfig: []byte("name: []\nvalue: true\ntype: bool\n"),
2023 wantErrT: newErrUnmarshal(&NameStringValuePair{}),
2024 },
2025 {
2026 name: "missing required name field",
2027 jsonConfig: []byte(`{}`),
2028 yamlConfig: []byte("{}"),
2029 wantErrT: newErrRequired(&NameStringValuePair{}, "name"),
2030 },
2031 {
2032 name: "missing required value field",
2033 jsonConfig: []byte(`{"name":"test"}`),
2034 yamlConfig: []byte("name: test"),
2035 wantErrT: newErrRequired(&NameStringValuePair{}, "value"),
2036 },
2037 {
2038 name: "invalid array name",
2039 jsonConfig: []byte(`{"name":[], "value": ["test-val", "test-val-2"], "type": "string_array"}`),
2040 yamlConfig: []byte("name: []\nvalue: [test-val, test-val-2]\ntype: string_array\n"),
2041 wantErrT: newErrUnmarshal(&NameStringValuePair{}),
2042 },
2043 {
2044 name: "valid string value",
2045 jsonConfig: []byte(`{"name":"test", "value": "test-val", "type": "string"}`),
2046 yamlConfig: []byte("name: test\nvalue: test-val\ntype: string\n"),
2047 wantNameStringValuePair: NameStringValuePair{
2048 Name: "test",
2049 Value: ptr("test-val"),
2050 },
2051 },
2052 {
2053 name: "invalid string_array value",
2054 jsonConfig: []byte(`{"name":"test", "value": ["test-val", "test-val-2"], "type": "string_array"}`),
2055 yamlConfig: []byte("name: test\nvalue: [test-val, test-val-2]\ntype: string_array\n"),
2056 wantErrT: newErrUnmarshal(&NameStringValuePair{}),
2057 },
2058 } {
2059 t.Run(tt.name, func(t *testing.T) {
2060 val := NameStringValuePair{}
2061 err := val.UnmarshalJSON(tt.jsonConfig)
2062 assert.ErrorIs(t, err, tt.wantErrT)
2063 assert.Equal(t, tt.wantNameStringValuePair, val)
2064
2065 val = NameStringValuePair{}
2066 err = yaml.Unmarshal(tt.yamlConfig, &val)
2067 assert.ErrorIs(t, err, tt.wantErrT)
2068 assert.Equal(t, tt.wantNameStringValuePair, val)
2069 })
2070 }
2071 }
2072
2073 func TestUnmarshalInstrumentType(t *testing.T) {
2074 var instrumentType InstrumentType
2075 for _, tt := range []struct {
2076 name string
2077 yamlConfig []byte
2078 jsonConfig []byte
2079 wantErrT error
2080 wantInstrumentType InstrumentType
2081 }{
2082 {
2083 name: "invalid data",
2084 jsonConfig: []byte(`{:2000}`),
2085 yamlConfig: []byte("name: []\nvalue: true\ntype: bool\n"),
2086 wantErrT: newErrUnmarshal(&instrumentType),
2087 },
2088 {
2089 name: "invalid instrument type",
2090 jsonConfig: []byte(`"test"`),
2091 yamlConfig: []byte("test"),
2092 wantErrT: newErrInvalid(`invalid selector (expected one of []interface {}{"counter", "gauge", "histogram", "observable_counter", "observable_gauge", "observable_up_down_counter", "up_down_counter"}): "test""`),
2093 },
2094 {
2095 name: "valid instrument type",
2096 jsonConfig: []byte(`"counter"`),
2097 yamlConfig: []byte("counter"),
2098 wantInstrumentType: InstrumentTypeCounter,
2099 },
2100 } {
2101 t.Run(tt.name, func(t *testing.T) {
2102 val := InstrumentType("")
2103 err := val.UnmarshalJSON(tt.jsonConfig)
2104 assert.ErrorIs(t, err, tt.wantErrT)
2105 assert.Equal(t, tt.wantInstrumentType, val)
2106
2107 val = InstrumentType("")
2108 err = yaml.Unmarshal(tt.yamlConfig, &val)
2109 assert.ErrorIs(t, err, tt.wantErrT)
2110 assert.Equal(t, tt.wantInstrumentType, val)
2111 })
2112 }
2113 }
2114
2115 func TestUnmarshalExperimentalPeerServiceMappingType(t *testing.T) {
2116 for _, tt := range []struct {
2117 name string
2118 yamlConfig []byte
2119 jsonConfig []byte
2120 wantErrT error
2121 wantExperimentalPeerServiceMapping ExperimentalPeerServiceMapping
2122 }{
2123 {
2124 name: "invalid data",
2125 jsonConfig: []byte(`{:2000}`),
2126 yamlConfig: []byte("peer: []\nservice: true"),
2127 wantErrT: newErrUnmarshal(&ExperimentalPeerServiceMapping{}),
2128 },
2129 {
2130 name: "missing required peer field",
2131 jsonConfig: []byte(`{}`),
2132 yamlConfig: []byte("{}"),
2133 wantErrT: newErrRequired(&ExperimentalPeerServiceMapping{}, "peer"),
2134 },
2135 {
2136 name: "missing required service field",
2137 jsonConfig: []byte(`{"peer":"test"}`),
2138 yamlConfig: []byte("peer: test"),
2139 wantErrT: newErrRequired(&ExperimentalPeerServiceMapping{}, "service"),
2140 },
2141 {
2142 name: "invalid string_array peer",
2143 jsonConfig: []byte(`{"peer":[], "service": ["test-val", "test-val-2"], "type": "string_array"}`),
2144 yamlConfig: []byte("peer: []\nservice: [test-val, test-val-2]\ntype: string_array\n"),
2145 wantErrT: newErrUnmarshal(&ExperimentalPeerServiceMapping{}),
2146 },
2147 {
2148 name: "valid string service",
2149 jsonConfig: []byte(`{"peer":"test", "service": "test-val"}`),
2150 yamlConfig: []byte("peer: test\nservice: test-val"),
2151 wantExperimentalPeerServiceMapping: ExperimentalPeerServiceMapping{
2152 Peer: "test",
2153 Service: "test-val",
2154 },
2155 },
2156 {
2157 name: "invalid string_array service",
2158 jsonConfig: []byte(`{"peer":"test", "service": ["test-val", "test-val-2"], "type": "string_array"}`),
2159 yamlConfig: []byte("peer: test\nservice: [test-val, test-val-2]\ntype: string_array\n"),
2160 wantErrT: newErrUnmarshal(&ExperimentalPeerServiceMapping{}),
2161 },
2162 } {
2163 t.Run(tt.name, func(t *testing.T) {
2164 val := ExperimentalPeerServiceMapping{}
2165 err := val.UnmarshalJSON(tt.jsonConfig)
2166 assert.ErrorIs(t, err, tt.wantErrT)
2167 assert.Equal(t, tt.wantExperimentalPeerServiceMapping, val)
2168
2169 val = ExperimentalPeerServiceMapping{}
2170 err = yaml.Unmarshal(tt.yamlConfig, &val)
2171 assert.ErrorIs(t, err, tt.wantErrT)
2172 assert.Equal(t, tt.wantExperimentalPeerServiceMapping, val)
2173 })
2174 }
2175 }
2176
2177 func TestUnmarshalExporterDefaultHistogramAggregation(t *testing.T) {
2178 var exporterDefaultHistogramAggregation ExporterDefaultHistogramAggregation
2179 for _, tt := range []struct {
2180 name string
2181 yamlConfig []byte
2182 jsonConfig []byte
2183 wantErrT error
2184 wantExporterDefaultHistogramAggregation ExporterDefaultHistogramAggregation
2185 }{
2186 {
2187 name: "invalid data",
2188 jsonConfig: []byte(`{:2000}`),
2189 yamlConfig: []byte("name: []\nvalue: true\ntype: bool\n"),
2190 wantErrT: newErrUnmarshal(&exporterDefaultHistogramAggregation),
2191 },
2192 {
2193 name: "invalid histogram aggregation",
2194 jsonConfig: []byte(`"test"`),
2195 yamlConfig: []byte("test"),
2196 wantErrT: newErrInvalid(`invalid histogram aggregation (expected one of []interface {}{"explicit_bucket_histogram", "base2_exponential_bucket_histogram"}): "test""`),
2197 },
2198 {
2199 name: "valid histogram aggregation",
2200 jsonConfig: []byte(`"base2_exponential_bucket_histogram"`),
2201 yamlConfig: []byte("base2_exponential_bucket_histogram"),
2202 wantExporterDefaultHistogramAggregation: ExporterDefaultHistogramAggregationBase2ExponentialBucketHistogram,
2203 },
2204 } {
2205 t.Run(tt.name, func(t *testing.T) {
2206 val := ExporterDefaultHistogramAggregation("")
2207 err := val.UnmarshalJSON(tt.jsonConfig)
2208 assert.ErrorIs(t, err, tt.wantErrT)
2209 assert.Equal(t, tt.wantExporterDefaultHistogramAggregation, val)
2210
2211 val = ExporterDefaultHistogramAggregation("")
2212 err = yaml.Unmarshal(tt.yamlConfig, &val)
2213 assert.ErrorIs(t, err, tt.wantErrT)
2214 assert.Equal(t, tt.wantExporterDefaultHistogramAggregation, val)
2215 })
2216 }
2217 }
2218
2219 func TestUnmarshalPullMetricReader(t *testing.T) {
2220 for _, tt := range []struct {
2221 name string
2222 yamlConfig []byte
2223 jsonConfig []byte
2224 wantErrT error
2225 wantExporter PullMetricExporter
2226 }{
2227 {
2228 name: "valid with proemtheus exporter",
2229 jsonConfig: []byte(`{"exporter":{"prometheus/development":{}}}`),
2230 yamlConfig: []byte("exporter:\n prometheus/development: {}"),
2231 wantExporter: PullMetricExporter{PrometheusDevelopment: &ExperimentalPrometheusMetricExporter{}},
2232 },
2233 {
2234 name: "missing required exporter field",
2235 jsonConfig: []byte(`{}`),
2236 yamlConfig: []byte("{}"),
2237 wantErrT: newErrRequired(&PullMetricReader{}, "exporter"),
2238 },
2239 {
2240 name: "invalid data",
2241 jsonConfig: []byte(`{:2000}`),
2242 yamlConfig: []byte("exporter:\n prometheus/development: []"),
2243 wantErrT: newErrUnmarshal(&PullMetricReader{}),
2244 },
2245 } {
2246 t.Run(tt.name, func(t *testing.T) {
2247 cl := PullMetricReader{}
2248 err := cl.UnmarshalJSON(tt.jsonConfig)
2249 assert.ErrorIs(t, err, tt.wantErrT)
2250 assert.Equal(t, tt.wantExporter, cl.Exporter)
2251
2252 cl = PullMetricReader{}
2253 err = yaml.Unmarshal(tt.yamlConfig, &cl)
2254 assert.ErrorIs(t, err, tt.wantErrT)
2255 assert.Equal(t, tt.wantExporter, cl.Exporter)
2256 })
2257 }
2258 }
2259
2260 func TestUnmarshalResourceJson(t *testing.T) {
2261 for _, tt := range []struct {
2262 name string
2263 yamlConfig []byte
2264 jsonConfig []byte
2265 wantErrT error
2266 wantResource Resource
2267 }{
2268 {
2269 name: "valid with all detectors",
2270 jsonConfig: []byte(`{"detection/development": {"detectors": [{"container": null},{"host": null},{"process": null},{"service": null}]}}`),
2271 yamlConfig: []byte("detection/development:\n detectors:\n - container:\n - host:\n - process:\n - service:"),
2272 wantResource: Resource{
2273 DetectionDevelopment: &ExperimentalResourceDetection{
2274 Detectors: []ExperimentalResourceDetector{
2275 {
2276 Container: ExperimentalContainerResourceDetector{},
2277 },
2278 {
2279 Host: ExperimentalHostResourceDetector{},
2280 },
2281 {
2282 Process: ExperimentalProcessResourceDetector{},
2283 },
2284 {
2285 Service: ExperimentalServiceResourceDetector{},
2286 },
2287 },
2288 },
2289 },
2290 },
2291 {
2292 name: "valid non-nil with all detectors",
2293 jsonConfig: []byte(`{"detection/development": {"detectors": [{"container": {}},{"host": {}},{"process": {}},{"service": {}}]}}`),
2294 yamlConfig: []byte("detection/development:\n detectors:\n - container: {}\n - host: {}\n - process: {}\n - service: {}"),
2295 wantResource: Resource{
2296 DetectionDevelopment: &ExperimentalResourceDetection{
2297 Detectors: []ExperimentalResourceDetector{
2298 {
2299 Container: ExperimentalContainerResourceDetector{},
2300 },
2301 {
2302 Host: ExperimentalHostResourceDetector{},
2303 },
2304 {
2305 Process: ExperimentalProcessResourceDetector{},
2306 },
2307 {
2308 Service: ExperimentalServiceResourceDetector{},
2309 },
2310 },
2311 },
2312 },
2313 },
2314 {
2315 name: "invalid container detector",
2316 jsonConfig: []byte(`{"detection/development": {"detectors": [{"container": 1}]}}`),
2317 yamlConfig: []byte("detection/development:\n detectors:\n - container: 1"),
2318 wantResource: Resource{
2319 DetectionDevelopment: &ExperimentalResourceDetection{
2320 Detectors: []ExperimentalResourceDetector{
2321 {},
2322 },
2323 },
2324 },
2325 wantErrT: newErrUnmarshal(&ExperimentalResourceDetector{}),
2326 },
2327 {
2328 name: "invalid host detector",
2329 jsonConfig: []byte(`{"detection/development": {"detectors": [{"host": 1}]}}`),
2330 yamlConfig: []byte("detection/development:\n detectors:\n - host: 1"),
2331 wantResource: Resource{
2332 DetectionDevelopment: &ExperimentalResourceDetection{
2333 Detectors: []ExperimentalResourceDetector{
2334 {},
2335 },
2336 },
2337 },
2338 wantErrT: newErrUnmarshal(&ExperimentalResourceDetector{}),
2339 },
2340 {
2341 name: "invalid service detector",
2342 jsonConfig: []byte(`{"detection/development": {"detectors": [{"service": 1}]}}`),
2343 yamlConfig: []byte("detection/development:\n detectors:\n - service: 1"),
2344 wantResource: Resource{
2345 DetectionDevelopment: &ExperimentalResourceDetection{
2346 Detectors: []ExperimentalResourceDetector{
2347 {},
2348 },
2349 },
2350 },
2351 wantErrT: newErrUnmarshal(&ExperimentalResourceDetector{}),
2352 },
2353 {
2354 name: "invalid process detector",
2355 jsonConfig: []byte(`{"detection/development": {"detectors": [{"process": 1}]}}`),
2356 yamlConfig: []byte("detection/development:\n detectors:\n - process: 1"),
2357 wantResource: Resource{
2358 DetectionDevelopment: &ExperimentalResourceDetection{
2359 Detectors: []ExperimentalResourceDetector{
2360 {},
2361 },
2362 },
2363 },
2364 wantErrT: newErrUnmarshal(&ExperimentalResourceDetector{}),
2365 },
2366 } {
2367 t.Run(tt.name, func(t *testing.T) {
2368 r := Resource{}
2369 err := json.Unmarshal(tt.jsonConfig, &r)
2370 assert.ErrorIs(t, err, tt.wantErrT)
2371 assert.Equal(t, tt.wantResource, r)
2372
2373 r = Resource{}
2374 err = yaml.Unmarshal(tt.yamlConfig, &r)
2375 assert.ErrorIs(t, err, tt.wantErrT)
2376 assert.Equal(t, tt.wantResource, r)
2377 })
2378 }
2379 }