-
Notifications
You must be signed in to change notification settings - Fork 319
Support using the OpenTelemetry API to interact with automatic W3C baggage #9982
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
97 changes: 97 additions & 0 deletions
97
...nt/agent-otel/otel-shim/src/main/java/datadog/opentelemetry/shim/baggage/OtelBaggage.java
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,97 @@ | ||
| package datadog.opentelemetry.shim.baggage; | ||
|
|
||
| import static java.util.stream.Collectors.toMap; | ||
|
|
||
| import io.opentelemetry.api.baggage.Baggage; | ||
| import io.opentelemetry.api.baggage.BaggageBuilder; | ||
| import io.opentelemetry.api.baggage.BaggageEntry; | ||
| import io.opentelemetry.api.baggage.BaggageEntryMetadata; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.function.BiConsumer; | ||
| import javax.annotation.Nullable; | ||
| import javax.annotation.ParametersAreNonnullByDefault; | ||
|
|
||
| @ParametersAreNonnullByDefault | ||
| public class OtelBaggage implements Baggage { | ||
| private final datadog.trace.bootstrap.instrumentation.api.Baggage delegate; | ||
|
|
||
| public OtelBaggage(datadog.trace.bootstrap.instrumentation.api.Baggage delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| public OtelBaggage(Map<String, String> items) { | ||
| this(datadog.trace.bootstrap.instrumentation.api.Baggage.create(items)); | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return delegate.asMap().size(); | ||
| } | ||
|
|
||
| @Override | ||
| public void forEach(BiConsumer<? super String, ? super BaggageEntry> consumer) { | ||
| for (Map.Entry<String, String> entry : delegate.asMap().entrySet()) { | ||
| consumer.accept(entry.getKey(), new ValueOnly(entry)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, BaggageEntry> asMap() { | ||
| return delegate.asMap().entrySet().stream().collect(toMap(Map.Entry::getKey, ValueOnly::new)); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public String getEntryValue(String key) { | ||
| return delegate.asMap().get(key); | ||
| } | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public BaggageEntry getEntry(String key) { | ||
| String value = getEntryValue(key); | ||
| return value != null ? new ValueOnly(value) : null; | ||
| } | ||
|
|
||
| @Override | ||
| public BaggageBuilder toBuilder() { | ||
| return new OtelBaggageBuilder(delegate.asMap()); | ||
| } | ||
|
|
||
| public datadog.trace.bootstrap.instrumentation.api.Baggage asAgentBaggage() { | ||
| return delegate; | ||
| } | ||
|
|
||
| static class ValueOnly implements BaggageEntry { | ||
| private final String value; | ||
|
|
||
| ValueOnly(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| ValueOnly(Map.Entry<String, String> entry) { | ||
| this(entry.getValue()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public BaggageEntryMetadata getMetadata() { | ||
| return BaggageEntryMetadata.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(value); | ||
| } | ||
|
|
||
| @Override | ||
| public final boolean equals(Object o) { | ||
| return (o instanceof ValueOnly) && Objects.equals(value, ((ValueOnly) o).value); | ||
| } | ||
| } | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
...t-otel/otel-shim/src/main/java/datadog/opentelemetry/shim/baggage/OtelBaggageBuilder.java
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,40 @@ | ||
| package datadog.opentelemetry.shim.baggage; | ||
|
|
||
| import io.opentelemetry.api.baggage.Baggage; | ||
| import io.opentelemetry.api.baggage.BaggageBuilder; | ||
| import io.opentelemetry.api.baggage.BaggageEntryMetadata; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import javax.annotation.Nullable; | ||
| import javax.annotation.ParametersAreNonnullByDefault; | ||
|
|
||
| @ParametersAreNonnullByDefault | ||
| public class OtelBaggageBuilder implements BaggageBuilder { | ||
| private final Map<String, String> items; | ||
|
|
||
| public OtelBaggageBuilder(Map<String, String> items) { | ||
| this.items = new HashMap<>(items); | ||
| } | ||
|
|
||
| @Override | ||
| public BaggageBuilder put( | ||
| @Nullable String key, @Nullable String value, BaggageEntryMetadata ignore) { | ||
| if (key != null && value != null) { | ||
| items.put(key, value); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public BaggageBuilder remove(@Nullable String key) { | ||
| if (key != null) { | ||
| items.remove(key); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Baggage build() { | ||
| return new OtelBaggage(items); | ||
| } | ||
| } |
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
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
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.
❓ This isn't a function that OTel has in their spec right?
Uh oh!
There was an error while loading. Please reload this page.
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.
correct - it is used to access the underlying Datadog baggage when the wrapper is "made current" using the OTel context API (i.e. for round-tripping purposes)
PS. the name is deliberate to match the
asAgentSpanmethod in theOtelSpanwrapper.