-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add delegating global propagator #1258
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2d14db7
Add delegating global propagator
MrAlias 010147a
Add Changes to CHANGELOG
MrAlias 60bc93b
Add PR number to CHANGELOG
MrAlias 3168c05
Merge remote-tracking branch 'upstream/master' into global-propagators
MrAlias af7ae17
Add tests using new test framework
MrAlias 4d728a0
Revert "Add tests using new test framework"
MrAlias 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 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,86 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package internal | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"go.opentelemetry.io/otel" | ||
) | ||
|
||
// textMapPropagator is a default TextMapPropagator that delegates calls to a | ||
// registered delegate if one is set, otherwise it defaults to delegating the | ||
// calls to a the default no-op otel.TextMapPropagator. | ||
type textMapPropagator struct { | ||
mtx sync.Mutex | ||
once sync.Once | ||
delegate otel.TextMapPropagator | ||
noop otel.TextMapPropagator | ||
} | ||
|
||
// Compile-time guarantee that textMapPropagator implements the | ||
// otel.TextMapPropagator interface. | ||
var _ otel.TextMapPropagator = (*textMapPropagator)(nil) | ||
|
||
func newTextMapPropagator() *textMapPropagator { | ||
return &textMapPropagator{ | ||
noop: otel.NewCompositeTextMapPropagator(), | ||
} | ||
} | ||
|
||
// SetDelegate sets a delegate otel.TextMapPropagator that all calls are | ||
// forwarded to. Delegation can only be performed once, all subsequent calls | ||
// perform no delegation. | ||
func (p *textMapPropagator) SetDelegate(delegate otel.TextMapPropagator) { | ||
if delegate == nil { | ||
return | ||
} | ||
|
||
p.mtx.Lock() | ||
p.once.Do(func() { p.delegate = delegate }) | ||
p.mtx.Unlock() | ||
} | ||
|
||
// HasDelegate returns if a delegate is set for p. | ||
func (p *textMapPropagator) HasDelegate() bool { | ||
p.mtx.Lock() | ||
defer p.mtx.Unlock() | ||
return p.delegate != nil | ||
} | ||
|
||
// Inject set cross-cutting concerns from the Context into the carrier. | ||
func (p *textMapPropagator) Inject(ctx context.Context, carrier otel.TextMapCarrier) { | ||
if p.HasDelegate() { | ||
p.delegate.Inject(ctx, carrier) | ||
} | ||
p.noop.Inject(ctx, carrier) | ||
} | ||
|
||
// Extract reads cross-cutting concerns from the carrier into a Context. | ||
func (p *textMapPropagator) Extract(ctx context.Context, carrier otel.TextMapCarrier) context.Context { | ||
if p.HasDelegate() { | ||
return p.delegate.Extract(ctx, carrier) | ||
} | ||
return p.noop.Extract(ctx, carrier) | ||
} | ||
|
||
// Fields returns the keys who's values are set with Inject. | ||
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. s/who's/whose/ |
||
func (p *textMapPropagator) Fields() []string { | ||
if p.HasDelegate() { | ||
return p.delegate.Fields() | ||
} | ||
return p.noop.Fields() | ||
} |
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
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.
Both here and in
(*textMapPropagator).Fields
, it would be safer to use the value read while holding the mutex. Sketching:You could introduce a helper method to make that more concise:
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.
This seems good. I was basing this off the fact that the delegate only ever changes from
nil
to notnil
, and it only does this once, but I think you're approach doesn't add overhead and guards against future changes invalidating these assumptions. I'll open a PR to address your feedback. Thanks.