Skip to content

Commit 4bd95c6

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 f5ec487 commit 4bd95c6

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

otelconf/config_common.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,34 @@ func validatePeriodicMetricReader(plain *PeriodicMetricReader) error {
9191
return nil
9292
}
9393

94+
// unmarshalTextMapPropagatorTypes handles all propagator type unmarshaling.
95+
func unmarshalTextMapPropagatorTypes(raw map[string]any, plain *TextMapPropagator) {
96+
// the value for b3 is nillable, if so, set it here
97+
if v, ok := raw["b3"]; ok && v == nil {
98+
plain.B3 = B3Propagator{}
99+
}
100+
// the value for b3multi is nillable, if so, set it here
101+
if v, ok := raw["b3multi"]; ok && v == nil {
102+
plain.B3Multi = B3MultiPropagator{}
103+
}
104+
// the value for baggage is nillable, if so, set it here
105+
if v, ok := raw["baggage"]; ok && v == nil {
106+
plain.Baggage = BaggagePropagator{}
107+
}
108+
// the value for jaeger is nillable, if so, set it here
109+
if v, ok := raw["jaeger"]; ok && v == nil {
110+
plain.Jaeger = JaegerPropagator{}
111+
}
112+
// the value for ottrace is nillable, if so, set it here
113+
if v, ok := raw["ottrace"]; ok && v == nil {
114+
plain.Ottrace = OpenTracingPropagator{}
115+
}
116+
// the value for tracecontext is nillable, if so, set it here
117+
if v, ok := raw["tracecontext"]; ok && v == nil {
118+
plain.Tracecontext = TraceContextPropagator{}
119+
}
120+
}
121+
94122
// validateBatchLogRecordProcessor handles validation for BatchLogRecordProcessor.
95123
func validateBatchLogRecordProcessor(plain *BatchLogRecordProcessor) error {
96124
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
@@ -96,6 +96,22 @@ func (j *LogRecordExporter) UnmarshalJSON(b []byte) error {
9696
return nil
9797
}
9898

99+
// UnmarshalJSON implements json.Unmarshaler.
100+
func (j *TextMapPropagator) UnmarshalJSON(b []byte) error {
101+
var raw map[string]any
102+
if err := json.Unmarshal(b, &raw); err != nil {
103+
return errors.Join(errors.New("unmarshaling error TextMapPropagator"))
104+
}
105+
type Plain TextMapPropagator
106+
var plain Plain
107+
if err := json.Unmarshal(b, &plain); err != nil {
108+
return errors.Join(errors.New("unmarshaling error TextMapPropagator"))
109+
}
110+
unmarshalTextMapPropagatorTypes(raw, (*TextMapPropagator)(&plain))
111+
*j = TextMapPropagator(plain)
112+
return nil
113+
}
114+
99115
// UnmarshalJSON implements json.Unmarshaler.
100116
func (j *BatchLogRecordProcessor) UnmarshalJSON(b []byte) error {
101117
type Plain BatchLogRecordProcessor

otelconf/config_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
1011
"go.yaml.in/yaml/v3"
1112
)
1213

@@ -52,6 +53,51 @@ func TestUnmarshalSpanExporterInvalidData(t *testing.T) {
5253
assert.ErrorIs(t, err, newErrUnmarshal(&SpanExporter{}))
5354
}
5455

56+
func TestUnmarshalTextMapPropagator(t *testing.T) {
57+
for _, tt := range []struct {
58+
name string
59+
yamlConfig []byte
60+
jsonConfig []byte
61+
wantErr string
62+
}{
63+
{
64+
name: "valid with b3 propagator",
65+
jsonConfig: []byte(`{"b3":{}}`),
66+
yamlConfig: []byte("b3: {}\n"),
67+
},
68+
{
69+
name: "valid with all propagators nil",
70+
jsonConfig: []byte(`{"b3":null,"b3multi":null,"baggage":null,"jaeger":null,"ottrace":null,"tracecontext":null}`),
71+
yamlConfig: []byte("b3:\nb3multi:\nbaggage:\njaeger:\nottrace:\ntracecontext:\n"),
72+
},
73+
{
74+
name: "invalid data",
75+
jsonConfig: []byte(`{"b3":2000}`),
76+
yamlConfig: []byte("b3: !!str str"),
77+
wantErr: "unmarshaling error TextMapPropagator",
78+
},
79+
} {
80+
t.Run(tt.name, func(t *testing.T) {
81+
tmp := TextMapPropagator{}
82+
err := tmp.UnmarshalJSON(tt.jsonConfig)
83+
if tt.wantErr != "" {
84+
require.Error(t, err)
85+
require.Contains(t, err.Error(), tt.wantErr)
86+
} else {
87+
require.NoError(t, err)
88+
}
89+
tmp = TextMapPropagator{}
90+
err = yaml.Unmarshal(tt.yamlConfig, &tmp)
91+
if tt.wantErr != "" {
92+
require.Error(t, err)
93+
require.Contains(t, err.Error(), tt.wantErr)
94+
} else {
95+
require.NoError(t, err)
96+
}
97+
})
98+
}
99+
}
100+
55101
func TestUnmarshalBatchLogRecordProcessor(t *testing.T) {
56102
for _, tt := range []struct {
57103
name string

otelconf/config_yaml.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ func (j *LogRecordExporter) UnmarshalYAML(node *yaml.Node) error {
6969
return nil
7070
}
7171

72+
// UnmarshalYAML implements yaml.Unmarshaler.
73+
func (j *TextMapPropagator) UnmarshalYAML(node *yaml.Node) error {
74+
var raw map[string]any
75+
if err := node.Decode(&raw); err != nil {
76+
return errors.Join(errors.New("unmarshaling error TextMapPropagator"))
77+
}
78+
type Plain TextMapPropagator
79+
var plain Plain
80+
if err := node.Decode(&plain); err != nil {
81+
return errors.Join(errors.New("unmarshaling error TextMapPropagator"))
82+
}
83+
unmarshalTextMapPropagatorTypes(raw, (*TextMapPropagator)(&plain))
84+
*j = TextMapPropagator(plain)
85+
return nil
86+
}
87+
7288
// UnmarshalYAML implements yaml.Unmarshaler.
7389
func (j *BatchLogRecordProcessor) UnmarshalYAML(node *yaml.Node) error {
7490
if !hasYAMLMapKey(node, "exporter") {

0 commit comments

Comments
 (0)