Skip to content

Commit d262f8d

Browse files
committed
otelconf: add unmarshalers for otlp exporters
Adding unmarshalers and tests for otlp http/grpc exporters. Also added a missing test for createHeaders. Part of splitting open-telemetry#8026 Signed-off-by: alex boten <[email protected]>
1 parent e24b42b commit d262f8d

File tree

5 files changed

+484
-17
lines changed

5 files changed

+484
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1616
- Add unmarshaling and validation for `CardinalityLimits` and `SpanLimits` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8043)
1717
- Add unmarshaling and validation for `BatchLogRecordProcessor`, `BatchSpanProcessor`, and `PeriodicMetricReader` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8049)
1818
- Add unmarshaling and validation for `TextMapPropagator` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8052)
19+
- Add unmarshaling and validation for `OTLPHttpExporter`, `OTLPGrpcExporter`, `OTLPGrpcMetricExporter` and `OTLPHttpMetricExporter` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#)
1920

2021
### Changed
2122

otelconf/config_common.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,17 @@ func (e *errBound) Is(target error) bool {
5252
return e.Field == t.Field && e.Bound == t.Bound && e.Op == t.Op
5353
}
5454

55-
type errRequiredExporter struct {
55+
type errRequired struct {
5656
Object any
57+
Field string
5758
}
5859

59-
func (e *errRequiredExporter) Error() string {
60-
return fmt.Sprintf("field exporter in %s: required", reflect.TypeOf(e.Object))
60+
func (e *errRequired) Error() string {
61+
return fmt.Sprintf("field %s in %s: required", e.Field, reflect.TypeOf(e.Object))
6162
}
6263

63-
func (e *errRequiredExporter) Is(target error) bool {
64-
t, ok := target.(*errRequiredExporter)
64+
func (e *errRequired) Is(target error) bool {
65+
t, ok := target.(*errRequired)
6566
if !ok {
6667
return false
6768
}
@@ -96,9 +97,9 @@ func newErrGreaterThanZero(field string) error {
9697
return &errBound{Field: field, Bound: 0, Op: ">"}
9798
}
9899

99-
// newErrRequiredExporter creates a new error indicating that the exporter field is required.
100-
func newErrRequiredExporter(object any) error {
101-
return &errRequiredExporter{Object: object}
100+
// newErrRequired creates a new error indicating that the exporter field is required.
101+
func newErrRequired(object any, field string) error {
102+
return &errRequired{Object: object, Field: field}
102103
}
103104

104105
// newErrUnmarshal creates a new error indicating that an error occurred during unmarshaling.

otelconf/config_json.go

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func (j *BatchLogRecordProcessor) UnmarshalJSON(b []byte) error {
273273
return errors.Join(newErrUnmarshal(j), err)
274274
}
275275
if sh.Exporter == nil {
276-
return newErrRequiredExporter(j)
276+
return newErrRequired(j, "exporter")
277277
}
278278
// Hydrate the exporter into the underlying field.
279279
if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
@@ -299,7 +299,7 @@ func (j *BatchSpanProcessor) UnmarshalJSON(b []byte) error {
299299
return errors.Join(newErrUnmarshal(j), err)
300300
}
301301
if sh.Exporter == nil {
302-
return newErrRequiredExporter(j)
302+
return newErrRequired(j, "exporter")
303303
}
304304
// Hydrate the exporter into the underlying field.
305305
if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
@@ -325,7 +325,7 @@ func (j *PeriodicMetricReader) UnmarshalJSON(b []byte) error {
325325
return errors.Join(newErrUnmarshal(j), err)
326326
}
327327
if sh.Exporter == nil {
328-
return newErrRequiredExporter(j)
328+
return newErrRequired(j, "exporter")
329329
}
330330
// Hydrate the exporter into the underlying field.
331331
if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
@@ -366,3 +366,99 @@ func (j *SpanLimits) UnmarshalJSON(value []byte) error {
366366
*j = SpanLimits(plain)
367367
return nil
368368
}
369+
370+
// UnmarshalJSON implements json.Unmarshaler.
371+
func (j *OTLPHttpMetricExporter) UnmarshalJSON(b []byte) error {
372+
type Plain OTLPHttpMetricExporter
373+
type shadow struct {
374+
Plain
375+
Endpoint json.RawMessage `json:"endpoint"`
376+
}
377+
var sh shadow
378+
if err := json.Unmarshal(b, &sh); err != nil {
379+
return errors.Join(newErrUnmarshal(j), err)
380+
}
381+
if sh.Endpoint == nil {
382+
return newErrRequired(j, "endpoint")
383+
}
384+
if err := json.Unmarshal(sh.Endpoint, &sh.Plain.Endpoint); err != nil {
385+
return err
386+
}
387+
if sh.Plain.Timeout != nil && 0 > *sh.Plain.Timeout {
388+
return newErrGreaterOrEqualZero("timeout")
389+
}
390+
*j = OTLPHttpMetricExporter(sh.Plain)
391+
return nil
392+
}
393+
394+
// UnmarshalJSON implements json.Unmarshaler.
395+
func (j *OTLPGrpcMetricExporter) UnmarshalJSON(b []byte) error {
396+
type Plain OTLPGrpcMetricExporter
397+
type shadow struct {
398+
Plain
399+
Endpoint json.RawMessage `json:"endpoint"`
400+
}
401+
var sh shadow
402+
if err := json.Unmarshal(b, &sh); err != nil {
403+
return errors.Join(newErrUnmarshal(j), err)
404+
}
405+
if sh.Endpoint == nil {
406+
return newErrRequired(j, "endpoint")
407+
}
408+
if err := json.Unmarshal(sh.Endpoint, &sh.Plain.Endpoint); err != nil {
409+
return err
410+
}
411+
if sh.Plain.Timeout != nil && 0 > *sh.Plain.Timeout {
412+
return newErrGreaterOrEqualZero("timeout")
413+
}
414+
*j = OTLPGrpcMetricExporter(sh.Plain)
415+
return nil
416+
}
417+
418+
// UnmarshalJSON implements json.Unmarshaler.
419+
func (j *OTLPHttpExporter) UnmarshalJSON(b []byte) error {
420+
type Plain OTLPHttpExporter
421+
type shadow struct {
422+
Plain
423+
Endpoint json.RawMessage `json:"endpoint"`
424+
}
425+
var sh shadow
426+
if err := json.Unmarshal(b, &sh); err != nil {
427+
return errors.Join(newErrUnmarshal(j), err)
428+
}
429+
if sh.Endpoint == nil {
430+
return newErrRequired(j, "endpoint")
431+
}
432+
if err := json.Unmarshal(sh.Endpoint, &sh.Plain.Endpoint); err != nil {
433+
return err
434+
}
435+
if sh.Plain.Timeout != nil && 0 > *sh.Plain.Timeout {
436+
return newErrGreaterOrEqualZero("timeout")
437+
}
438+
*j = OTLPHttpExporter(sh.Plain)
439+
return nil
440+
}
441+
442+
// UnmarshalJSON implements json.Unmarshaler.
443+
func (j *OTLPGrpcExporter) UnmarshalJSON(b []byte) error {
444+
type Plain OTLPGrpcExporter
445+
type shadow struct {
446+
Plain
447+
Endpoint json.RawMessage `json:"endpoint"`
448+
}
449+
var sh shadow
450+
if err := json.Unmarshal(b, &sh); err != nil {
451+
return errors.Join(newErrUnmarshal(j), err)
452+
}
453+
if sh.Endpoint == nil {
454+
return newErrRequired(j, "endpoint")
455+
}
456+
if err := json.Unmarshal(sh.Endpoint, &sh.Plain.Endpoint); err != nil {
457+
return err
458+
}
459+
if sh.Plain.Timeout != nil && 0 > *sh.Plain.Timeout {
460+
return newErrGreaterOrEqualZero("timeout")
461+
}
462+
*j = OTLPGrpcExporter(sh.Plain)
463+
return nil
464+
}

0 commit comments

Comments
 (0)