Skip to content

Commit 42c6f9a

Browse files
committed
new approach and remove logs
1 parent b70afb3 commit 42c6f9a

File tree

2 files changed

+6
-71
lines changed

2 files changed

+6
-71
lines changed

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

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
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;
97
import java.util.Deque;
108
import java.util.concurrent.ConcurrentHashMap;
119
import java.util.concurrent.ConcurrentLinkedDeque;
@@ -71,21 +69,18 @@ public boolean preSampleRequest(final @Nonnull AppSecRequestContext ctx) {
7169
}
7270
long hash = computeApiHash(route, method, statusCode);
7371
ctx.setApiSecurityEndpointHash(hash);
74-
75-
long currentTime = timeSource.getCurrentTimeMillis();
76-
boolean isExpired = isApiAccessExpired(hash);
77-
78-
if (!isExpired) {
79-
logSamplingDecision("preSampleRequest", currentTime, false, "not expired");
72+
if (!isApiAccessExpired(hash)) {
8073
return false;
8174
}
8275
if (counter.tryAcquire()) {
8376
log.debug("API security sampling is required for this request (presampled)");
8477
ctx.setKeepOpenForApiSecurityPostProcessing(true);
85-
logSamplingDecision("preSampleRequest", currentTime, true, "acquired semaphore");
78+
if (!Config.get().isApmTracingEnabled()) {
79+
// Update immediately to prevent concurrent requests from seeing the same expired state
80+
return updateApiAccessIfExpired(hash);
81+
}
8682
return true;
8783
}
88-
logSamplingDecision("preSampleRequest", currentTime, false, "semaphore full");
8984
return false;
9085
}
9186

@@ -100,11 +95,7 @@ public boolean sampleRequest(AppSecRequestContext ctx) {
10095
// This should never happen, it should have been short-circuited before.
10196
return false;
10297
}
103-
long currentTime = timeSource.getCurrentTimeMillis();
104-
boolean decision = updateApiAccessIfExpired(hash);
105-
logSamplingDecision(
106-
"sampleRequest", currentTime, decision, decision ? "sampled" : "already sampled");
107-
return decision;
98+
return Config.get().isApmTracingEnabled() ? updateApiAccessIfExpired(hash) : true;
10899
}
109100

110101
@Override
@@ -181,27 +172,4 @@ private long computeApiHash(final String route, final String method, final int s
181172
result = 31 * result + statusCode;
182173
return result;
183174
}
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-
}
207175
}

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,30 @@ 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-
3834
final RequestContext ctx_ = span.getRequestContext();
3935
if (ctx_ == null) {
40-
logProcessingDecision(timestamp, traceId, spanId, false, "no request context", "start");
4136
return;
4237
}
4338
final AppSecRequestContext ctx = ctx_.getData(RequestContextSlot.APPSEC);
4439
if (ctx == null) {
45-
logProcessingDecision(timestamp, traceId, spanId, false, "no appsec context", "start");
4640
return;
4741
}
4842

4943
if (!ctx.isKeepOpenForApiSecurityPostProcessing()) {
50-
logProcessingDecision(
51-
timestamp, traceId, spanId, false, "not marked for post-processing", "start");
5244
return;
5345
}
5446

5547
try {
5648
if (timeoutCheck.getAsBoolean()) {
5749
log.debug("Timeout detected, skipping API security post-processing");
58-
logProcessingDecision(
59-
timestamp, traceId, spanId, false, "timeout detected", "pre-sampling");
6050
return;
6151
}
6252
if (!sampler.sampleRequest(ctx)) {
6353
log.debug("Request not sampled, skipping API security post-processing");
64-
logProcessingDecision(
65-
timestamp, traceId, spanId, false, "request not sampled", "post-sampling");
6654
return;
6755
}
6856
log.debug("Request sampled, processing API security post-processing");
69-
logProcessingDecision(
70-
timestamp, traceId, spanId, true, "sampled, extracting schemas", "extracting");
7157
extractSchemas(ctx, ctx_.getTraceSegment());
72-
logProcessingDecision(timestamp, traceId, spanId, true, "extraction completed", "completed");
7358
} finally {
7459
ctx.setKeepOpenForApiSecurityPostProcessing(false);
7560
try {
@@ -82,7 +67,6 @@ public void process(@Nonnull AgentSpan span, @Nonnull BooleanSupplier timeoutChe
8267
log.debug("Error closing AppSecRequestContext", e);
8368
}
8469
sampler.releaseOne();
85-
logProcessingDecision(timestamp, traceId, spanId, false, "cleanup completed", "cleanup");
8670
}
8771
}
8872

@@ -105,21 +89,4 @@ private void extractSchemas(final AppSecRequestContext ctx, final TraceSegment t
10589
log.debug("Subscriber info expired", e);
10690
}
10791
}
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-
}
12592
}

0 commit comments

Comments
 (0)