-
Notifications
You must be signed in to change notification settings - Fork 344
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
Open
claponcet
wants to merge
5
commits into
master
Choose a base branch
from
clara.poncet/ai-guard-standalone
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
5 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 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
119 changes: 119 additions & 0 deletions
119
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,119 @@ | ||
| 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.SAMPLER_DROP; | ||
| import static datadog.trace.api.sampling.PrioritySampling.USER_KEEP; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| 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.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 { | ||
| // consume the first allowed slot | ||
| DDSpan first = (DDSpan) tracer.buildSpan("datadog", "op").start(); | ||
| first.finish(); | ||
| writer.waitForTraces(1); | ||
|
|
||
| // second trace sets _dd.p.ts but NOT AI_GUARD_KEEP — should still be dropped | ||
| DDSpan second = (DDSpan) tracer.buildSpan("datadog", "op").start(); | ||
| second.setTag(Tags.PROPAGATED_TRACE_SOURCE, ProductTraceSource.AI_GUARD); | ||
| second.finish(); | ||
| writer.waitForTraces(2); | ||
|
|
||
| assertEquals(SAMPLER_DROP, (int) second.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.