Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add unmarshaling and validation for `CardinalityLimits` and `SpanLimits` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8043)
- Add unmarshaling and validation for `BatchLogRecordProcessor`, `BatchSpanProcessor`, and `PeriodicMetricReader` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8049)
- Add unmarshaling and validation for `TextMapPropagator` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8052)
- Add unmarshaling and validation for `OTLPHttpExporter`, `OTLPGrpcExporter`, `OTLPGrpcMetricExporter` and `OTLPHttpMetricExporter` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8112)

### Changed

Expand Down
17 changes: 9 additions & 8 deletions otelconf/config_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,17 @@ func (e *errBound) Is(target error) bool {
return e.Field == t.Field && e.Bound == t.Bound && e.Op == t.Op
}

type errRequiredExporter struct {
type errRequired struct {
Object any
Field string
}

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

func (e *errRequiredExporter) Is(target error) bool {
t, ok := target.(*errRequiredExporter)
func (e *errRequired) Is(target error) bool {
t, ok := target.(*errRequired)
if !ok {
return false
}
Expand Down Expand Up @@ -96,9 +97,9 @@ func newErrGreaterThanZero(field string) error {
return &errBound{Field: field, Bound: 0, Op: ">"}
}

// newErrRequiredExporter creates a new error indicating that the exporter field is required.
func newErrRequiredExporter(object any) error {
return &errRequiredExporter{Object: object}
// newErrRequired creates a new error indicating that the exporter field is required.
func newErrRequired(object any, field string) error {
return &errRequired{Object: object, Field: field}
}

// newErrUnmarshal creates a new error indicating that an error occurred during unmarshaling.
Expand Down
102 changes: 99 additions & 3 deletions otelconf/config_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (j *BatchLogRecordProcessor) UnmarshalJSON(b []byte) error {
return errors.Join(newErrUnmarshal(j), err)
}
if sh.Exporter == nil {
return newErrRequiredExporter(j)
return newErrRequired(j, "exporter")
}
// Hydrate the exporter into the underlying field.
if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
Expand All @@ -299,7 +299,7 @@ func (j *BatchSpanProcessor) UnmarshalJSON(b []byte) error {
return errors.Join(newErrUnmarshal(j), err)
}
if sh.Exporter == nil {
return newErrRequiredExporter(j)
return newErrRequired(j, "exporter")
}
// Hydrate the exporter into the underlying field.
if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
Expand All @@ -325,7 +325,7 @@ func (j *PeriodicMetricReader) UnmarshalJSON(b []byte) error {
return errors.Join(newErrUnmarshal(j), err)
}
if sh.Exporter == nil {
return newErrRequiredExporter(j)
return newErrRequired(j, "exporter")
}
// Hydrate the exporter into the underlying field.
if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
Expand Down Expand Up @@ -366,3 +366,99 @@ func (j *SpanLimits) UnmarshalJSON(value []byte) error {
*j = SpanLimits(plain)
return nil
}

// UnmarshalJSON implements json.Unmarshaler.
func (j *OTLPHttpMetricExporter) UnmarshalJSON(b []byte) error {
type Plain OTLPHttpMetricExporter
type shadow struct {
Plain
Endpoint json.RawMessage `json:"endpoint"`
}
var sh shadow
if err := json.Unmarshal(b, &sh); err != nil {
return errors.Join(newErrUnmarshal(j), err)
}
if sh.Endpoint == nil {
return newErrRequired(j, "endpoint")
}
if err := json.Unmarshal(sh.Endpoint, &sh.Plain.Endpoint); err != nil {
return err
}
if sh.Timeout != nil && 0 > *sh.Timeout {
return newErrGreaterOrEqualZero("timeout")
Comment thread
dashpole marked this conversation as resolved.
}
*j = OTLPHttpMetricExporter(sh.Plain)
return nil
}

// UnmarshalJSON implements json.Unmarshaler.
func (j *OTLPGrpcMetricExporter) UnmarshalJSON(b []byte) error {
type Plain OTLPGrpcMetricExporter
type shadow struct {
Plain
Endpoint json.RawMessage `json:"endpoint"`
}
var sh shadow
if err := json.Unmarshal(b, &sh); err != nil {
return errors.Join(newErrUnmarshal(j), err)
}
if sh.Endpoint == nil {
return newErrRequired(j, "endpoint")
}
if err := json.Unmarshal(sh.Endpoint, &sh.Plain.Endpoint); err != nil {
return err
}
if sh.Timeout != nil && 0 > *sh.Timeout {
return newErrGreaterOrEqualZero("timeout")
}
*j = OTLPGrpcMetricExporter(sh.Plain)
return nil
}

// UnmarshalJSON implements json.Unmarshaler.
func (j *OTLPHttpExporter) UnmarshalJSON(b []byte) error {
type Plain OTLPHttpExporter
type shadow struct {
Plain
Endpoint json.RawMessage `json:"endpoint"`
}
var sh shadow
if err := json.Unmarshal(b, &sh); err != nil {
return errors.Join(newErrUnmarshal(j), err)
}
if sh.Endpoint == nil {
return newErrRequired(j, "endpoint")
}
if err := json.Unmarshal(sh.Endpoint, &sh.Plain.Endpoint); err != nil {
return err
}
if sh.Timeout != nil && 0 > *sh.Timeout {
return newErrGreaterOrEqualZero("timeout")
}
*j = OTLPHttpExporter(sh.Plain)
return nil
}

// UnmarshalJSON implements json.Unmarshaler.
func (j *OTLPGrpcExporter) UnmarshalJSON(b []byte) error {
type Plain OTLPGrpcExporter
type shadow struct {
Plain
Endpoint json.RawMessage `json:"endpoint"`
}
var sh shadow
if err := json.Unmarshal(b, &sh); err != nil {
return errors.Join(newErrUnmarshal(j), err)
}
if sh.Endpoint == nil {
return newErrRequired(j, "endpoint")
}
if err := json.Unmarshal(sh.Endpoint, &sh.Plain.Endpoint); err != nil {
return err
}
if sh.Timeout != nil && 0 > *sh.Timeout {
return newErrGreaterOrEqualZero("timeout")
}
*j = OTLPGrpcExporter(sh.Plain)
return nil
}
Loading
Loading