44import datadog .trace .api .Config ;
55import datadog .trace .api .time .SystemTimeSource ;
66import datadog .trace .api .time .TimeSource ;
7+ import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
8+ import datadog .trace .bootstrap .instrumentation .api .AgentTracer ;
79import java .util .Deque ;
810import java .util .concurrent .ConcurrentHashMap ;
911import java .util .concurrent .ConcurrentLinkedDeque ;
@@ -73,12 +75,16 @@ public boolean preSampleRequest(final @Nonnull AppSecRequestContext ctx) {
7375 return false ;
7476 }
7577 if (counter .tryAcquire ()) {
76- log .debug ("API security sampling is required for this request (presampled)" );
7778 ctx .setKeepOpenForApiSecurityPostProcessing (true );
7879 if (!Config .get ().isApmTracingEnabled ()) {
7980 // Update immediately to prevent concurrent requests from seeing the same expired state
80- return updateApiAccessIfExpired (hash );
81+ boolean sampled = updateApiAccessIfExpired (hash );
82+ if (sampled ) {
83+ logSamplingDecision ("preSampleRequest" , hash );
84+ }
85+ return sampled ;
8186 }
87+ logSamplingDecision ("preSampleRequest" , hash );
8288 return true ;
8389 }
8490 return false ;
@@ -95,7 +101,11 @@ public boolean sampleRequest(AppSecRequestContext ctx) {
95101 // This should never happen, it should have been short-circuited before.
96102 return false ;
97103 }
98- return Config .get ().isApmTracingEnabled () ? updateApiAccessIfExpired (hash ) : true ;
104+ boolean sampled = Config .get ().isApmTracingEnabled () ? updateApiAccessIfExpired (hash ) : true ;
105+ if (sampled ) {
106+ logSamplingDecision ("sampleRequest" , hash );
107+ }
108+ return sampled ;
99109 }
100110
101111 @ Override
@@ -172,4 +182,29 @@ private long computeApiHash(final String route, final String method, final int s
172182 result = 31 * result + statusCode ;
173183 return result ;
174184 }
185+
186+ private void logSamplingDecision (final String method , final long hash ) {
187+ if (!log .isDebugEnabled ()) {
188+ return ;
189+ }
190+
191+ final long timestamp = timeSource .getCurrentTimeMillis ();
192+ AgentSpan activeSpan = AgentTracer .get ().activeSpan ();
193+
194+ if (activeSpan != null ) {
195+ log .debug (
196+ "API security sampling decision in {}: hash={}, traceId={}, spanId={}, timestamp={}" ,
197+ method ,
198+ hash ,
199+ activeSpan .getTraceId (),
200+ activeSpan .getSpanId (),
201+ timestamp );
202+ } else {
203+ log .debug (
204+ "API security sampling decision in {}: hash={}, traceId=null, spanId=null, timestamp={}" ,
205+ method ,
206+ hash ,
207+ timestamp );
208+ }
209+ }
175210}
0 commit comments