Skip to content

Commit cb3e452

Browse files
committed
otelconf: unmarshal TextMapPropagator
Adds unmarshaling code for TextMapPropagator which includes supporting nillable values for the different propagators. Part of splitting open-telemetry#8026 Signed-off-by: alex boten <[email protected]>
1 parent 1d217c5 commit cb3e452

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

otelconf/config_common.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ package otelconf // import "go.opentelemetry.io/contrib/otelconf"
55

66
import "fmt"
77

8+
// unmarshalTextMapPropagatorTypes handles all propagator type unmarshaling.
9+
func unmarshalTextMapPropagatorTypes(raw map[string]any, plain *TextMapPropagator) {
10+
// the value for b3 is nillable, if so, set it here
11+
if v, ok := raw["b3"]; ok && v == nil {
12+
plain.B3 = B3Propagator{}
13+
}
14+
// the value for b3multi is nillable, if so, set it here
15+
if v, ok := raw["b3multi"]; ok && v == nil {
16+
plain.B3Multi = B3MultiPropagator{}
17+
}
18+
// the value for baggage is nillable, if so, set it here
19+
if v, ok := raw["baggage"]; ok && v == nil {
20+
plain.Baggage = BaggagePropagator{}
21+
}
22+
// the value for jaeger is nillable, if so, set it here
23+
if v, ok := raw["jaeger"]; ok && v == nil {
24+
plain.Jaeger = JaegerPropagator{}
25+
}
26+
// the value for ottrace is nillable, if so, set it here
27+
if v, ok := raw["ottrace"]; ok && v == nil {
28+
plain.Ottrace = OpenTracingPropagator{}
29+
}
30+
// the value for tracecontext is nillable, if so, set it here
31+
if v, ok := raw["tracecontext"]; ok && v == nil {
32+
plain.Tracecontext = TraceContextPropagator{}
33+
}
34+
}
35+
836
// validateBatchLogRecordProcessor handles validation for BatchLogRecordProcessor.
937
func validateBatchLogRecordProcessor(plain *BatchLogRecordProcessor) error {
1038
if plain.ExportTimeout != nil && 0 > *plain.ExportTimeout {

otelconf/config_json.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ import (
88
"errors"
99
)
1010

11+
// UnmarshalJSON implements json.Unmarshaler.
12+
func (j *TextMapPropagator) UnmarshalJSON(b []byte) error {
13+
var raw map[string]any
14+
if err := json.Unmarshal(b, &raw); err != nil {
15+
return errors.Join(errors.New("unmarshaling error TextMapPropagator"))
16+
}
17+
type Plain TextMapPropagator
18+
var plain Plain
19+
if err := json.Unmarshal(b, &plain); err != nil {
20+
return errors.Join(errors.New("unmarshaling error TextMapPropagator"))
21+
}
22+
unmarshalTextMapPropagatorTypes(raw, (*TextMapPropagator)(&plain))
23+
*j = TextMapPropagator(plain)
24+
return nil
25+
}
26+
1127
// UnmarshalJSON implements json.Unmarshaler.
1228
func (j *BatchLogRecordProcessor) UnmarshalJSON(b []byte) error {
1329
var raw map[string]any

otelconf/config_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,51 @@ import (
1010
"go.yaml.in/yaml/v3"
1111
)
1212

13+
func TestUnmarshalTextMapPropagator(t *testing.T) {
14+
for _, tt := range []struct {
15+
name string
16+
yamlConfig []byte
17+
jsonConfig []byte
18+
wantErr string
19+
}{
20+
{
21+
name: "valid with b3 propagator",
22+
jsonConfig: []byte(`{"b3":{}}`),
23+
yamlConfig: []byte("b3: {}\n"),
24+
},
25+
{
26+
name: "valid with all propagators nil",
27+
jsonConfig: []byte(`{"b3":null,"b3multi":null,"baggage":null,"jaeger":null,"ottrace":null,"tracecontext":null}`),
28+
yamlConfig: []byte("b3:\nb3multi:\nbaggage:\njaeger:\nottrace:\ntracecontext:\n"),
29+
},
30+
{
31+
name: "invalid data",
32+
jsonConfig: []byte(`{"b3":2000}`),
33+
yamlConfig: []byte("b3: !!str str"),
34+
wantErr: "unmarshaling error TextMapPropagator",
35+
},
36+
} {
37+
t.Run(tt.name, func(t *testing.T) {
38+
tmp := TextMapPropagator{}
39+
err := tmp.UnmarshalJSON(tt.jsonConfig)
40+
if tt.wantErr != "" {
41+
require.Error(t, err)
42+
require.Contains(t, err.Error(), tt.wantErr)
43+
} else {
44+
require.NoError(t, err)
45+
}
46+
tmp = TextMapPropagator{}
47+
err = yaml.Unmarshal(tt.yamlConfig, &tmp)
48+
if tt.wantErr != "" {
49+
require.Error(t, err)
50+
require.Contains(t, err.Error(), tt.wantErr)
51+
} else {
52+
require.NoError(t, err)
53+
}
54+
})
55+
}
56+
}
57+
1358
func TestUnmarshalBatchLogRecordProcessor(t *testing.T) {
1459
for _, tt := range []struct {
1560
name string

otelconf/config_yaml.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ import (
99
"go.yaml.in/yaml/v3"
1010
)
1111

12+
// UnmarshalYAML implements yaml.Unmarshaler.
13+
func (j *TextMapPropagator) UnmarshalYAML(node *yaml.Node) error {
14+
var raw map[string]any
15+
if err := node.Decode(&raw); err != nil {
16+
return errors.Join(errors.New("unmarshaling error TextMapPropagator"))
17+
}
18+
type Plain TextMapPropagator
19+
var plain Plain
20+
if err := node.Decode(&plain); err != nil {
21+
return errors.Join(errors.New("unmarshaling error TextMapPropagator"))
22+
}
23+
unmarshalTextMapPropagatorTypes(raw, (*TextMapPropagator)(&plain))
24+
*j = TextMapPropagator(plain)
25+
return nil
26+
}
27+
1228
// UnmarshalYAML implements yaml.Unmarshaler.
1329
func (j *BatchLogRecordProcessor) UnmarshalYAML(node *yaml.Node) error {
1430
var raw map[string]any

0 commit comments

Comments
 (0)