-
Notifications
You must be signed in to change notification settings - Fork 345
Support DD_APM_TRACING_ENABLED=false for AI Guard traces #11885
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 12 commits into
master
from
clara.poncet/ai-guard-standalone
Jul 22, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
42d77de
Support DD_APM_TRACING_ENABLED=false for AI Guard traces
claponcet 1e9ccb8
Fix remaining EVENT_TAG reference in Groovy test
claponcet 40cce9e
Merge branch 'master' into clara.poncet/ai-guard-standalone
claponcet c4290f3
Set AI Guard bit on _dd.p.ts propagated trace source tag
claponcet 2490539
Merge branch 'master' into clara.poncet/ai-guard-standalone
claponcet e846cb5
Fix flaky AIGuardSamplingTest by asserting on force-keep, not sampler…
claponcet ffccf2f
Merge branch 'master' into clara.poncet/ai-guard-standalone
claponcet 8f17c5b
Merge branch 'master' into clara.poncet/ai-guard-standalone
claponcet 123505c
Replace isProductMarked varargs overload with allocation-free two-arg…
claponcet 5c40eea
Merge branch 'master' into clara.poncet/ai-guard-standalone
claponcet 7e42da8
Merge branch 'master' into clara.poncet/ai-guard-standalone
claponcet 6d4db9a
Fix stale junit-utils import after test-junit-utils module rename in …
claponcet 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
114 changes: 114 additions & 0 deletions
114
dd-trace-core/src/test/java/datadog/trace/common/sampling/AIGuardSamplingTest.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,114 @@ | ||
| package datadog.trace.common.sampling; | ||
|
|
||
| import static datadog.trace.api.config.AppSecConfig.APPSEC_ENABLED; | ||
| import static datadog.trace.api.config.GeneralConfig.APM_TRACING_ENABLED; | ||
| import static datadog.trace.api.sampling.PrioritySampling.USER_KEEP; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
|
||
| import datadog.trace.api.ProductTraceSource; | ||
| import datadog.trace.bootstrap.instrumentation.api.Tags; | ||
| import datadog.trace.common.writer.ListWriter; | ||
| import datadog.trace.core.CoreTracer; | ||
| import datadog.trace.core.DDCoreJavaSpecification; | ||
| import datadog.trace.core.DDSpan; | ||
| import datadog.trace.test.junit.utils.config.WithConfig; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** Verifies that AI Guard traces are kept regardless of APM/ASM configuration. */ | ||
| public class AIGuardSamplingTest extends DDCoreJavaSpecification { | ||
|
|
||
| private ListWriter writer; | ||
| private CoreTracer tracer; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| writer = new ListWriter(); | ||
| tracer = tracerBuilder().writer(writer).build(); | ||
| } | ||
|
|
||
| @Test | ||
| void aiGuardTraceIsKeptWhenApmEnabled() throws Exception { | ||
| DDSpan span = (DDSpan) tracer.buildSpan("datadog", "operation").start(); | ||
| span.setTag(Tags.AI_GUARD_KEEP, true); | ||
| span.setTag(Tags.AI_GUARD_EVENT, true); | ||
| span.setTag(Tags.PROPAGATED_TRACE_SOURCE, ProductTraceSource.AI_GUARD); | ||
| span.finish(); | ||
| writer.waitForTraces(1); | ||
| assertEquals(USER_KEEP, (int) span.getSamplingPriority()); | ||
| assertDecisionMakerIsAiGuard(span); | ||
| } | ||
|
|
||
| @Test | ||
| @WithConfig(key = APM_TRACING_ENABLED, value = "false") | ||
| void aiGuardTraceIsKeptWhenApmAndAsmDisabled() throws Exception { | ||
| DDSpan span = (DDSpan) tracer.buildSpan("datadog", "operation").start(); | ||
| span.setTag(Tags.AI_GUARD_KEEP, true); | ||
| span.setTag(Tags.AI_GUARD_EVENT, true); | ||
| span.setTag(Tags.PROPAGATED_TRACE_SOURCE, ProductTraceSource.AI_GUARD); | ||
| span.finish(); | ||
| writer.waitForTraces(1); | ||
| assertEquals(USER_KEEP, (int) span.getSamplingPriority()); | ||
| assertDecisionMakerIsAiGuard(span); | ||
| } | ||
|
|
||
| @Test | ||
| @WithConfig(key = APM_TRACING_ENABLED, value = "false") | ||
| @WithConfig(key = APPSEC_ENABLED, value = "true") | ||
| void aiGuardTraceIsKeptInAsmStandaloneMode() throws Exception { | ||
| DDSpan span = (DDSpan) tracer.buildSpan("datadog", "operation").start(); | ||
| span.setTag(Tags.AI_GUARD_KEEP, true); | ||
| span.setTag(Tags.AI_GUARD_EVENT, true); | ||
| span.setTag(Tags.PROPAGATED_TRACE_SOURCE, ProductTraceSource.AI_GUARD); | ||
| span.finish(); | ||
| writer.waitForTraces(1); | ||
| assertEquals(USER_KEEP, (int) span.getSamplingPriority()); | ||
| assertDecisionMakerIsAiGuard(span); | ||
| } | ||
|
|
||
| /** _dd.p.ts alone (without AI_GUARD_KEEP) must not bypass the sampler. */ | ||
| @Test | ||
| @WithConfig(key = APM_TRACING_ENABLED, value = "false") | ||
| @WithConfig(key = APPSEC_ENABLED, value = "true") | ||
| void propagatedTraceSourceAloneDoesNotBypassSampler() throws Exception { | ||
| DDSpan span = (DDSpan) tracer.buildSpan("datadog", "op").start(); | ||
| span.setTag(Tags.PROPAGATED_TRACE_SOURCE, ProductTraceSource.AI_GUARD); | ||
| span.finish(); | ||
| writer.waitForTraces(1); | ||
|
|
||
| // _dd.p.ts alone must not force-keep the trace; only AI_GUARD_KEEP does that. | ||
| assertNotEquals(USER_KEEP, (int) span.getSamplingPriority()); | ||
| } | ||
|
|
||
| /** | ||
| * AI Guard traces must be kept even when AsmStandaloneSampler has exhausted its rate-limit slot. | ||
| */ | ||
| @Test | ||
| @WithConfig(key = APM_TRACING_ENABLED, value = "false") | ||
| @WithConfig(key = APPSEC_ENABLED, value = "true") | ||
| void aiGuardTraceIsKeptAfterRateLimitSlotIsExhausted() throws Exception { | ||
| // consume the first allowed slot | ||
| DDSpan first = (DDSpan) tracer.buildSpan("datadog", "op").start(); | ||
| first.finish(); | ||
| writer.waitForTraces(1); | ||
|
|
||
| // AI Guard trace must still be force-kept even though the rate-limit slot is gone | ||
| DDSpan aiGuard = (DDSpan) tracer.buildSpan("datadog", "op").start(); | ||
| aiGuard.setTag(Tags.AI_GUARD_KEEP, true); | ||
| aiGuard.setTag(Tags.AI_GUARD_EVENT, true); | ||
| aiGuard.setTag(Tags.PROPAGATED_TRACE_SOURCE, ProductTraceSource.AI_GUARD); | ||
| aiGuard.finish(); | ||
| writer.waitForTraces(2); | ||
|
|
||
| assertEquals(USER_KEEP, (int) aiGuard.getSamplingPriority()); | ||
| assertDecisionMakerIsAiGuard(aiGuard); | ||
| } | ||
|
|
||
| private static void assertDecisionMakerIsAiGuard(DDSpan span) { | ||
| Map<String, String> ptags = span.spanContext().getPropagationTags().createTagMap(); | ||
| assertEquals("-13", ptags.get("_dd.p.dm"), "_dd.p.dm must be -13 (AI Guard decision maker)"); | ||
| assertEquals("20", ptags.get("_dd.p.ts"), "_dd.p.ts must have AI Guard bit set (0x20)"); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.