Debian Codegraph @

otelconf/x/config_yaml.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package x // import "go.opentelemetry.io/contrib/otelconf/x"
5
6 import (
7 "errors"
8 "fmt"
9 "reflect"
10
11 "go.yaml.in/yaml/v3"
12 )
13
14 // hasYAMLMapKey reports whether the provided mapping node contains the given
15 // key. It assumes the node is a mapping node and performs a linear scan of its
16 // key nodes.
17 func hasYAMLMapKey(node *yaml.Node, key string) bool {
18 if node == nil || node.Kind != yaml.MappingNode {
19 return false
20 }
21 for i := 0; i+1 < len(node.Content); i += 2 {
22 if node.Content[i].Kind == yaml.ScalarNode && node.Content[i].Value == key {
23 return true
24 }
25 }
26 return false
27 }
28
29 // UnmarshalYAML implements yaml.Unmarshaler.
30 func (j *ExperimentalResourceDetector) UnmarshalYAML(node *yaml.Node) error {
31 type Plain ExperimentalResourceDetector
32 var plain Plain
33 if err := node.Decode(&plain); err != nil {
34 return errors.Join(newErrUnmarshal(j), err)
35 }
36 // container can be nil, must check and set here
37 if hasYAMLMapKey(node, "container") && plain.Container == nil {
38 plain.Container = ExperimentalContainerResourceDetector{}
39 }
40 // host can be nil, must check and set here
41 if hasYAMLMapKey(node, "host") && plain.Host == nil {
42 plain.Host = ExperimentalHostResourceDetector{}
43 }
44 // process can be nil, must check and set here
45 if hasYAMLMapKey(node, "process") && plain.Process == nil {
46 plain.Process = ExperimentalProcessResourceDetector{}
47 }
48 // service can be nil, must check and set here
49 if hasYAMLMapKey(node, "service") && plain.Service == nil {
50 plain.Service = ExperimentalServiceResourceDetector{}
51 }
52 *j = ExperimentalResourceDetector(plain)
53 return nil
54 }
55
56 // UnmarshalYAML implements yaml.Unmarshaler.
57 func (j *PushMetricExporter) UnmarshalYAML(node *yaml.Node) error {
58 type Plain PushMetricExporter
59 var plain Plain
60 if err := node.Decode(&plain); err != nil {
61 return errors.Join(newErrUnmarshal(j), err)
62 }
63 // console can be nil, must check and set here
64 if hasYAMLMapKey(node, "console") && plain.Console == nil {
65 plain.Console = &ConsoleMetricExporter{}
66 }
67 *j = PushMetricExporter(plain)
68 return nil
69 }
70
71 // UnmarshalYAML implements yaml.Unmarshaler.
72 func (j *OpenTelemetryConfiguration) UnmarshalYAML(node *yaml.Node) error {
73 if !hasYAMLMapKey(node, "file_format") {
74 return newErrRequired(j, "file_format")
75 }
76 type Plain OpenTelemetryConfiguration
77 type shadow struct {
78 Plain
79 LogLevel *SeverityNumber `yaml:"log_level,omitempty"`
80 AttributeLimits *AttributeLimits `yaml:"attribute_limits,omitempty"`
81 Disabled *bool `yaml:"disabled,omitempty"`
82 FileFormat string `yaml:"file_format"`
83 LoggerProvider *LoggerProvider `yaml:"logger_provider,omitempty"`
84 MeterProvider *MeterProvider `yaml:"meter_provider,omitempty"`
85 TracerProvider *TracerProvider `yaml:"tracer_provider,omitempty"`
86 Propagator *Propagator `yaml:"propagator,omitempty"`
87 Resource *Resource `yaml:"resource,omitempty"`
88 InstrumentationDevelopment *ExperimentalInstrumentation `yaml:"instrumentation/development"`
89 }
90 var sh shadow
91
92 if err := node.Decode(&sh); err != nil {
93 return errors.Join(newErrUnmarshal(j), err)
94 }
95
96 if sh.AttributeLimits != nil {
97 sh.Plain.AttributeLimits = sh.AttributeLimits
98 }
99
100 sh.Plain.FileFormat = sh.FileFormat
101 if sh.Disabled != nil {
102 sh.Plain.Disabled = sh.Disabled
103 } else {
104 // Configure the log level of the internal logger used by the SDK.
105 // If omitted, info is used.
106 sh.Plain.Disabled = ptr(false)
107 }
108 if sh.LoggerProvider != nil {
109 sh.Plain.LoggerProvider = sh.LoggerProvider
110 }
111 if sh.MeterProvider != nil {
112 sh.Plain.MeterProvider = sh.MeterProvider
113 }
114 if sh.TracerProvider != nil {
115 sh.Plain.TracerProvider = sh.TracerProvider
116 }
117 if sh.Propagator != nil {
118 sh.Plain.Propagator = sh.Propagator
119 }
120 if sh.Resource != nil {
121 sh.Plain.Resource = sh.Resource
122 }
123 if sh.InstrumentationDevelopment != nil {
124 sh.Plain.InstrumentationDevelopment = sh.InstrumentationDevelopment
125 }
126
127 if sh.LogLevel != nil {
128 sh.Plain.LogLevel = sh.LogLevel
129 } else {
130 // Configure the log level of the internal logger used by the SDK.
131 // If omitted, info is used.
132 sh.Plain.LogLevel = ptr(SeverityNumberInfo)
133 }
134
135 *j = OpenTelemetryConfiguration(sh.Plain)
136 return nil
137 }
138
139 // UnmarshalYAML implements yaml.Unmarshaler.
140 func (j *SpanExporter) UnmarshalYAML(node *yaml.Node) error {
141 type Plain SpanExporter
142 var plain Plain
143 if err := node.Decode(&plain); err != nil {
144 return errors.Join(newErrUnmarshal(j), err)
145 }
146 // console can be nil, must check and set here
147 if hasYAMLMapKey(node, "console") && plain.Console == nil {
148 plain.Console = ConsoleExporter{}
149 }
150 *j = SpanExporter(plain)
151 return nil
152 }
153
154 // UnmarshalYAML implements yaml.Unmarshaler.
155 func (j *LogRecordExporter) UnmarshalYAML(node *yaml.Node) error {
156 type Plain LogRecordExporter
157 var plain Plain
158 if err := node.Decode(&plain); err != nil {
159 return errors.Join(newErrUnmarshal(j), err)
160 }
161 // console can be nil, must check and set here
162 if hasYAMLMapKey(node, "console") && plain.Console == nil {
163 plain.Console = ConsoleExporter{}
164 }
165 *j = LogRecordExporter(plain)
166 return nil
167 }
168
169 // UnmarshalYAML implements yaml.Unmarshaler.
170 func (j *TextMapPropagator) UnmarshalYAML(node *yaml.Node) error {
171 type Plain TextMapPropagator
172 var plain Plain
173 if err := node.Decode(&plain); err != nil {
174 return errors.Join(newErrUnmarshal(j), err)
175 }
176 // b3 can be nil, must check and set here
177 if hasYAMLMapKey(node, "b3") && plain.B3 == nil {
178 plain.B3 = B3Propagator{}
179 }
180 // b3multi can be nil, must check and set here
181 if hasYAMLMapKey(node, "b3multi") && plain.B3Multi == nil {
182 plain.B3Multi = B3MultiPropagator{}
183 }
184 // baggage can be nil, must check and set here
185 if hasYAMLMapKey(node, "baggage") && plain.Baggage == nil {
186 plain.Baggage = BaggagePropagator{}
187 }
188 // jaeger can be nil, must check and set here
189 if hasYAMLMapKey(node, "jaeger") && plain.Jaeger == nil {
190 plain.Jaeger = JaegerPropagator{}
191 }
192 // ottrace can be nil, must check and set here
193 if hasYAMLMapKey(node, "ottrace") && plain.Ottrace == nil {
194 plain.Ottrace = OpenTracingPropagator{}
195 }
196 // tracecontext can be nil, must check and set here
197 if hasYAMLMapKey(node, "tracecontext") && plain.Tracecontext == nil {
198 plain.Tracecontext = TraceContextPropagator{}
199 }
200 *j = TextMapPropagator(plain)
201 return nil
202 }
203
204 // UnmarshalYAML implements yaml.Unmarshaler.
205 func (j *BatchLogRecordProcessor) UnmarshalYAML(node *yaml.Node) error {
206 if !hasYAMLMapKey(node, "exporter") {
207 return newErrRequired(j, "exporter")
208 }
209 type Plain BatchLogRecordProcessor
210 var plain Plain
211 if err := node.Decode(&plain); err != nil {
212 return errors.Join(newErrUnmarshal(j), err)
213 }
214 if err := validateBatchLogRecordProcessor((*BatchLogRecordProcessor)(&plain)); err != nil {
215 return err
216 }
217 *j = BatchLogRecordProcessor(plain)
218 return nil
219 }
220
221 // UnmarshalYAML implements yaml.Unmarshaler.
222 func (j *Sampler) UnmarshalYAML(node *yaml.Node) error {
223 var raw map[string]any
224 if err := node.Decode(&raw); err != nil {
225 return err
226 }
227 type Plain Sampler
228 var plain Plain
229 if err := node.Decode(&plain); err != nil {
230 return err
231 }
232 unmarshalSamplerTypes(raw, (*Sampler)(&plain))
233 *j = Sampler(plain)
234 return nil
235 }
236
237 // UnmarshalYAML implements yaml.Unmarshaler.
238 func (j *MetricProducer) UnmarshalYAML(node *yaml.Node) error {
239 var raw map[string]any
240 if err := node.Decode(&raw); err != nil {
241 return err
242 }
243 type Plain MetricProducer
244 var plain Plain
245 if err := node.Decode(&plain); err != nil {
246 return err
247 }
248 unmarshalMetricProducer(raw, (*MetricProducer)(&plain))
249 *j = MetricProducer(plain)
250 return nil
251 }
252
253 // UnmarshalYAML implements yaml.Unmarshaler.
254 func (j *BatchSpanProcessor) UnmarshalYAML(node *yaml.Node) error {
255 if !hasYAMLMapKey(node, "exporter") {
256 return newErrRequired(j, "exporter")
257 }
258 type Plain BatchSpanProcessor
259 var plain Plain
260 if err := node.Decode(&plain); err != nil {
261 return errors.Join(newErrUnmarshal(j), err)
262 }
263 if err := validateBatchSpanProcessor((*BatchSpanProcessor)(&plain)); err != nil {
264 return err
265 }
266 *j = BatchSpanProcessor(plain)
267 return nil
268 }
269
270 // UnmarshalYAML implements yaml.Unmarshaler.
271 func (j *PeriodicMetricReader) UnmarshalYAML(node *yaml.Node) error {
272 if !hasYAMLMapKey(node, "exporter") {
273 return newErrRequired(j, "exporter")
274 }
275 type Plain PeriodicMetricReader
276 var plain Plain
277 if err := node.Decode(&plain); err != nil {
278 return errors.Join(newErrUnmarshal(j), err)
279 }
280 if err := validatePeriodicMetricReader((*PeriodicMetricReader)(&plain)); err != nil {
281 return err
282 }
283 *j = PeriodicMetricReader(plain)
284 return nil
285 }
286
287 // UnmarshalYAML implements yaml.Unmarshaler.
288 func (j *CardinalityLimits) UnmarshalYAML(node *yaml.Node) error {
289 type Plain CardinalityLimits
290 var plain Plain
291 if err := node.Decode(&plain); err != nil {
292 return errors.Join(newErrUnmarshal(j), err)
293 }
294 if err := validateCardinalityLimits((*CardinalityLimits)(&plain)); err != nil {
295 return err
296 }
297 *j = CardinalityLimits(plain)
298 return nil
299 }
300
301 // UnmarshalYAML implements yaml.Unmarshaler.
302 func (j *SpanLimits) UnmarshalYAML(node *yaml.Node) error {
303 type Plain SpanLimits
304 var plain Plain
305 if err := node.Decode(&plain); err != nil {
306 return errors.Join(newErrUnmarshal(j), err)
307 }
308 if err := validateSpanLimits((*SpanLimits)(&plain)); err != nil {
309 return err
310 }
311 *j = SpanLimits(plain)
312 return nil
313 }
314
315 // UnmarshalYAML implements yaml.Unmarshaler.
316 func (j *OTLPHttpMetricExporter) UnmarshalYAML(node *yaml.Node) error {
317 if !hasYAMLMapKey(node, "endpoint") {
318 return newErrRequired(j, "endpoint")
319 }
320 type Plain OTLPHttpMetricExporter
321 var plain Plain
322 if err := node.Decode(&plain); err != nil {
323 return errors.Join(newErrUnmarshal(j), err)
324 }
325 if plain.Timeout != nil && 0 > *plain.Timeout {
326 return newErrGreaterOrEqualZero("timeout")
327 }
328 *j = OTLPHttpMetricExporter(plain)
329 return nil
330 }
331
332 // UnmarshalYAML implements yaml.Unmarshaler.
333 func (j *OTLPGrpcMetricExporter) UnmarshalYAML(node *yaml.Node) error {
334 if !hasYAMLMapKey(node, "endpoint") {
335 return newErrRequired(j, "endpoint")
336 }
337 type Plain OTLPGrpcMetricExporter
338 var plain Plain
339 if err := node.Decode(&plain); err != nil {
340 return errors.Join(newErrUnmarshal(j), err)
341 }
342 if plain.Timeout != nil && 0 > *plain.Timeout {
343 return newErrGreaterOrEqualZero("timeout")
344 }
345 *j = OTLPGrpcMetricExporter(plain)
346 return nil
347 }
348
349 // UnmarshalYAML implements yaml.Unmarshaler.
350 func (j *OTLPHttpExporter) UnmarshalYAML(node *yaml.Node) error {
351 if !hasYAMLMapKey(node, "endpoint") {
352 return newErrRequired(j, "endpoint")
353 }
354 type Plain OTLPHttpExporter
355 var plain Plain
356 if err := node.Decode(&plain); err != nil {
357 return errors.Join(newErrUnmarshal(j), err)
358 }
359 if plain.Timeout != nil && 0 > *plain.Timeout {
360 return newErrGreaterOrEqualZero("timeout")
361 }
362 *j = OTLPHttpExporter(plain)
363 return nil
364 }
365
366 // UnmarshalYAML implements yaml.Unmarshaler.
367 func (j *OTLPGrpcExporter) UnmarshalYAML(node *yaml.Node) error {
368 if !hasYAMLMapKey(node, "endpoint") {
369 return newErrRequired(j, "endpoint")
370 }
371 type Plain OTLPGrpcExporter
372 var plain Plain
373 if err := node.Decode(&plain); err != nil {
374 return errors.Join(newErrUnmarshal(j), err)
375 }
376 if plain.Timeout != nil && 0 > *plain.Timeout {
377 return newErrGreaterOrEqualZero("timeout")
378 }
379 *j = OTLPGrpcExporter(plain)
380 return nil
381 }
382
383 // UnmarshalYAML implements yaml.Unmarshaler.
384 func (j *AttributeType) UnmarshalYAML(node *yaml.Node) error {
385 var v string
386 if err := node.Decode(&v); err != nil {
387 return errors.Join(newErrUnmarshal(j), err)
388 }
389 var ok bool
390 for _, expected := range enumValuesAttributeType {
391 if reflect.DeepEqual(v, expected) {
392 ok = true
393 break
394 }
395 }
396 if !ok {
397 return newErrInvalid(fmt.Sprintf("unexpected value type %#v, expected one of %#v)", v, enumValuesAttributeType))
398 }
399 *j = AttributeType(v)
400 return nil
401 }
402
403 // UnmarshalYAML implements yaml.Unmarshaler.
404 func (j *AttributeNameValue) UnmarshalYAML(node *yaml.Node) error {
405 if !hasYAMLMapKey(node, "name") {
406 return newErrRequired(j, "name")
407 }
408 if !hasYAMLMapKey(node, "value") {
409 return newErrRequired(j, "value")
410 }
411 type Plain AttributeNameValue
412 var plain Plain
413 if err := node.Decode(&plain); err != nil {
414 return errors.Join(newErrUnmarshal(j), err)
415 }
416
417 // yaml unmarshaller defaults to unmarshalling to int
418 if plain.Type != nil && *plain.Type == AttributeTypeDouble {
419 val, ok := plain.Value.(int)
420 if ok {
421 plain.Value = float64(val)
422 }
423 }
424
425 if plain.Type != nil && *plain.Type == AttributeTypeDoubleArray {
426 m, ok := plain.Value.([]any)
427 if ok {
428 var vals []any
429 for _, v := range m {
430 val, ok := v.(int)
431 if ok {
432 vals = append(vals, float64(val))
433 } else {
434 vals = append(vals, v)
435 }
436 }
437 plain.Value = vals
438 }
439 }
440
441 *j = AttributeNameValue(plain)
442 return nil
443 }
444
445 // UnmarshalYAML implements yaml.Unmarshaler.
446 func (j *SimpleLogRecordProcessor) UnmarshalYAML(node *yaml.Node) error {
447 if !hasYAMLMapKey(node, "exporter") {
448 return newErrRequired(j, "exporter")
449 }
450 type Plain SimpleLogRecordProcessor
451 var plain Plain
452 if err := node.Decode(&plain); err != nil {
453 return errors.Join(newErrUnmarshal(j), err)
454 }
455 *j = SimpleLogRecordProcessor(plain)
456 return nil
457 }
458
459 // UnmarshalYAML implements yaml.Unmarshaler.
460 func (j *SimpleSpanProcessor) UnmarshalYAML(node *yaml.Node) error {
461 if !hasYAMLMapKey(node, "exporter") {
462 return newErrRequired(j, "exporter")
463 }
464 type Plain SimpleSpanProcessor
465 var plain Plain
466 if err := node.Decode(&plain); err != nil {
467 return errors.Join(newErrUnmarshal(j), err)
468 }
469 *j = SimpleSpanProcessor(plain)
470 return nil
471 }
472
473 // UnmarshalYAML implements yaml.Unmarshaler.
474 func (j *NameStringValuePair) UnmarshalYAML(node *yaml.Node) error {
475 if !hasYAMLMapKey(node, "name") {
476 return newErrRequired(j, "name")
477 }
478 if !hasYAMLMapKey(node, "value") {
479 return newErrRequired(j, "value")
480 }
481
482 type Plain NameStringValuePair
483 var plain Plain
484 if err := node.Decode(&plain); err != nil {
485 return errors.Join(newErrUnmarshal(j), err)
486 }
487
488 *j = NameStringValuePair(plain)
489 return nil
490 }
491
492 // UnmarshalYAML implements yaml.Unmarshaler.
493 func (j *InstrumentType) UnmarshalYAML(node *yaml.Node) error {
494 type Plain InstrumentType
495 var plain Plain
496 if err := node.Decode(&plain); err != nil {
497 return errors.Join(newErrUnmarshal(j), err)
498 }
499 if err := supportedInstrumentType(InstrumentType(plain)); err != nil {
500 return err
501 }
502
503 *j = InstrumentType(plain)
504 return nil
505 }
506
507 // UnmarshalYAML implements yaml.Unmarshaler.
508 func (j *ExperimentalPeerServiceMapping) UnmarshalYAML(node *yaml.Node) error {
509 if !hasYAMLMapKey(node, "peer") {
510 return newErrRequired(j, "peer")
511 }
512 if !hasYAMLMapKey(node, "service") {
513 return newErrRequired(j, "service")
514 }
515
516 type Plain ExperimentalPeerServiceMapping
517 var plain Plain
518 if err := node.Decode(&plain); err != nil {
519 return errors.Join(newErrUnmarshal(j), err)
520 }
521
522 *j = ExperimentalPeerServiceMapping(plain)
523 return nil
524 }
525
526 // UnmarshalYAML implements yaml.Unmarshaler.
527 func (j *ExporterDefaultHistogramAggregation) UnmarshalYAML(node *yaml.Node) error {
528 type Plain ExporterDefaultHistogramAggregation
529 var plain Plain
530 if err := node.Decode(&plain); err != nil {
531 return errors.Join(newErrUnmarshal(j), err)
532 }
533 if err := supportedHistogramAggregation(ExporterDefaultHistogramAggregation(plain)); err != nil {
534 return err
535 }
536
537 *j = ExporterDefaultHistogramAggregation(plain)
538 return nil
539 }
540
541 // UnmarshalYAML implements yaml.Unmarshaler.
542 func (j *PullMetricReader) UnmarshalYAML(node *yaml.Node) error {
543 if !hasYAMLMapKey(node, "exporter") {
544 return newErrRequired(j, "exporter")
545 }
546 type Plain PullMetricReader
547 var plain Plain
548 if err := node.Decode(&plain); err != nil {
549 return errors.Join(newErrUnmarshal(j), err)
550 }
551 *j = PullMetricReader(plain)
552 return nil
553 }