Repository navigation
Stop repeating global tags as OTLP span attributes #12813
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
Open
mhlidd
wants to merge
3
commits into
master
Choose a base branch
from
matthew.li/otlp-dedup-resource-span-tags
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+409
−4
Open
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
78 changes: 78 additions & 0 deletions
78
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,78 @@ | ||
| package datadog.trace.core.otlp.trace; | ||
|
|
||
| import datadog.trace.api.TagMap; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.BiConsumer; | ||
|
|
||
| /** | ||
| * Visits the span tags to export over OTLP, leaving out tags the trace resource already carries. | ||
| * | ||
| * <p>Tracer-wide tags (global tags, {@code DD_SPAN_TAGS}, {@code env}, ...) are shared by spans as | ||
| * the frozen read-through parent of each span's {@link TagMap}. Global tags are also exported once | ||
| * per payload as resource attributes, so repeating them on every span is redundant. The parent's | ||
| * other "leftover" entries are worked out once per parent and cached; each span then writes its own | ||
| * local tags followed by whichever leftovers it can still see. | ||
| * | ||
| * <p>Spans without a read-through parent (e.g. when tracer-wide tags need interception and are | ||
| * copied into each span) have all their tags written, as before. | ||
| * | ||
| * <p>Not thread-safe: each encoder owns its own instance. | ||
| */ | ||
| final class OtlpSpanTags { | ||
| private static final TagMap.EntryReader[] NO_ENTRIES = new TagMap.EntryReader[0]; | ||
|
|
||
| /** Attributes carried by the trace resource. */ | ||
| private final Map<String, String> resourceAttributes; | ||
|
|
||
| /** Read-through parent the cached leftovers were computed from. */ | ||
| private TagMap cachedParent; | ||
|
|
||
| /** Entries of {@link #cachedParent} not covered by the resource, so still written per span. */ | ||
| private TagMap.EntryReader[] cachedLeftovers = NO_ENTRIES; | ||
|
|
||
| OtlpSpanTags(Map<String, String> resourceAttributes) { | ||
| this.resourceAttributes = resourceAttributes; | ||
| } | ||
|
|
||
| <T> void forEach(TagMap tags, T thisObj, BiConsumer<T, ? super TagMap.EntryReader> consumer) { | ||
| TagMap parent = tags.parent(); | ||
| if (null == parent || resourceAttributes.isEmpty()) { | ||
| // nothing shared with the resource | ||
| tags.forEach(thisObj, consumer); | ||
| return; | ||
| } | ||
| tags.forEachLocal(thisObj, consumer); | ||
| for (TagMap.EntryReader leftover : leftovers(parent)) { | ||
| // skips leftovers the span holds locally (already written) or removed | ||
| if (tags.readsThroughToParent(leftover.tag())) { | ||
| consumer.accept(thisObj, leftover); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private TagMap.EntryReader[] leftovers(TagMap parent) { | ||
| if (parent != cachedParent) { | ||
| List<TagMap.EntryReader> leftovers = new ArrayList<>(); | ||
| parent.forEach( | ||
| entry -> { | ||
| if (!coveredByResource(entry)) { | ||
| leftovers.add(entry); | ||
| } | ||
| }); | ||
| cachedLeftovers = leftovers.toArray(NO_ENTRIES); | ||
| cachedParent = parent; | ||
|
mhlidd marked this conversation as resolved.
|
||
| } | ||
| return cachedLeftovers; | ||
| } | ||
|
|
||
| /** | ||
| * Covered when the resource has the same key and value. Only object entries qualify, since those | ||
| * are written as string attributes just like the resource; numeric and boolean entries keep their | ||
| * type on the span. | ||
| */ | ||
| private boolean coveredByResource(TagMap.EntryReader entry) { | ||
| return entry.isObject() && entry.stringValue().equals(resourceAttributes.get(entry.tag())); | ||
| } | ||
| } | ||
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
89 changes: 89 additions & 0 deletions
89
dd-trace-core/src/test/java/datadog/trace/core/otlp/trace/OtlpSpanTagsForkedTest.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,89 @@ | ||
| package datadog.trace.core.otlp.trace; | ||
|
|
||
| import static java.util.Arrays.asList; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| import datadog.json.JsonMapper; | ||
| import datadog.trace.api.config.GeneralConfig; | ||
| import datadog.trace.api.config.TracerConfig; | ||
| import datadog.trace.common.writer.ListWriter; | ||
| import datadog.trace.core.CoreSpan; | ||
| import datadog.trace.core.CoreTracer; | ||
| import datadog.trace.core.DDSpan; | ||
| import datadog.trace.core.otlp.common.OtlpPayload; | ||
| import datadog.trace.test.junit.utils.config.WithConfig; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * End-to-end check that global tags exported as OTLP resource attributes are not repeated on each | ||
| * span, while span-only tracer tags still are. Forked because the trace resource is built once from | ||
| * {@code Config} on first use. | ||
| */ | ||
| @WithConfig(key = GeneralConfig.TAGS, value = "team:core,region:us1") | ||
| @WithConfig(key = TracerConfig.SPAN_TAGS, value = "owner:alice") | ||
| class OtlpSpanTagsForkedTest { | ||
|
|
||
| @Test | ||
| void globalTagsOnlyOnResource() throws IOException { | ||
| CoreTracer tracer = CoreTracer.builder().writer(new ListWriter()).build(); | ||
| DDSpan span = (DDSpan) tracer.startSpan("test", "op"); | ||
| span.setTag("http.method", "GET"); | ||
| span.setTag("region", "eu1"); // span-level override of a global tag is kept | ||
| span.finish(); | ||
|
|
||
| OtlpTraceJsonCollector collector = new OtlpTraceJsonCollector(); | ||
| collector.addTrace(asList((CoreSpan<?>) span)); | ||
| Map<String, Object> resourceSpans = parse(collector.collectTraces()); | ||
|
|
||
| Map<String, Object> resourceAttributes = attributes(resource(resourceSpans)); | ||
| assertEquals("core", resourceAttributes.get("team")); | ||
| assertEquals("us1", resourceAttributes.get("region")); | ||
|
|
||
| Map<String, Object> spanAttributes = attributes(onlySpan(resourceSpans)); | ||
| assertFalse(spanAttributes.containsKey("team")); | ||
| assertEquals("eu1", spanAttributes.get("region")); | ||
| assertEquals("alice", spanAttributes.get("owner")); | ||
| assertEquals("GET", spanAttributes.get("http.method")); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static Map<String, Object> parse(OtlpPayload payload) throws IOException { | ||
| byte[] bytes = new byte[payload.getContentLength()]; | ||
| payload.getContent().get(bytes); | ||
| Map<String, Object> root = JsonMapper.fromJsonToMap(new String(bytes, StandardCharsets.UTF_8)); | ||
| return (Map<String, Object>) ((List<Object>) root.get("resourceSpans")).get(0); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static Map<String, Object> resource(Map<String, Object> resourceSpans) { | ||
| return (Map<String, Object>) resourceSpans.get("resource"); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static Map<String, Object> onlySpan(Map<String, Object> resourceSpans) { | ||
| List<Object> scopeSpans = (List<Object>) resourceSpans.get("scopeSpans"); | ||
| List<Object> spans = (List<Object>) ((Map<String, Object>) scopeSpans.get(0)).get("spans"); | ||
| assertEquals(1, spans.size()); | ||
| return (Map<String, Object>) spans.get(0); | ||
| } | ||
|
|
||
| /** Flattens OTLP JSON {@code KeyValue} attributes, failing on duplicate keys. */ | ||
| @SuppressWarnings("unchecked") | ||
| private static Map<String, Object> attributes(Map<String, Object> owner) { | ||
| Map<String, Object> result = new HashMap<>(); | ||
| for (Object attribute : (List<Object>) owner.get("attributes")) { | ||
| Map<String, Object> keyValue = (Map<String, Object>) attribute; | ||
| Map<String, Object> value = (Map<String, Object>) keyValue.get("value"); | ||
| Object previous = result.put((String) keyValue.get("key"), value.values().iterator().next()); | ||
| assertNull(previous, "duplicate attribute " + keyValue.get("key")); | ||
| } | ||
| return result; | ||
| } | ||
| } |
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.