Repository navigation
🍒 12028 - Stamp _dd.apm.enabled on every exported chunk when APM tracing is disabled #12068
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
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
175 changes: 175 additions & 0 deletions
175
dd-trace-core/src/test/java/datadog/trace/core/ApmTracingDisabledChunkMarkerTest.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,175 @@ | ||
| package datadog.trace.core; | ||
|
|
||
| import static datadog.trace.api.DDTags.APM_ENABLED; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import datadog.trace.common.writer.ListWriter; | ||
| import datadog.trace.junit.utils.config.WithConfig; | ||
| import java.util.List; | ||
| import java.util.concurrent.TimeoutException; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * When APM tracing is disabled ({@code DD_APM_TRACING_ENABLED=false}), the intake bills APM host | ||
| * usage for every exported trace <em>chunk</em> that does not carry the {@code _dd.apm.enabled:0} | ||
| * marker. To make sure no chunk gets flushed without the correct billing tag, it gets added to | ||
| * every single span. These tests lock that in. | ||
| */ | ||
| @WithConfig(key = "apm.tracing.enabled", value = "false") | ||
| class ApmTracingDisabledChunkMarkerTest extends DDCoreJavaSpecification { | ||
|
|
||
| private ListWriter writer; | ||
| private CoreTracer tracer; | ||
|
|
||
| @Override | ||
| protected boolean useStrictTraceWrites() { | ||
| // Production standalone-ASM uses the delaying PendingTraceBuffer, which is what lets a buffered | ||
| // root be written on its own before a late child arrives. Strict writes would instead select | ||
| // the discarding buffer and coalesce both spans into a single chunk, hiding the bug. | ||
| return false; | ||
| } | ||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| writer = new ListWriter(); | ||
| tracer = tracerBuilder().writer(writer).build(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void cleanup() { | ||
| if (tracer != null) { | ||
| tracer.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void everyExportedChunkCarriesApmDisabledMarker() throws InterruptedException, TimeoutException { | ||
| DDSpan root = (DDSpan) tracer.buildSpan("test", "root").start(); | ||
| DDSpan child = (DDSpan) tracer.buildSpan("test", "child").asChildOf(root.spanContext()).start(); | ||
|
|
||
| PendingTrace trace = (PendingTrace) root.spanContext().getTraceCollector(); | ||
|
|
||
| // Root finishes while the child is still running -> root is buffered, not yet written. | ||
| root.finish(); | ||
| assertTrue(writer.isEmpty()); | ||
|
|
||
| // Write the buffered root out on its own chunk. This sets rootSpanWritten=true and is exactly | ||
| // what the PendingTraceBuffer worker does when a buffered root ages out (SEND_DELAY_NS). | ||
| trace.write(); | ||
| writer.waitForTraces(1); | ||
|
|
||
| // The long-lived child finishes later. Because the root was already written, it flushes alone. | ||
| child.finish(); | ||
| trace.write(); | ||
| writer.waitForTraces(2); | ||
|
|
||
| List<DDSpan> rootChunk = writer.get(0); | ||
| List<DDSpan> childChunk = writer.get(1); | ||
| assertEquals(1, rootChunk.size(), "expected the root to flush alone in the first chunk"); | ||
| assertEquals(1, childChunk.size(), "expected the child to flush alone in the second chunk"); | ||
| assertEquals(root, rootChunk.get(0), "first chunk should be the local root"); | ||
| assertEquals(child, childChunk.get(0), "second chunk should be the delayed child"); | ||
|
|
||
| // Sanity: the child really is a child of the local root (a local, non-remote parent) — this is | ||
| // exactly the case that stamping the marker only on the local root span would miss. | ||
| assertEquals(root.getSpanId(), child.getParentId()); | ||
|
|
||
| // Both chunks must carry the billing marker. The delayed child chunk is load-bearing: without | ||
| // the marker the intake would bill APM host usage for this otherwise-unmarked chunk. | ||
| assertAllSpansMarked(rootChunk, "root chunk"); | ||
| assertAllSpansMarked(childChunk, "delayed child chunk"); | ||
| } | ||
|
|
||
| @Test | ||
| void singleChunkTraceCarriesApmDisabledMarker() throws InterruptedException, TimeoutException { | ||
| // Positive control: when the whole trace is exported as a single chunk (child finishes before | ||
| // the root, so nothing is written until the root closes), the marker is present on every span. | ||
| DDSpan root = (DDSpan) tracer.buildSpan("test", "root").start(); | ||
| DDSpan child = (DDSpan) tracer.buildSpan("test", "child").asChildOf(root.spanContext()).start(); | ||
|
|
||
| child.finish(); | ||
| assertTrue(writer.isEmpty(), "trace must not be written while the root is still open"); | ||
| root.finish(); | ||
| writer.waitForTraces(1); | ||
|
|
||
| assertEquals(1, writer.size(), "expected the whole trace in a single chunk"); | ||
| List<DDSpan> chunk = writer.get(0); | ||
| assertEquals(2, chunk.size(), "expected both spans in the same chunk"); | ||
| assertAllSpansMarked(chunk, "single-chunk trace"); | ||
| } | ||
|
|
||
| @Test | ||
| void rootlessMultiSpanChunkMarksEverySpan() throws InterruptedException, TimeoutException { | ||
| // A multi-span chunk exported WITHOUT its local root (a partial flush / orphaned subtree): the | ||
| // root is written first, then its descendants flush together in a later, root-less chunk. Every | ||
| // span in that chunk must still carry the marker. | ||
| DDSpan root = (DDSpan) tracer.buildSpan("test", "root").start(); | ||
| DDSpan childA = | ||
| (DDSpan) tracer.buildSpan("test", "childA").asChildOf(root.spanContext()).start(); | ||
| DDSpan grandchild = | ||
| (DDSpan) tracer.buildSpan("test", "grandchild").asChildOf(childA.spanContext()).start(); | ||
|
|
||
| PendingTrace trace = (PendingTrace) root.spanContext().getTraceCollector(); | ||
|
|
||
| // Flush the root on its own first (rootSpanWritten=true), so the descendants can only export in | ||
| // a later, root-less chunk. | ||
| root.finish(); | ||
| trace.write(); | ||
| writer.waitForTraces(1); | ||
| assertEquals(1, writer.get(0).size(), "expected the root to flush alone in the first chunk"); | ||
|
|
||
| // Both descendants finish, then flush together in a single root-less chunk. | ||
| childA.finish(); | ||
| grandchild.finish(); | ||
| trace.write(); | ||
| writer.waitForTraces(2); | ||
|
|
||
| List<DDSpan> chunk = writer.get(1); | ||
| assertEquals(2, chunk.size(), "expected childA and grandchild in one root-less chunk"); | ||
| assertTrue(chunk.contains(childA) && chunk.contains(grandchild), "chunk must hold both spans"); | ||
| assertTrue(!chunk.contains(root), "the local root must not be part of this chunk"); | ||
|
|
||
| // Every span in the root-less chunk carries the marker, including the inner grandchild whose | ||
| // parent is present in the chunk. | ||
| assertAllSpansMarked(chunk, "root-less descendant chunk"); | ||
| } | ||
|
|
||
| @Test | ||
| @WithConfig(key = "apm.tracing.enabled", value = "true") | ||
| void apmTracingEnabledLeavesChunkUnmarked() throws InterruptedException, TimeoutException { | ||
| // Negative control: the method-level override flips APM tracing back on (overriding the | ||
| // class-level false before setup() builds the tracer), so the billing marker must NOT be | ||
| // stamped on any span. Guards against a regression that marks unconditionally, ignoring the | ||
| // apm.tracing.enabled flag. | ||
| DDSpan root = (DDSpan) tracer.buildSpan("test", "root").start(); | ||
| DDSpan child = (DDSpan) tracer.buildSpan("test", "child").asChildOf(root.spanContext()).start(); | ||
|
|
||
| child.finish(); | ||
| root.finish(); | ||
| writer.waitForTraces(1); | ||
|
|
||
| List<DDSpan> chunk = writer.get(0); | ||
| assertEquals(2, chunk.size(), "expected both spans in the same chunk"); | ||
| for (DDSpan span : chunk) { | ||
| assertNull( | ||
| span.getTag(APM_ENABLED), | ||
| "span '" | ||
| + span.getOperationName() | ||
| + "' must not carry _dd.apm.enabled when APM tracing is enabled"); | ||
| } | ||
| } | ||
|
|
||
| /** Every span of an exported chunk must carry the numeric {@code _dd.apm.enabled:0} marker. */ | ||
| private static void assertAllSpansMarked(List<DDSpan> chunk, String description) { | ||
| for (DDSpan span : chunk) { | ||
| assertEquals( | ||
| Integer.valueOf(0), | ||
| span.getTag(APM_ENABLED), | ||
| description + " span '" + span.getOperationName() + "' is missing _dd.apm.enabled:0"); | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -194,7 +194,6 @@ | |
| import static datadog.trace.api.ConfigDefaults.DEFAULT_WEBSOCKET_MESSAGES_SEPARATE_TRACES; | ||
| import static datadog.trace.api.ConfigDefaults.DEFAULT_WEBSOCKET_TAG_SESSION_ID; | ||
| import static datadog.trace.api.ConfigSetting.NON_DEFAULT_SEQ_ID; | ||
| import static datadog.trace.api.DDTags.APM_ENABLED; | ||
| import static datadog.trace.api.DDTags.HOST_TAG; | ||
| import static datadog.trace.api.DDTags.INTERNAL_HOST_NAME; | ||
| import static datadog.trace.api.DDTags.LANGUAGE_TAG_KEY; | ||
|
|
@@ -5169,9 +5168,10 @@ public TagMap getLocalRootSpanTags() { | |
| result.put(LANGUAGE_TAG_KEY, LANGUAGE_TAG_VALUE); | ||
| result.put(SCHEMA_VERSION_TAG_KEY, SpanNaming.instance().version()); | ||
| result.put(DDTags.PROFILING_ENABLED, isProfilingEnabled() ? 1 : 0); | ||
| if (!isApmTracingEnabled()) { | ||
| result.put(APM_ENABLED, 0); | ||
| } | ||
| // The _dd.apm.enabled:0 billing marker is intentionally NOT set here. When APM tracing is | ||
| // disabled it is stamped on every span of each exported chunk (see CoreTracer.write), so that | ||
| // chunks flushed without their local root span (e.g. a late child span) still opt out of APM | ||
| // host billing. | ||
|
Comment on lines
+5171
to
+5174
Collaborator
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. 🎯 suggestion: This kind of comment should go to the tag declaration instead. |
||
|
|
||
| if (reportHostName) { | ||
| final String hostName = getHostName(); | ||
|
|
||
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.
🔨 issue: Use the config name constant instead of string literal