-
Notifications
You must be signed in to change notification settings - Fork 359
Add trace.builder.tags.precedence.enabled to let explicit builder tags win (phase 1a) #11738
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
dougqh
wants to merge
9
commits into
master
Choose a base branch
from
dougqh/builder-tags-precedence
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
61bfa55
Add trace.builder.tags.precedence.enabled to let explicit builder tag…
dougqh 1de3fae
Merge branch 'master' into dougqh/builder-tags-precedence
dougqh ddebb2b
Register builder-tags-precedence flag and fix static-config bug
dougqh 0f090a0
Merge branch 'master' into dougqh/builder-tags-precedence
dougqh 73c6766
Merge remote-tracking branch 'origin/master' into HEAD
dougqh 971fe28
Merge branch 'master' into dougqh/builder-tags-precedence
dougqh eb12946
Address review feedback: trim historical comment, add flag-enabled tests
dougqh 4f14098
Merge remote-tracking branch 'origin/dougqh/builder-tags-precedence' …
dougqh 3648103
Merge remote-tracking branch 'origin/master' into dougqh/builder-tags…
dougqh 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
149 changes: 149 additions & 0 deletions
149
dd-trace-core/src/test/java/datadog/trace/core/BuilderTagsPrecedenceTest.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,149 @@ | ||
| package datadog.trace.core; | ||
|
|
||
| import static datadog.trace.api.TracePropagationStyle.DATADOG; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import datadog.trace.api.DDTraceId; | ||
| import datadog.trace.api.TagMap; | ||
| import datadog.trace.api.config.TracerConfig; | ||
| import datadog.trace.api.sampling.PrioritySampling; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import datadog.trace.core.propagation.ExtractedContext; | ||
| import datadog.trace.core.propagation.PropagationTags; | ||
| import java.util.Collections; | ||
| import java.util.Properties; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Characterization of the span-build tag-ordering wart (see {@code CoreTracer} span builder). | ||
| * | ||
| * <p>An inbound header-derived tag ({@code coreTags}) and an explicit per-span builder tag ({@code | ||
| * tagLedger}) that share a key are both applied to the span. Historically the builder tag is | ||
| * applied <em>before</em> {@code coreTags}, so the header tag silently OVERRIDES the explicit | ||
| * builder tag — flagged in-code since 2020 ("maybe the builder tags should come last"). | ||
| * | ||
| * <p>{@link #headerTagOverridesBuilderTagByDefault} pins the <b>default</b> (flag-off) behavior. | ||
| * {@link #builderTagWinsWhenPrecedenceEnabled} exercises the {@code | ||
| * trace.builder.tags.precedence.enabled} flag, which inverts it so the explicit builder tag wins | ||
| * (the logical precedence) -- read per-tracer off the {@code CoreTracerBuilder}'s own {@code | ||
| * Config}, so no process property or forking is needed to flip it in-test. | ||
| */ | ||
| class BuilderTagsPrecedenceTest extends DDCoreJavaSpecification { | ||
|
|
||
| private static final String KEY = "test.collision.tag"; | ||
| private static final String HEADER_VALUE = "from-header"; | ||
| private static final String BUILDER_VALUE = "from-builder"; | ||
|
|
||
| private CoreTracer tracer; | ||
|
|
||
| @AfterEach | ||
| void cleanup() { | ||
| if (tracer != null) { | ||
| tracer.close(); | ||
| } | ||
| } | ||
|
|
||
| /** An extracted context carrying a header-derived tag ({@code coreTags}) on the given key. */ | ||
| private static ExtractedContext extractedWithHeaderTag(String key, String value) { | ||
| return new ExtractedContext( | ||
| DDTraceId.ONE, | ||
| 2, | ||
| PrioritySampling.SAMPLER_KEEP, | ||
| null, | ||
| 0, | ||
| Collections.<String, String>emptyMap(), | ||
| TagMap.fromMap(Collections.singletonMap(key, value)), | ||
| null, | ||
| PropagationTags.factory().empty(), | ||
| null, | ||
| DATADOG); | ||
| } | ||
|
|
||
| /** | ||
| * Default config: the historical order applies, so the inbound header tag overrides the explicit | ||
| * builder tag. This documents the wart; flipping the default would (intentionally) break this. | ||
| */ | ||
| @Test | ||
| void headerTagOverridesBuilderTagByDefault() { | ||
| tracer = tracerBuilder().build(); | ||
| AgentSpan span = | ||
| tracer | ||
| .buildSpan("test", "root") | ||
| .asChildOf(extractedWithHeaderTag(KEY, HEADER_VALUE)) | ||
| .withTag(KEY, BUILDER_VALUE) | ||
| .start(); | ||
| try { | ||
| Object resolved = ((DDSpan) span).getTag(KEY); | ||
| assertEquals( | ||
| HEADER_VALUE, | ||
| resolved, | ||
| "By default the historical order lets the inbound header tag override the explicit " | ||
| + "builder tag (the documented wart). If this fails, the default ordering changed."); | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
| } | ||
|
dougqh marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * With the flag enabled, the explicit builder tag is applied last, so it wins over the inbound | ||
| * header tag -- the inversion this PR adds. | ||
| */ | ||
| @Test | ||
| void builderTagWinsWhenPrecedenceEnabled() { | ||
| Properties properties = new Properties(); | ||
| properties.setProperty(TracerConfig.TRACE_BUILDER_TAGS_PRECEDENCE_ENABLED, "true"); | ||
| tracer = tracerBuilder().withProperties(properties).build(); | ||
|
|
||
| AgentSpan span = | ||
| tracer | ||
| .buildSpan("test", "root") | ||
| .asChildOf(extractedWithHeaderTag(KEY, HEADER_VALUE)) | ||
| .withTag(KEY, BUILDER_VALUE) | ||
| .start(); | ||
| try { | ||
| Object resolved = ((DDSpan) span).getTag(KEY); | ||
| assertEquals( | ||
| BUILDER_VALUE, | ||
| resolved, | ||
| "With trace.builder.tags.precedence.enabled=true, the explicit builder tag must win " | ||
| + "over the inbound header tag."); | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Confirms the flag is read from the tracer's own config (set via {@code | ||
| * CoreTracerBuilder#withProperties}), not a cached global default -- the bug fixed alongside this | ||
| * test, per the review discussion on this PR. | ||
| */ | ||
| @Test | ||
| void precedenceFlagIsPerTracerNotAGlobalDefault() { | ||
| tracer = tracerBuilder().build(); | ||
| Properties properties = new Properties(); | ||
| properties.setProperty(TracerConfig.TRACE_BUILDER_TAGS_PRECEDENCE_ENABLED, "true"); | ||
| CoreTracer enabledTracer = tracerBuilder().withProperties(properties).build(); | ||
| try { | ||
| AgentSpan defaultSpan = | ||
| tracer | ||
| .buildSpan("test", "root") | ||
| .asChildOf(extractedWithHeaderTag(KEY, HEADER_VALUE)) | ||
| .withTag(KEY, BUILDER_VALUE) | ||
| .start(); | ||
| defaultSpan.finish(); | ||
| assertEquals(HEADER_VALUE, ((DDSpan) defaultSpan).getTag(KEY)); | ||
|
|
||
| AgentSpan enabledSpan = | ||
| enabledTracer | ||
| .buildSpan("test", "root") | ||
| .asChildOf(extractedWithHeaderTag(KEY, HEADER_VALUE)) | ||
| .withTag(KEY, BUILDER_VALUE) | ||
| .start(); | ||
| enabledSpan.finish(); | ||
| assertEquals(BUILDER_VALUE, ((DDSpan) enabledSpan).getTag(KEY)); | ||
| } finally { | ||
| enabledTracer.close(); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.