Debian Codegraph @

otelconf/config_yaml.go

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