-
Notifications
You must be signed in to change notification settings - Fork 786
otelconf: add support for propagators configuration #7977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0555f11
otelconf: add support for propagators configuration
codeboten da6d994
update changelog
codeboten 94ba961
fix changelog
codeboten c012efc
ignore empty names
codeboten 5c1d108
linting
codeboten cd5dc01
Merge branch 'main' into codeboten/propagators
codeboten 48f2207
Merge branch 'main' into codeboten/propagators
codeboten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package otelconf // import "go.opentelemetry.io/contrib/otelconf/v0.3.0" | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "go.opentelemetry.io/otel/propagation" | ||
|
|
||
| "go.opentelemetry.io/contrib/propagators/autoprop" | ||
| ) | ||
|
|
||
| var errInvalidPropagatorNil = errors.New("invalid propagator name: nil") | ||
|
|
||
| func propagator(cfg configOptions) (propagation.TextMapPropagator, error) { | ||
| if cfg.opentelemetryConfig.Propagator == nil { | ||
| return autoprop.NewTextMapPropagator(), nil | ||
| } | ||
|
|
||
| n := len(cfg.opentelemetryConfig.Propagator.Composite) | ||
| if n == 0 { | ||
| return autoprop.NewTextMapPropagator(), nil | ||
| } | ||
|
|
||
| var names []string | ||
| for _, name := range cfg.opentelemetryConfig.Propagator.Composite { | ||
| if name == nil { | ||
| return nil, errInvalidPropagatorNil | ||
| } | ||
| if *name == "" { | ||
| continue | ||
| } | ||
|
|
||
| names = append(names, *name) | ||
| } | ||
| if len(names) == 0 { | ||
| return autoprop.NewTextMapPropagator(), nil | ||
| } | ||
|
|
||
| return autoprop.TextMapPropagator(names...) | ||
| } |
This file contains hidden or 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,195 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package otelconf | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "go.opentelemetry.io/otel/propagation" | ||
|
|
||
| "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" | ||
| ) | ||
|
|
||
| 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: propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}), | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "valid tracecontext", | ||
| cfg: configOptions{ | ||
| opentelemetryConfig: OpenTelemetryConfiguration{ | ||
| Propagator: &Propagator{ | ||
| Composite: []*string{ptr("tracecontext")}, | ||
| }, | ||
| }, | ||
| }, | ||
| want: propagation.TraceContext{}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "valid baggage", | ||
| cfg: configOptions{ | ||
| opentelemetryConfig: OpenTelemetryConfiguration{ | ||
| Propagator: &Propagator{ | ||
| Composite: []*string{ptr("baggage")}, | ||
| }, | ||
| }, | ||
| }, | ||
| want: propagation.Baggage{}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "valid b3", | ||
| cfg: configOptions{ | ||
| opentelemetryConfig: OpenTelemetryConfiguration{ | ||
| Propagator: &Propagator{ | ||
| Composite: []*string{ptr("b3")}, | ||
| }, | ||
| }, | ||
| }, | ||
| want: b3.New(), | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "valid b3multi", | ||
| cfg: configOptions{ | ||
| opentelemetryConfig: OpenTelemetryConfiguration{ | ||
| Propagator: &Propagator{ | ||
| Composite: []*string{ptr("b3multi")}, | ||
| }, | ||
| }, | ||
| }, | ||
| want: b3.New(b3.WithInjectEncoding(b3.B3MultipleHeader)), | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "valid jaeger", | ||
| cfg: configOptions{ | ||
| opentelemetryConfig: OpenTelemetryConfiguration{ | ||
| Propagator: &Propagator{ | ||
| Composite: []*string{ptr("jaeger")}, | ||
| }, | ||
| }, | ||
| }, | ||
| want: jaeger.Jaeger{}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "valid xray", | ||
| cfg: configOptions{ | ||
| opentelemetryConfig: OpenTelemetryConfiguration{ | ||
| Propagator: &Propagator{ | ||
| Composite: []*string{ptr("xray")}, | ||
| }, | ||
| }, | ||
| }, | ||
| want: xray.Propagator{}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "valid ottrace", | ||
| cfg: configOptions{ | ||
| opentelemetryConfig: OpenTelemetryConfiguration{ | ||
| Propagator: &Propagator{ | ||
| Composite: []*string{ptr("ottrace")}, | ||
| }, | ||
| }, | ||
| }, | ||
| want: ot.OT{}, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "multiple propagators", | ||
| cfg: configOptions{ | ||
| opentelemetryConfig: OpenTelemetryConfiguration{ | ||
| Propagator: &Propagator{ | ||
| Composite: []*string{ptr("tracecontext"), ptr("baggage"), ptr("b3")}, | ||
| }, | ||
| }, | ||
| }, | ||
| want: propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, b3.New()), | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "empty composite", | ||
| cfg: configOptions{ | ||
| opentelemetryConfig: OpenTelemetryConfiguration{ | ||
| Propagator: &Propagator{ | ||
| Composite: []*string{}, | ||
| }, | ||
| }, | ||
| }, | ||
| want: propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}), | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "empty propagator name", | ||
| cfg: configOptions{ | ||
| opentelemetryConfig: OpenTelemetryConfiguration{ | ||
| Propagator: &Propagator{ | ||
| Composite: []*string{ptr(""), ptr("tracecontext")}, | ||
| }, | ||
| }, | ||
| }, | ||
| want: propagation.TraceContext{}, | ||
| wantErr: false, | ||
| }, | ||
|
Comment on lines
+144
to
+155
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it mean that somebody explicitly set an empty string? Is not an error scenario? Shouldn't we failed the parsing if anything is wrong in the YAML file? |
||
| { | ||
| name: "nil propagator name", | ||
| cfg: configOptions{ | ||
| opentelemetryConfig: OpenTelemetryConfiguration{ | ||
| Propagator: &Propagator{ | ||
| Composite: []*string{nil, ptr("tracecontext")}, | ||
| }, | ||
| }, | ||
| }, | ||
| want: nil, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "unsupported propagator", | ||
| cfg: configOptions{ | ||
| opentelemetryConfig: OpenTelemetryConfiguration{ | ||
| Propagator: &Propagator{ | ||
| Composite: []*string{ptr("unknown")}, | ||
| }, | ||
| }, | ||
| }, | ||
| want: propagation.NewCompositeTextMapPropagator(), | ||
| wantErr: true, | ||
| errMsg: "unknown 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) | ||
| }) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this following the specification? Should it not be a noop propagator?