Debian Codegraph @

otelconf/config_json.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 "encoding/json"
8 "errors"
9 "fmt"
10 "reflect"
11 )
12
13 // UnmarshalJSON implements json.Unmarshaler.
14 func (j *ConsoleExporter) UnmarshalJSON(b []byte) error {
15 type plain ConsoleExporter
16 var p plain
17 if err := json.Unmarshal(b, &p); err != nil {
18 return errors.Join(newErrUnmarshal(j), err)
19 }
20 // If key is present (even if empty object), ensure non-nil value.
21 if p == nil {
22 *j = ConsoleExporter{}
23 } else {
24 *j = ConsoleExporter(p)
25 }
26 return nil
27 }
28
29 // UnmarshalJSON implements json.Unmarshaler.
30 func (j *B3Propagator) UnmarshalJSON(b []byte) error {
31 type plain B3Propagator
32 var p plain
33 if err := json.Unmarshal(b, &p); err != nil {
34 return errors.Join(newErrUnmarshal(j), err)
35 }
36 // If key is present (even if empty object), ensure non-nil value.
37 if p == nil {
38 *j = B3Propagator{}
39 } else {
40 *j = B3Propagator(p)
41 }
42 return nil
43 }
44
45 // UnmarshalJSON implements json.Unmarshaler.
46 func (j *B3MultiPropagator) UnmarshalJSON(b []byte) error {
47 type plain B3MultiPropagator
48 var p plain
49 if err := json.Unmarshal(b, &p); err != nil {
50 return errors.Join(newErrUnmarshal(j), err)
51 }
52 // If key is present (even if empty object), ensure non-nil value.
53 if p == nil {
54 *j = B3MultiPropagator{}
55 } else {
56 *j = B3MultiPropagator(p)
57 }
58 return nil
59 }
60
61 // UnmarshalJSON implements json.Unmarshaler.
62 func (j *BaggagePropagator) UnmarshalJSON(b []byte) error {
63 type plain BaggagePropagator
64 var p plain
65 if err := json.Unmarshal(b, &p); err != nil {
66 return errors.Join(newErrUnmarshal(j), err)
67 }
68 // If key is present (even if empty object), ensure non-nil value.
69 if p == nil {
70 *j = BaggagePropagator{}
71 } else {
72 *j = BaggagePropagator(p)
73 }
74 return nil
75 }
76
77 // UnmarshalJSON implements json.Unmarshaler.
78 func (j *JaegerPropagator) UnmarshalJSON(b []byte) error {
79 type plain JaegerPropagator
80 var p plain
81 if err := json.Unmarshal(b, &p); err != nil {
82 return errors.Join(newErrUnmarshal(j), err)
83 }
84 // If key is present (even if empty object), ensure non-nil value.
85 if p == nil {
86 *j = JaegerPropagator{}
87 } else {
88 *j = JaegerPropagator(p)
89 }
90 return nil
91 }
92
93 // UnmarshalJSON implements json.Unmarshaler.
94 func (j *OpenTracingPropagator) UnmarshalJSON(b []byte) error {
95 type plain OpenTracingPropagator
96 var p plain
97 if err := json.Unmarshal(b, &p); err != nil {
98 return errors.Join(newErrUnmarshal(j), err)
99 }
100 // If key is present (even if empty object), ensure non-nil value.
101 if p == nil {
102 *j = OpenTracingPropagator{}
103 } else {
104 *j = OpenTracingPropagator(p)
105 }
106 return nil
107 }
108
109 // UnmarshalJSON implements json.Unmarshaler.
110 func (j *TraceContextPropagator) UnmarshalJSON(b []byte) error {
111 type plain TraceContextPropagator
112 var p plain
113 if err := json.Unmarshal(b, &p); err != nil {
114 return errors.Join(newErrUnmarshal(j), err)
115 }
116 // If key is present (even if empty object), ensure non-nil value.
117 if p == nil {
118 *j = TraceContextPropagator{}
119 } else {
120 *j = TraceContextPropagator(p)
121 }
122 return nil
123 }
124
125 // UnmarshalJSON implements json.Unmarshaler.
126 func (j *PushMetricExporter) UnmarshalJSON(b []byte) error {
127 // Use a shadow struct with a RawMessage field to detect key presence.
128 type Plain PushMetricExporter
129 type shadow struct {
130 Plain
131 Console json.RawMessage `json:"console"`
132 }
133 var sh shadow
134 if err := json.Unmarshal(b, &sh); err != nil {
135 return errors.Join(newErrUnmarshal(j), err)
136 }
137
138 if sh.Console != nil {
139 var c ConsoleMetricExporter
140 if err := json.Unmarshal(sh.Console, &c); err != nil {
141 return err
142 }
143 sh.Plain.Console = &c
144 }
145 *j = PushMetricExporter(sh.Plain)
146 return nil
147 }
148
149 // UnmarshalJSON implements json.Unmarshaler.
150 func (j *SpanExporter) UnmarshalJSON(b []byte) error {
151 // Use a shadow struct with a RawMessage field to detect key presence.
152 type Plain SpanExporter
153 type shadow struct {
154 Plain
155 Console json.RawMessage `json:"console"`
156 }
157 var sh shadow
158 if err := json.Unmarshal(b, &sh); err != nil {
159 return errors.Join(newErrUnmarshal(j), err)
160 }
161
162 if sh.Console != nil {
163 var c ConsoleExporter
164 if err := json.Unmarshal(sh.Console, &c); err != nil {
165 return err
166 }
167 sh.Plain.Console = c
168 }
169 *j = SpanExporter(sh.Plain)
170 return nil
171 }
172
173 // UnmarshalJSON implements json.Unmarshaler.
174 func (j *LogRecordExporter) UnmarshalJSON(b []byte) error {
175 // Use a shadow struct with a RawMessage field to detect key presence.
176 type Plain LogRecordExporter
177 type shadow struct {
178 Plain
179 Console json.RawMessage `json:"console"`
180 }
181 var sh shadow
182 if err := json.Unmarshal(b, &sh); err != nil {
183 return errors.Join(newErrUnmarshal(j), err)
184 }
185
186 if sh.Console != nil {
187 var c ConsoleExporter
188 if err := json.Unmarshal(sh.Console, &c); err != nil {
189 return err
190 }
191 sh.Plain.Console = c
192 }
193 *j = LogRecordExporter(sh.Plain)
194 return nil
195 }
196
197 // UnmarshalJSON implements json.Unmarshaler.
198 func (j *TextMapPropagator) UnmarshalJSON(b []byte) error {
199 type Plain TextMapPropagator
200 type shadow struct {
201 Plain
202 B3 json.RawMessage `json:"b3"`
203 B3Multi json.RawMessage `json:"b3multi"`
204 Baggage json.RawMessage `json:"baggage"`
205 Jaeger json.RawMessage `json:"jaeger"`
206 Ottrace json.RawMessage `json:"ottrace"`
207 Tracecontext json.RawMessage `json:"tracecontext"`
208 }
209 var sh shadow
210 if err := json.Unmarshal(b, &sh); err != nil {
211 return errors.Join(newErrUnmarshal(j), err)
212 }
213
214 if sh.B3 != nil {
215 var p B3Propagator
216 if err := json.Unmarshal(sh.B3, &p); err != nil {
217 return errors.Join(newErrUnmarshal(j), err)
218 }
219 sh.Plain.B3 = p
220 }
221
222 if sh.B3Multi != nil {
223 var p B3MultiPropagator
224 if err := json.Unmarshal(sh.B3Multi, &p); err != nil {
225 return errors.Join(newErrUnmarshal(j), err)
226 }
227 sh.Plain.B3Multi = p
228 }
229
230 if sh.Baggage != nil {
231 var p BaggagePropagator
232 if err := json.Unmarshal(sh.Baggage, &p); err != nil {
233 return errors.Join(newErrUnmarshal(j), err)
234 }
235 sh.Plain.Baggage = p
236 }
237
238 if sh.Jaeger != nil {
239 var p JaegerPropagator
240 if err := json.Unmarshal(sh.Jaeger, &p); err != nil {
241 return errors.Join(newErrUnmarshal(j), err)
242 }
243 sh.Plain.Jaeger = p
244 }
245
246 if sh.Ottrace != nil {
247 var p OpenTracingPropagator
248 if err := json.Unmarshal(sh.Ottrace, &p); err != nil {
249 return errors.Join(newErrUnmarshal(j), err)
250 }
251 sh.Plain.Ottrace = p
252 }
253
254 if sh.Tracecontext != nil {
255 var p TraceContextPropagator
256 if err := json.Unmarshal(sh.Tracecontext, &p); err != nil {
257 return errors.Join(newErrUnmarshal(j), err)
258 }
259 sh.Plain.Tracecontext = p
260 }
261
262 *j = TextMapPropagator(sh.Plain)
263 return nil
264 }
265
266 // UnmarshalJSON implements json.Unmarshaler.
267 func (j *BatchLogRecordProcessor) UnmarshalJSON(b []byte) error {
268 type Plain BatchLogRecordProcessor
269 type shadow struct {
270 Plain
271 Exporter json.RawMessage `json:"exporter"`
272 }
273 var sh shadow
274 if err := json.Unmarshal(b, &sh); err != nil {
275 return errors.Join(newErrUnmarshal(j), err)
276 }
277 if sh.Exporter == nil {
278 return newErrRequired(j, "exporter")
279 }
280 // Hydrate the exporter into the underlying field.
281 if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
282 return err
283 }
284 if err := validateBatchLogRecordProcessor((*BatchLogRecordProcessor)(&sh.Plain)); err != nil {
285 return err
286 }
287 *j = BatchLogRecordProcessor(sh.Plain)
288 return nil
289 }
290
291 // UnmarshalJSON implements json.Unmarshaler.
292 func (j *BatchSpanProcessor) UnmarshalJSON(b []byte) error {
293 type Plain BatchSpanProcessor
294 type shadow struct {
295 Plain
296 Exporter json.RawMessage `json:"exporter"`
297 }
298 var sh shadow
299 if err := json.Unmarshal(b, &sh); err != nil {
300 return errors.Join(newErrUnmarshal(j), err)
301 }
302 if sh.Exporter == nil {
303 return newErrRequired(j, "exporter")
304 }
305 // Hydrate the exporter into the underlying field.
306 if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
307 return err
308 }
309 if err := validateBatchSpanProcessor((*BatchSpanProcessor)(&sh.Plain)); err != nil {
310 return err
311 }
312 *j = BatchSpanProcessor(sh.Plain)
313 return nil
314 }
315
316 // UnmarshalJSON implements json.Unmarshaler.
317 func (j *OpenTelemetryConfiguration) UnmarshalJSON(b []byte) error {
318 type Plain OpenTelemetryConfiguration
319 type shadow struct {
320 Plain
321 FileFormat json.RawMessage `json:"file_format"`
322 LoggerProvider json.RawMessage `json:"logger_provider"`
323 MeterProvider json.RawMessage `json:"meter_provider"`
324 TracerProvider json.RawMessage `json:"tracer_provider"`
325 Propagator json.RawMessage `json:"propagator"`
326 Resource json.RawMessage `json:"resource"`
327 AttributeLimits json.RawMessage `json:"attribute_limits"`
328 Disabled json.RawMessage `json:"disabled"`
329 LogLevel json.RawMessage `json:"log_level"`
330 }
331 var sh shadow
332 if err := json.Unmarshal(b, &sh); err != nil {
333 return errors.Join(newErrUnmarshal(j), err)
334 }
335
336 if len(sh.FileFormat) == 0 {
337 return newErrRequired(j, "file_format")
338 }
339
340 if err := json.Unmarshal(sh.FileFormat, &sh.Plain.FileFormat); err != nil {
341 return errors.Join(newErrUnmarshal(j), err)
342 }
343
344 if sh.LoggerProvider != nil {
345 var l LoggerProvider
346 if err := json.Unmarshal(sh.LoggerProvider, &l); err != nil {
347 return errors.Join(newErrUnmarshal(j), err)
348 }
349 sh.Plain.LoggerProvider = &l
350 }
351
352 if sh.MeterProvider != nil {
353 var m MeterProvider
354 if err := json.Unmarshal(sh.MeterProvider, &m); err != nil {
355 return errors.Join(newErrUnmarshal(j), err)
356 }
357 sh.Plain.MeterProvider = &m
358 }
359
360 if sh.TracerProvider != nil {
361 var t TracerProvider
362 if err := json.Unmarshal(sh.TracerProvider, &t); err != nil {
363 return errors.Join(newErrUnmarshal(j), err)
364 }
365 sh.Plain.TracerProvider = &t
366 }
367
368 if sh.Propagator != nil {
369 var p Propagator
370 if err := json.Unmarshal(sh.Propagator, &p); err != nil {
371 return errors.Join(newErrUnmarshal(j), err)
372 }
373 sh.Plain.Propagator = &p
374 }
375
376 if sh.Resource != nil {
377 var r Resource
378 if err := json.Unmarshal(sh.Resource, &r); err != nil {
379 return errors.Join(newErrUnmarshal(j), err)
380 }
381 sh.Plain.Resource = &r
382 }
383
384 if sh.AttributeLimits != nil {
385 var r AttributeLimits
386 if err := json.Unmarshal(sh.AttributeLimits, &r); err != nil {
387 return errors.Join(newErrUnmarshal(j), err)
388 }
389 sh.Plain.AttributeLimits = &r
390 }
391
392 if sh.Disabled != nil {
393 if err := json.Unmarshal(sh.Disabled, &sh.Plain.Disabled); err != nil {
394 return errors.Join(newErrUnmarshal(j), err)
395 }
396 } else {
397 // Configure if the SDK is disabled or not.
398 // If omitted or null, false is used.
399 sh.Plain.Disabled = ptr(false)
400 }
401
402 if sh.LogLevel != nil {
403 if err := json.Unmarshal(sh.LogLevel, &sh.Plain.LogLevel); err != nil {
404 return errors.Join(newErrUnmarshal(j), err)
405 }
406 } else {
407 // Configure the log level of the internal logger used by the SDK.
408 // If omitted, info is used.
409 sh.Plain.LogLevel = ptr(SeverityNumberInfo)
410 }
411
412 *j = OpenTelemetryConfiguration(sh.Plain)
413 return nil
414 }
415
416 // UnmarshalJSON implements json.Unmarshaler.
417 func (j *PeriodicMetricReader) UnmarshalJSON(b []byte) error {
418 type Plain PeriodicMetricReader
419 type shadow struct {
420 Plain
421 Exporter json.RawMessage `json:"exporter"`
422 }
423 var sh shadow
424 if err := json.Unmarshal(b, &sh); err != nil {
425 return errors.Join(newErrUnmarshal(j), err)
426 }
427 if sh.Exporter == nil {
428 return newErrRequired(j, "exporter")
429 }
430 // Hydrate the exporter into the underlying field.
431 if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
432 return err
433 }
434 err := validatePeriodicMetricReader((*PeriodicMetricReader)(&sh.Plain))
435 if err != nil {
436 return err
437 }
438 *j = PeriodicMetricReader(sh.Plain)
439 return nil
440 }
441
442 // UnmarshalJSON implements json.Unmarshaler.
443 func (j *CardinalityLimits) UnmarshalJSON(value []byte) error {
444 type Plain CardinalityLimits
445 var plain Plain
446 if err := json.Unmarshal(value, &plain); err != nil {
447 return errors.Join(newErrUnmarshal(j), err)
448 }
449 if err := validateCardinalityLimits((*CardinalityLimits)(&plain)); err != nil {
450 return err
451 }
452 *j = CardinalityLimits(plain)
453 return nil
454 }
455
456 // UnmarshalJSON implements json.Unmarshaler.
457 func (j *SpanLimits) UnmarshalJSON(value []byte) error {
458 type Plain SpanLimits
459 var plain Plain
460 if err := json.Unmarshal(value, &plain); err != nil {
461 return errors.Join(newErrUnmarshal(j), err)
462 }
463 if err := validateSpanLimits((*SpanLimits)(&plain)); err != nil {
464 return err
465 }
466 *j = SpanLimits(plain)
467 return nil
468 }
469
470 // UnmarshalJSON implements json.Unmarshaler.
471 func (j *OTLPHttpMetricExporter) UnmarshalJSON(b []byte) error {
472 type Plain OTLPHttpMetricExporter
473 type shadow struct {
474 Plain
475 Endpoint json.RawMessage `json:"endpoint"`
476 }
477 var sh shadow
478 if err := json.Unmarshal(b, &sh); err != nil {
479 return errors.Join(newErrUnmarshal(j), err)
480 }
481 if sh.Endpoint == nil {
482 return newErrRequired(j, "endpoint")
483 }
484 if err := json.Unmarshal(sh.Endpoint, &sh.Plain.Endpoint); err != nil {
485 return err
486 }
487 if sh.Timeout != nil && 0 > *sh.Timeout {
488 return newErrGreaterOrEqualZero("timeout")
489 }
490 *j = OTLPHttpMetricExporter(sh.Plain)
491 return nil
492 }
493
494 // UnmarshalJSON implements json.Unmarshaler.
495 func (j *OTLPGrpcMetricExporter) UnmarshalJSON(b []byte) error {
496 type Plain OTLPGrpcMetricExporter
497 type shadow struct {
498 Plain
499 Endpoint json.RawMessage `json:"endpoint"`
500 }
501 var sh shadow
502 if err := json.Unmarshal(b, &sh); err != nil {
503 return errors.Join(newErrUnmarshal(j), err)
504 }
505 if sh.Endpoint == nil {
506 return newErrRequired(j, "endpoint")
507 }
508 if err := json.Unmarshal(sh.Endpoint, &sh.Plain.Endpoint); err != nil {
509 return err
510 }
511 if sh.Timeout != nil && 0 > *sh.Timeout {
512 return newErrGreaterOrEqualZero("timeout")
513 }
514 *j = OTLPGrpcMetricExporter(sh.Plain)
515 return nil
516 }
517
518 // UnmarshalJSON implements json.Unmarshaler.
519 func (j *OTLPHttpExporter) UnmarshalJSON(b []byte) error {
520 type Plain OTLPHttpExporter
521 type shadow struct {
522 Plain
523 Endpoint json.RawMessage `json:"endpoint"`
524 }
525 var sh shadow
526 if err := json.Unmarshal(b, &sh); err != nil {
527 return errors.Join(newErrUnmarshal(j), err)
528 }
529 if sh.Endpoint == nil {
530 return newErrRequired(j, "endpoint")
531 }
532 if err := json.Unmarshal(sh.Endpoint, &sh.Plain.Endpoint); err != nil {
533 return err
534 }
535 if sh.Timeout != nil && 0 > *sh.Timeout {
536 return newErrGreaterOrEqualZero("timeout")
537 }
538 *j = OTLPHttpExporter(sh.Plain)
539 return nil
540 }
541
542 // UnmarshalJSON implements json.Unmarshaler.
543 func (j *OTLPGrpcExporter) UnmarshalJSON(b []byte) error {
544 type Plain OTLPGrpcExporter
545 type shadow struct {
546 Plain
547 Endpoint json.RawMessage `json:"endpoint"`
548 }
549 var sh shadow
550 if err := json.Unmarshal(b, &sh); err != nil {
551 return errors.Join(newErrUnmarshal(j), err)
552 }
553 if sh.Endpoint == nil {
554 return newErrRequired(j, "endpoint")
555 }
556 if err := json.Unmarshal(sh.Endpoint, &sh.Plain.Endpoint); err != nil {
557 return err
558 }
559 if sh.Timeout != nil && 0 > *sh.Timeout {
560 return newErrGreaterOrEqualZero("timeout")
561 }
562 *j = OTLPGrpcExporter(sh.Plain)
563 return nil
564 }
565
566 // UnmarshalJSON implements json.Unmarshaler.
567 func (j *AttributeType) UnmarshalJSON(b []byte) error {
568 var v string
569 if err := json.Unmarshal(b, &v); err != nil {
570 return errors.Join(newErrUnmarshal(j), err)
571 }
572 var ok bool
573 for _, expected := range enumValuesAttributeType {
574 if reflect.DeepEqual(v, expected) {
575 ok = true
576 break
577 }
578 }
579 if !ok {
580 return newErrInvalid(fmt.Sprintf("unexpected value type %#v, expected one of %#v)", v, enumValuesAttributeType))
581 }
582 *j = AttributeType(v)
583 return nil
584 }
585
586 // UnmarshalJSON implements json.Unmarshaler.
587 func (j *AttributeNameValue) UnmarshalJSON(b []byte) error {
588 type Plain AttributeNameValue
589 type shadow struct {
590 Plain
591 Name json.RawMessage `json:"name"`
592 Value json.RawMessage `json:"value"`
593 }
594 var sh shadow
595 if err := json.Unmarshal(b, &sh); err != nil {
596 return errors.Join(newErrUnmarshal(j), err)
597 }
598 if sh.Name == nil {
599 return newErrRequired(j, "name")
600 }
601 if err := json.Unmarshal(sh.Name, &sh.Plain.Name); err != nil {
602 return err
603 }
604 if sh.Value == nil {
605 return newErrRequired(j, "value")
606 }
607 if err := json.Unmarshal(sh.Value, &sh.Plain.Value); err != nil {
608 return err
609 }
610
611 // json unmarshaller defaults to unmarshalling to float for int values
612 if sh.Type != nil && *sh.Type == AttributeTypeInt {
613 val, ok := sh.Plain.Value.(float64)
614 if ok {
615 sh.Plain.Value = int(val)
616 }
617 }
618
619 if sh.Type != nil && *sh.Type == AttributeTypeIntArray {
620 m, ok := sh.Plain.Value.([]any)
621 if ok {
622 var vals []any
623 for _, v := range m {
624 val, ok := v.(float64)
625 if ok {
626 vals = append(vals, int(val))
627 } else {
628 vals = append(vals, v)
629 }
630 }
631 sh.Plain.Value = vals
632 }
633 }
634
635 *j = AttributeNameValue(sh.Plain)
636 return nil
637 }
638
639 // UnmarshalJSON implements json.Unmarshaler.
640 func (j *SimpleLogRecordProcessor) UnmarshalJSON(b []byte) error {
641 type Plain SimpleLogRecordProcessor
642 type shadow struct {
643 Plain
644 Exporter json.RawMessage `json:"exporter"`
645 }
646 var sh shadow
647 if err := json.Unmarshal(b, &sh); err != nil {
648 return errors.Join(newErrUnmarshal(j), err)
649 }
650 if sh.Exporter == nil {
651 return newErrRequired(j, "exporter")
652 }
653 // Hydrate the exporter into the underlying field.
654 if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
655 return err
656 }
657 *j = SimpleLogRecordProcessor(sh.Plain)
658 return nil
659 }
660
661 // UnmarshalJSON implements json.Unmarshaler.
662 func (j *SimpleSpanProcessor) UnmarshalJSON(b []byte) error {
663 type Plain SimpleSpanProcessor
664 type shadow struct {
665 Plain
666 Exporter json.RawMessage `json:"exporter"`
667 }
668 var sh shadow
669 if err := json.Unmarshal(b, &sh); err != nil {
670 return errors.Join(newErrUnmarshal(j), err)
671 }
672 if sh.Exporter == nil {
673 return newErrRequired(j, "exporter")
674 }
675 // Hydrate the exporter into the underlying field.
676 if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
677 return err
678 }
679 *j = SimpleSpanProcessor(sh.Plain)
680 return nil
681 }
682
683 // UnmarshalJSON implements json.Unmarshaler.
684 func (j *NameStringValuePair) UnmarshalJSON(b []byte) error {
685 type Plain NameStringValuePair
686 type shadow struct {
687 Plain
688 Name json.RawMessage `json:"name"`
689 Value json.RawMessage `json:"value"`
690 }
691 var sh shadow
692 if err := json.Unmarshal(b, &sh); err != nil {
693 return errors.Join(newErrUnmarshal(j), err)
694 }
695 if sh.Name == nil {
696 return newErrRequired(j, "name")
697 }
698 if err := json.Unmarshal(sh.Name, &sh.Plain.Name); err != nil {
699 return errors.Join(newErrUnmarshal(j), err)
700 }
701 if sh.Value == nil {
702 return newErrRequired(j, "value")
703 }
704 if err := json.Unmarshal(sh.Value, &sh.Plain.Value); err != nil {
705 return errors.Join(newErrUnmarshal(j), err)
706 }
707
708 *j = NameStringValuePair(sh.Plain)
709 return nil
710 }
711
712 // UnmarshalJSON implements json.Unmarshaler.
713 func (j *InstrumentType) UnmarshalJSON(b []byte) error {
714 var v string
715 if err := json.Unmarshal(b, &v); err != nil {
716 return errors.Join(newErrUnmarshal(j), err)
717 }
718 if err := supportedInstrumentType(InstrumentType(v)); err != nil {
719 return err
720 }
721 *j = InstrumentType(v)
722 return nil
723 }
724
725 // UnmarshalJSON implements json.Unmarshaler.
726 func (j *ExporterDefaultHistogramAggregation) UnmarshalJSON(b []byte) error {
727 var v string
728 if err := json.Unmarshal(b, &v); err != nil {
729 return errors.Join(newErrUnmarshal(j), err)
730 }
731 if err := supportedHistogramAggregation(ExporterDefaultHistogramAggregation(v)); err != nil {
732 return err
733 }
734 *j = ExporterDefaultHistogramAggregation(v)
735 return nil
736 }
737
738 // UnmarshalJSON implements json.Unmarshaler.
739 func (j *PullMetricReader) UnmarshalJSON(b []byte) error {
740 type Plain PullMetricReader
741 type shadow struct {
742 Plain
743 Exporter json.RawMessage `json:"exporter"`
744 }
745 var sh shadow
746 if err := json.Unmarshal(b, &sh); err != nil {
747 return errors.Join(newErrUnmarshal(j), err)
748 }
749 if sh.Exporter == nil {
750 return newErrRequired(j, "exporter")
751 }
752 // Hydrate the exporter into the underlying field.
753 if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
754 return err
755 }
756 *j = PullMetricReader(sh.Plain)
757 return nil
758 }
759
760 // UnmarshalJSON implements json.Unmarshaler.
761 func (j *Sampler) UnmarshalJSON(b []byte) error {
762 var raw map[string]any
763 if err := json.Unmarshal(b, &raw); err != nil {
764 return err
765 }
766 type Plain Sampler
767 var plain Plain
768 if err := json.Unmarshal(b, &plain); err != nil {
769 return err
770 }
771 unmarshalSamplerTypes(raw, (*Sampler)(&plain))
772 *j = Sampler(plain)
773 return nil
774 }
775
776 // UnmarshalJSON implements json.Unmarshaler.
777 func (j *MetricProducer) UnmarshalJSON(b []byte) error {
778 var raw map[string]any
779 if err := json.Unmarshal(b, &raw); err != nil {
780 return err
781 }
782 type Plain MetricProducer
783 var plain Plain
784 if err := json.Unmarshal(b, &plain); err != nil {
785 return err
786 }
787 unmarshalMetricProducer(raw, (*MetricProducer)(&plain))
788 *j = MetricProducer(plain)
789 return nil
790 }