Skip to content

Commit b70afb3

Browse files
committed
test logs
1 parent c6297d4 commit b70afb3

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

dd-java-agent/appsec/src/main/java/com/datadog/appsec/api/security/ApiSecuritySamplerImpl.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import datadog.trace.api.Config;
55
import datadog.trace.api.time.SystemTimeSource;
66
import datadog.trace.api.time.TimeSource;
7+
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
8+
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
79
import java.util.Deque;
810
import java.util.concurrent.ConcurrentHashMap;
911
import java.util.concurrent.ConcurrentLinkedDeque;
@@ -69,14 +71,21 @@ public boolean preSampleRequest(final @Nonnull AppSecRequestContext ctx) {
6971
}
7072
long hash = computeApiHash(route, method, statusCode);
7173
ctx.setApiSecurityEndpointHash(hash);
72-
if (!isApiAccessExpired(hash)) {
74+
75+
long currentTime = timeSource.getCurrentTimeMillis();
76+
boolean isExpired = isApiAccessExpired(hash);
77+
78+
if (!isExpired) {
79+
logSamplingDecision("preSampleRequest", currentTime, false, "not expired");
7380
return false;
7481
}
7582
if (counter.tryAcquire()) {
7683
log.debug("API security sampling is required for this request (presampled)");
7784
ctx.setKeepOpenForApiSecurityPostProcessing(true);
85+
logSamplingDecision("preSampleRequest", currentTime, true, "acquired semaphore");
7886
return true;
7987
}
88+
logSamplingDecision("preSampleRequest", currentTime, false, "semaphore full");
8089
return false;
8190
}
8291

@@ -91,7 +100,11 @@ public boolean sampleRequest(AppSecRequestContext ctx) {
91100
// This should never happen, it should have been short-circuited before.
92101
return false;
93102
}
94-
return updateApiAccessIfExpired(hash);
103+
long currentTime = timeSource.getCurrentTimeMillis();
104+
boolean decision = updateApiAccessIfExpired(hash);
105+
logSamplingDecision(
106+
"sampleRequest", currentTime, decision, decision ? "sampled" : "already sampled");
107+
return decision;
95108
}
96109

97110
@Override
@@ -168,4 +181,27 @@ private long computeApiHash(final String route, final String method, final int s
168181
result = 31 * result + statusCode;
169182
return result;
170183
}
184+
185+
private void logSamplingDecision(String method, long timestamp, boolean decision, String reason) {
186+
AgentSpan activeSpan = AgentTracer.get().activeSpan();
187+
String traceId = "null";
188+
String spanId = "null";
189+
190+
if (activeSpan != null) {
191+
if (activeSpan.getTraceId() != null) {
192+
traceId = activeSpan.getTraceId().toString();
193+
}
194+
spanId = String.valueOf(activeSpan.getSpanId());
195+
}
196+
197+
log.info(
198+
"[API_SECURITY_SAMPLING] method={}, timestamp={}, traceId={}, spanId={}, decision={}, reason={}, expirationTimeMs={}",
199+
method,
200+
timestamp,
201+
traceId,
202+
spanId,
203+
decision,
204+
reason,
205+
expirationTimeInMs);
206+
}
171207
}

dd-java-agent/appsec/src/main/java/com/datadog/appsec/api/security/AppSecSpanPostProcessor.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,45 @@ public AppSecSpanPostProcessor(ApiSecuritySampler sampler, EventProducerService
3131

3232
@Override
3333
public void process(@Nonnull AgentSpan span, @Nonnull BooleanSupplier timeoutCheck) {
34+
long timestamp = System.currentTimeMillis();
35+
String traceId = span.getTraceId() != null ? span.getTraceId().toString() : "null";
36+
String spanId = String.valueOf(span.getSpanId());
37+
3438
final RequestContext ctx_ = span.getRequestContext();
3539
if (ctx_ == null) {
40+
logProcessingDecision(timestamp, traceId, spanId, false, "no request context", "start");
3641
return;
3742
}
3843
final AppSecRequestContext ctx = ctx_.getData(RequestContextSlot.APPSEC);
3944
if (ctx == null) {
45+
logProcessingDecision(timestamp, traceId, spanId, false, "no appsec context", "start");
4046
return;
4147
}
4248

4349
if (!ctx.isKeepOpenForApiSecurityPostProcessing()) {
50+
logProcessingDecision(
51+
timestamp, traceId, spanId, false, "not marked for post-processing", "start");
4452
return;
4553
}
4654

4755
try {
4856
if (timeoutCheck.getAsBoolean()) {
4957
log.debug("Timeout detected, skipping API security post-processing");
58+
logProcessingDecision(
59+
timestamp, traceId, spanId, false, "timeout detected", "pre-sampling");
5060
return;
5161
}
5262
if (!sampler.sampleRequest(ctx)) {
5363
log.debug("Request not sampled, skipping API security post-processing");
64+
logProcessingDecision(
65+
timestamp, traceId, spanId, false, "request not sampled", "post-sampling");
5466
return;
5567
}
5668
log.debug("Request sampled, processing API security post-processing");
69+
logProcessingDecision(
70+
timestamp, traceId, spanId, true, "sampled, extracting schemas", "extracting");
5771
extractSchemas(ctx, ctx_.getTraceSegment());
72+
logProcessingDecision(timestamp, traceId, spanId, true, "extraction completed", "completed");
5873
} finally {
5974
ctx.setKeepOpenForApiSecurityPostProcessing(false);
6075
try {
@@ -67,6 +82,7 @@ public void process(@Nonnull AgentSpan span, @Nonnull BooleanSupplier timeoutChe
6782
log.debug("Error closing AppSecRequestContext", e);
6883
}
6984
sampler.releaseOne();
85+
logProcessingDecision(timestamp, traceId, spanId, false, "cleanup completed", "cleanup");
7086
}
7187
}
7288

@@ -89,4 +105,21 @@ private void extractSchemas(final AppSecRequestContext ctx, final TraceSegment t
89105
log.debug("Subscriber info expired", e);
90106
}
91107
}
108+
109+
private void logProcessingDecision(
110+
long timestamp,
111+
String traceId,
112+
String spanId,
113+
boolean processed,
114+
String reason,
115+
String stage) {
116+
log.info(
117+
"[APPSEC_SPAN_POST_PROCESSING] timestamp={}, traceId={}, spanId={}, processed={}, reason={}, stage={}",
118+
timestamp,
119+
traceId,
120+
spanId,
121+
processed,
122+
reason,
123+
stage);
124+
}
92125
}

0 commit comments

Comments
 (0)