Skip to content

Commit fe75494

Browse files
committed
change logs
1 parent 8f0abf6 commit fe75494

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,49 @@ public ApiSecuritySamplerImpl(
5454
public boolean preSampleRequest(final @Nonnull AppSecRequestContext ctx) {
5555
final String route = ctx.getRoute();
5656
if (route == null) {
57+
log.info("[APPSEC-57815] preSampleRequest returning false - route is null");
5758
return false;
5859
}
5960
final String method = ctx.getMethod();
6061
if (method == null) {
62+
log.info("[APPSEC-57815] preSampleRequest returning false - method is null, route={}", route);
6163
return false;
6264
}
6365
final int statusCode = ctx.getResponseStatus();
6466
if (statusCode <= 0) {
67+
log.info(
68+
"[APPSEC-57815] preSampleRequest returning false - invalid statusCode={}, route={}, method={}",
69+
statusCode,
70+
route,
71+
method);
6572
return false;
6673
}
6774
long hash = computeApiHash(route, method, statusCode);
6875
ctx.setApiSecurityEndpointHash(hash);
69-
if (!isApiAccessExpired(hash)) {
76+
77+
boolean isExpired = isApiAccessExpired(hash);
78+
log.info(
79+
"[APPSEC-57815] preSampleRequest checking expiration - route={}, method={}, statusCode={}, hash={}, isExpired={}, expirationTimeMs={}",
80+
route,
81+
method,
82+
statusCode,
83+
hash,
84+
isExpired,
85+
expirationTimeInMs);
86+
87+
if (!isExpired) {
7088
return false;
7189
}
72-
if (counter.tryAcquire()) {
90+
91+
int availablePermits = counter.availablePermits();
92+
boolean acquired = counter.tryAcquire();
93+
log.info(
94+
"[APPSEC-57815] preSampleRequest semaphore check - availablePermits={}, acquired={}, route={}",
95+
availablePermits,
96+
acquired,
97+
route);
98+
99+
if (acquired) {
73100
log.debug("API security sampling is required for this request (presampled)");
74101
ctx.setKeepOpenForApiSecurityPostProcessing(true);
75102
return true;

dd-java-agent/appsec/src/main/java/com/datadog/appsec/gateway/GatewayBridge.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -861,28 +861,17 @@ private NoopFlow onRequestEnded(RequestContext ctx_, IGSpanInfo spanInfo) {
861861
if (traceSeg != null) {
862862
// Set API Security sampling tags if needed
863863
if (sampledForApiSec && !apmTracingEnabled) {
864-
log.info(
865-
"[APPSEC-57815] Setting ASM_KEEP=true (API Security sampled, APM tracing disabled)");
866864
traceSeg.setTagTop(Tags.ASM_KEEP, true);
867865
// Must set _dd.p.ts locally so TraceCollector respects force-keep in standalone mode
868866
// (TraceCollector.java lines 67-74 ignore force-keep without _dd.p.ts when APM disabled)
869867
traceSeg.setTagTop(Tags.PROPAGATED_TRACE_SOURCE, ProductTraceSource.ASM);
870-
// Verify the tag was set
871-
Object asmKeepAfterSet = traceSeg.getTagTop(Tags.ASM_KEEP);
872-
Object propagatedTsAfterSet = traceSeg.getTagTop(Tags.PROPAGATED_TRACE_SOURCE);
873-
log.info(
874-
"[APPSEC-57815] After setTagTop - ASM_KEEP: {}, _dd.p.ts: {}",
875-
asmKeepAfterSet,
876-
propagatedTsAfterSet);
877868
}
878869

879870
traceSeg.setTagTop("_dd.appsec.enabled", 1);
880871
traceSeg.setTagTop("_dd.runtime_family", "jvm");
881872

882873
Collection<AppSecEvent> collectedEvents = ctx.transferCollectedEvents();
883874

884-
log.info("[APPSEC-57815] Collected {} AppSec events", collectedEvents.size());
885-
886875
for (TraceSegmentPostProcessor pp : this.traceSegmentPostProcessors) {
887876
pp.processTraceSegment(traceSeg, ctx, collectedEvents);
888877
}
@@ -895,11 +884,8 @@ private NoopFlow onRequestEnded(RequestContext ctx_, IGSpanInfo spanInfo) {
895884
// If detected any events - mark span at appsec.event
896885
if (!collectedEvents.isEmpty()) {
897886
boolean manuallyKept = ctx.isManuallyKept();
898-
log.info("[APPSEC-57815] AppSec events detected - manuallyKept={}", manuallyKept);
899887
if (manuallyKept) {
900888
// Set asm keep in case that root span was not available when events are detected
901-
log.info(
902-
"[APPSEC-57815] Setting ASM_KEEP=true and _dd.p.ts=ASM (AppSec events + manually kept)");
903889
traceSeg.setTagTop(Tags.ASM_KEEP, true);
904890
traceSeg.setTagTop(Tags.PROPAGATED_TRACE_SOURCE, ProductTraceSource.ASM);
905891
}
@@ -963,29 +949,32 @@ private NoopFlow onRequestEnded(RequestContext ctx_, IGSpanInfo spanInfo) {
963949
);
964950
}
965951

966-
// Log final state of propagation tags from TraceSegment (not from spanInfo.getTags() which is
967-
// immutable)
968-
Object finalPropagatedTs = traceSeg.getTagTop(Tags.PROPAGATED_TRACE_SOURCE);
969-
Object finalAsmKeep = traceSeg.getTagTop(Tags.ASM_KEEP);
970-
log.info(
971-
"[APPSEC-57815] Request ended - final state from TraceSegment: _dd.p.ts={}, _dd.appsec.keep={}",
972-
finalPropagatedTs,
973-
finalAsmKeep);
974-
975952
ctx.close();
976953
return NoopFlow.INSTANCE;
977954
}
978955

979956
private boolean maybeSampleForApiSecurity(
980957
AppSecRequestContext ctx, IGSpanInfo spanInfo, Map<String, Object> tags) {
981958
log.debug("Checking API Security for end of request handler on span: {}", spanInfo.getSpanId());
959+
982960
// API Security sampling requires http.route tag.
983961
final Object route = tags.get(Tags.HTTP_ROUTE);
962+
final String existingRoute = ctx.getRoute();
963+
964+
log.info(
965+
"[APPSEC-57815] maybeSampleForApiSecurity called - spanId={}, route from tags={}, existingRoute={}, ctx.hashCode={}",
966+
spanInfo.getSpanId(),
967+
route,
968+
existingRoute,
969+
System.identityHashCode(ctx));
970+
984971
if (route != null) {
985972
ctx.setRoute(route.toString());
986973
}
974+
987975
ApiSecuritySampler requestSampler = requestSamplerSupplier.get();
988976
boolean sampled = requestSampler.preSampleRequest(ctx);
977+
989978
log.info(
990979
"[APPSEC-57815] API Security sampling decision - route={}, sampled={}", route, sampled);
991980
return sampled;

0 commit comments

Comments
 (0)