forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: add support for configuring propagators
Fixes open-telemetry#6712
- Loading branch information
1 parent
d21dd87
commit e083744
Showing
6 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
|
||
"go.opentelemetry.io/contrib/propagators/aws/xray" | ||
"go.opentelemetry.io/contrib/propagators/b3" | ||
"go.opentelemetry.io/contrib/propagators/jaeger" | ||
"go.opentelemetry.io/contrib/propagators/ot" | ||
"go.opentelemetry.io/otel/propagation" | ||
) | ||
|
||
func propagator(cfg configOptions) (propagation.TextMapPropagator, error) { | ||
if cfg.opentelemetryConfig.Propagator == nil { | ||
return nil, nil | ||
} | ||
|
||
var errs []error | ||
var ps []propagation.TextMapPropagator | ||
for _, name := range cfg.opentelemetryConfig.Propagator.Composite { | ||
if name == nil || *name == "" { | ||
continue | ||
} | ||
|
||
p, err := propagatorByName(*name) | ||
if err == nil { | ||
ps = append(ps, p) | ||
} else { | ||
|
||
errs = append(errs, err) | ||
} | ||
} | ||
|
||
if len(errs) > 0 { | ||
return nil, errors.Join(errs...) | ||
} | ||
|
||
if len(ps) == 0 { | ||
return nil, nil | ||
} | ||
|
||
res := propagation.NewCompositeTextMapPropagator(ps...) | ||
return res, nil | ||
} | ||
|
||
func propagatorByName(name string) (propagation.TextMapPropagator, error) { | ||
switch name { | ||
case "tracecontext": | ||
return propagation.TraceContext{}, nil | ||
case "baggage": | ||
return propagation.Baggage{}, nil | ||
case "b3": | ||
return b3.New(), nil | ||
case "b3multi": | ||
return b3.New(b3.WithInjectEncoding(b3.B3MultipleHeader)), nil | ||
case "jaeger": | ||
return jaeger.Jaeger{}, nil | ||
case "xray": | ||
return xray.Propagator{}, nil | ||
case "ottrace": | ||
return ot.OT{}, nil | ||
default: | ||
return nil, errors.New("unsupported propagator") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/contrib/propagators/aws/xray" | ||
"go.opentelemetry.io/contrib/propagators/b3" | ||
"go.opentelemetry.io/contrib/propagators/jaeger" | ||
"go.opentelemetry.io/contrib/propagators/ot" | ||
"go.opentelemetry.io/otel/propagation" | ||
) | ||
|
||
func TestPropagator(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg configOptions | ||
want propagation.TextMapPropagator | ||
wantErr bool | ||
errMsg string | ||
}{ | ||
{ | ||
name: "nil propagator config", | ||
cfg: configOptions{ | ||
opentelemetryConfig: OpenTelemetryConfiguration{ | ||
Propagator: nil, | ||
}, | ||
}, | ||
want: nil, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid tracecontext", | ||
cfg: configOptions{ | ||
opentelemetryConfig: OpenTelemetryConfiguration{ | ||
Propagator: &Propagator{ | ||
Composite: []*string{strPtr("tracecontext")}, | ||
}, | ||
}, | ||
}, | ||
want: propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid baggage", | ||
cfg: configOptions{ | ||
opentelemetryConfig: OpenTelemetryConfiguration{ | ||
Propagator: &Propagator{ | ||
Composite: []*string{strPtr("baggage")}, | ||
}, | ||
}, | ||
}, | ||
want: propagation.NewCompositeTextMapPropagator(propagation.Baggage{}), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid b3", | ||
cfg: configOptions{ | ||
opentelemetryConfig: OpenTelemetryConfiguration{ | ||
Propagator: &Propagator{ | ||
Composite: []*string{strPtr("b3")}, | ||
}, | ||
}, | ||
}, | ||
want: propagation.NewCompositeTextMapPropagator(b3.New()), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid b3multi", | ||
cfg: configOptions{ | ||
opentelemetryConfig: OpenTelemetryConfiguration{ | ||
Propagator: &Propagator{ | ||
Composite: []*string{strPtr("b3multi")}, | ||
}, | ||
}, | ||
}, | ||
want: propagation.NewCompositeTextMapPropagator(b3.New(b3.WithInjectEncoding(b3.B3MultipleHeader))), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid jaeger", | ||
cfg: configOptions{ | ||
opentelemetryConfig: OpenTelemetryConfiguration{ | ||
Propagator: &Propagator{ | ||
Composite: []*string{strPtr("jaeger")}, | ||
}, | ||
}, | ||
}, | ||
want: propagation.NewCompositeTextMapPropagator(jaeger.Jaeger{}), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid xray", | ||
cfg: configOptions{ | ||
opentelemetryConfig: OpenTelemetryConfiguration{ | ||
Propagator: &Propagator{ | ||
Composite: []*string{strPtr("xray")}, | ||
}, | ||
}, | ||
}, | ||
want: propagation.NewCompositeTextMapPropagator(xray.Propagator{}), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid ottrace", | ||
cfg: configOptions{ | ||
opentelemetryConfig: OpenTelemetryConfiguration{ | ||
Propagator: &Propagator{ | ||
Composite: []*string{strPtr("ottrace")}, | ||
}, | ||
}, | ||
}, | ||
want: propagation.NewCompositeTextMapPropagator(ot.OT{}), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "multiple propagators", | ||
cfg: configOptions{ | ||
opentelemetryConfig: OpenTelemetryConfiguration{ | ||
Propagator: &Propagator{ | ||
Composite: []*string{strPtr("tracecontext"), strPtr("baggage"), strPtr("b3")}, | ||
}, | ||
}, | ||
}, | ||
want: propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, b3.New()), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "empty composite", | ||
cfg: configOptions{ | ||
opentelemetryConfig: OpenTelemetryConfiguration{ | ||
Propagator: &Propagator{ | ||
Composite: []*string{}, | ||
}, | ||
}, | ||
}, | ||
want: nil, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "nil propagator name", | ||
cfg: configOptions{ | ||
opentelemetryConfig: OpenTelemetryConfiguration{ | ||
Propagator: &Propagator{ | ||
Composite: []*string{nil, strPtr("tracecontext")}, | ||
}, | ||
}, | ||
}, | ||
want: propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "unsupported propagator", | ||
cfg: configOptions{ | ||
opentelemetryConfig: OpenTelemetryConfiguration{ | ||
Propagator: &Propagator{ | ||
Composite: []*string{strPtr("unknown")}, | ||
}, | ||
}, | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
errMsg: "unsupported propagator", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := propagator(tt.cfg) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), tt.errMsg) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func strPtr(s string) *string { | ||
return &s | ||
} |