Repository navigation
Write global tags once on the OTLP trace resource #13065
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
gh-worker-dd-mergequeue-cf854d
merged 3 commits into
master
from
matthew.li/otlp-resource-global-tags
Oct 28, 2026
Merged
Changes from all commits
Commits
Show all changes
3 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
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
67 changes: 67 additions & 0 deletions
67
dd-trace-core/src/main/java/datadog/trace/core/otlp/trace/OtlpSpanTags.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,67 @@ | ||
| package datadog.trace.core.otlp.trace; | ||
|
|
||
| import datadog.trace.api.TagMap; | ||
| import datadog.trace.api.function.TriConsumer; | ||
| import datadog.trace.core.Metadata; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Visits the tags an OTLP span carries as attributes: the span's direct tags, plus the span | ||
| * defaults ({@code DD_TRACE_SPAN_TAGS}, {@code env}, ...) it doesn't set itself. The global tags | ||
| * are left to the resource, which carries them once per payload. | ||
| * | ||
| * <p>A span default is only written while the span still carries it: one the span removed, or that | ||
| * interception consumed into a span field (such as {@code resource.name}), is skipped. When the | ||
| * tracer tags need interception they're copied into each span, so global tags then also appear on | ||
| * the span, as before. | ||
| * | ||
| * <p>Not thread-safe: each writer owns one. | ||
| */ | ||
| final class OtlpSpanTags<T> { | ||
| private final T target; | ||
| private final TriConsumer<T, Integer, TagMap.EntryReader> tagWriter; | ||
|
|
||
| // per-span state | ||
| private Integer direction; | ||
| private TagMap spanTags; | ||
| private TagMap spanDefaultTags; | ||
| private final List<String> overriddenDefaults = new ArrayList<>(4); | ||
|
|
||
| OtlpSpanTags(T target, TriConsumer<T, Integer, TagMap.EntryReader> tagWriter) { | ||
| this.target = target; | ||
| this.tagWriter = tagWriter; | ||
| } | ||
|
|
||
| /** Writes the span's attribute tags, naming them for the span's {@code direction}. */ | ||
| void write(Metadata metadata, Integer direction) { | ||
| this.direction = direction; | ||
| this.spanTags = metadata.getTags(); | ||
| this.spanDefaultTags = metadata.getSpanDefaultTags(); | ||
| try { | ||
| spanTags.forEachLocal(this, OtlpSpanTags::writeDirectTag); | ||
| if (!spanDefaultTags.isEmpty()) { | ||
| spanDefaultTags.forEach(this, OtlpSpanTags::writeDefaultTag); | ||
| } | ||
| } finally { | ||
| this.spanTags = null; | ||
| this.spanDefaultTags = null; | ||
| overriddenDefaults.clear(); | ||
| } | ||
| } | ||
|
|
||
| private void writeDirectTag(TagMap.EntryReader entry) { | ||
| if (!spanDefaultTags.isEmpty() && spanDefaultTags.containsKey(entry.tag())) { | ||
| overriddenDefaults.add(entry.tag()); | ||
| } | ||
| tagWriter.accept(target, direction, entry); | ||
| } | ||
|
|
||
| private void writeDefaultTag(TagMap.EntryReader entry) { | ||
| // skip defaults the span set itself, or no longer carries (removed, or consumed by | ||
| // interception) | ||
| if (!overriddenDefaults.contains(entry.tag()) && spanTags.getEntry(entry.tag()) != null) { | ||
| tagWriter.accept(target, direction, entry); | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.