Skip to content

Commit 9329e98

Browse files
authored
optimize method probe without condition (#9834)
In case of a method probe without condition we can move the sampling is currently done in LogProbe::evaluate to LogProbe::isReadToCapture. This way if the sample fails we return false on isReadyTocapture and no CapturedContext is created and nothing captured. If we sample the execution the CapturedContext will be created we capture the context and serialize the result, but we are sure that we are not doing for nothing. We introduce also a differentiation for single probe/multiple probe in calling evaluate that is propagated to probe implementation. it helps to differentiate the behavior based on the instrumentation
1 parent dfaef99 commit 9329e98

File tree

11 files changed

+70
-31
lines changed

11 files changed

+70
-31
lines changed

dd-java-agent/agent-debugger/debugger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/CapturedContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ public Status evaluate(
297297
ProbeImplementation probeImplementation,
298298
String thisClassName,
299299
long startTimestamp,
300-
MethodLocation methodLocation) {
300+
MethodLocation methodLocation,
301+
boolean singleProbe) {
301302
Status status =
302303
statusByProbeId.computeIfAbsent(
303304
probeImplementation.getProbeId().getEncodedId(),
@@ -311,7 +312,7 @@ public Status evaluate(
311312
boolean shouldEvaluate =
312313
MethodLocation.isSame(methodLocation, probeImplementation.getEvaluateAt());
313314
if (shouldEvaluate) {
314-
probeImplementation.evaluate(this, status, methodLocation);
315+
probeImplementation.evaluate(this, status, methodLocation, singleProbe);
315316
}
316317
return status;
317318
}

dd-java-agent/agent-debugger/debugger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/DebuggerContext.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ public static void evalContext(
311311
}
312312
CapturedContext.Status status =
313313
context.evaluate(
314-
probeImplementation, callingClass.getTypeName(), startTimestamp, methodLocation);
314+
probeImplementation,
315+
callingClass.getTypeName(),
316+
startTimestamp,
317+
methodLocation,
318+
false);
315319
needFreeze |= status.shouldFreezeContext();
316320
}
317321
// only freeze the context when we have at lest one snapshot probe, and we should send
@@ -343,7 +347,11 @@ public static void evalContext(
343347
}
344348
CapturedContext.Status status =
345349
context.evaluate(
346-
probeImplementation, callingClass.getTypeName(), startTimestamp, methodLocation);
350+
probeImplementation,
351+
callingClass.getTypeName(),
352+
startTimestamp,
353+
methodLocation,
354+
true);
347355
boolean needFreeze = status.shouldFreezeContext();
348356
// only freeze the context when we have at lest one snapshot probe, and we should send
349357
// snapshot
@@ -371,7 +379,7 @@ public static void evalContextAndCommit(
371379
continue;
372380
}
373381
context.evaluate(
374-
probeImplementation, callingClass.getTypeName(), -1, MethodLocation.DEFAULT);
382+
probeImplementation, callingClass.getTypeName(), -1, MethodLocation.DEFAULT, false);
375383
probeImplementations.add(probeImplementation);
376384
}
377385
for (ProbeImplementation probeImplementation : probeImplementations) {
@@ -395,7 +403,8 @@ public static void evalContextAndCommit(
395403
if (probeImplementation == null) {
396404
return;
397405
}
398-
context.evaluate(probeImplementation, callingClass.getTypeName(), -1, MethodLocation.DEFAULT);
406+
context.evaluate(
407+
probeImplementation, callingClass.getTypeName(), -1, MethodLocation.DEFAULT, true);
399408
probeImplementation.commit(context, line);
400409
} catch (Exception ex) {
401410
LOGGER.debug("Error in evalContextAndCommit: ", ex);

dd-java-agent/agent-debugger/debugger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/ProbeImplementation.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public interface ProbeImplementation {
1717
String getStrTags();
1818

1919
void evaluate(
20-
CapturedContext context, CapturedContext.Status status, MethodLocation methodLocation);
20+
CapturedContext context,
21+
CapturedContext.Status status,
22+
MethodLocation methodLocation,
23+
boolean singleProbe);
2124

2225
void commit(
2326
CapturedContext entryContext,
@@ -82,7 +85,10 @@ public String getStrTags() {
8285

8386
@Override
8487
public void evaluate(
85-
CapturedContext context, CapturedContext.Status status, MethodLocation methodLocation) {}
88+
CapturedContext context,
89+
CapturedContext.Status status,
90+
MethodLocation methodLocation,
91+
boolean singleProbe) {}
8692

8793
@Override
8894
public void commit(

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/ExceptionProbe.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ public CapturedContext.Status createStatus() {
7171

7272
@Override
7373
public void evaluate(
74-
CapturedContext context, CapturedContext.Status status, MethodLocation methodLocation) {
74+
CapturedContext context,
75+
CapturedContext.Status status,
76+
MethodLocation methodLocation,
77+
boolean singleProbe) {
7578
ExceptionProbeStatus exceptionStatus;
7679
if (status instanceof ExceptionProbeStatus) {
7780
exceptionStatus = (ExceptionProbeStatus) status;
@@ -108,7 +111,7 @@ public void evaluate(
108111
exceptionStatus.setForceSampling(true);
109112
}
110113
exceptionStatus.setCapture(true);
111-
super.evaluate(context, status, methodLocation);
114+
super.evaluate(context, status, methodLocation, singleProbe);
112115
}
113116
}
114117

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/LogProbe.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ public InstrumentationResult.Status instrument(
485485

486486
@Override
487487
public boolean isReadyToCapture() {
488-
if (isLineProbe() && !hasCondition()) {
488+
if (!hasCondition()) {
489489
// we are sampling here to avoid creating CapturedContext when the sampling result is negative
490490
return ProbeRateLimiter.tryProbe(id);
491491
}
@@ -494,20 +494,25 @@ public boolean isReadyToCapture() {
494494

495495
@Override
496496
public void evaluate(
497-
CapturedContext context, CapturedContext.Status status, MethodLocation methodLocation) {
497+
CapturedContext context,
498+
CapturedContext.Status status,
499+
MethodLocation methodLocation,
500+
boolean singleProbe) {
498501
if (!(status instanceof LogStatus)) {
499502
throw new IllegalStateException("Invalid status: " + status.getClass());
500503
}
501504
LogStatus logStatus = (LogStatus) status;
502-
if (isLineProbe() && !hasCondition()) {
503-
// sampling was already done in isReadToCapture so we assume that if we are executing the
504-
// current method it means the status should be sampled
505-
if (!logStatus.getDebugSessionStatus().isDisabled()) {
506-
logStatus.setSampled(true);
505+
if (!hasCondition()) {
506+
if (singleProbe) {
507+
// sampling was already done in isReadToCapture so we assume that if we are executing the
508+
// current method it means the status should be sampled
509+
if (!logStatus.getDebugSessionStatus().isDisabled()) {
510+
logStatus.setSampled(true);
511+
}
512+
} else {
513+
// sample when no condition associated
514+
sample(logStatus, methodLocation);
507515
}
508-
} else if (!hasCondition()) {
509-
// sample when no condition associated
510-
sample(logStatus, methodLocation);
511516
}
512517
logStatus.setCondition(evaluateCondition(context, logStatus));
513518
CapturedContext.CapturedThrowable throwable = context.getCapturedThrowable();

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/ProbeDefinition.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ public ProbeLocation getLocation() {
137137

138138
@Override
139139
public void evaluate(
140-
CapturedContext context, CapturedContext.Status status, MethodLocation methodLocation) {}
140+
CapturedContext context,
141+
CapturedContext.Status status,
142+
MethodLocation methodLocation,
143+
boolean singleProbe) {}
141144

142145
@Override
143146
public void commit(

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/SpanDecorationProbe.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ public InstrumentationResult.Status instrument(
143143

144144
@Override
145145
public void evaluate(
146-
CapturedContext context, CapturedContext.Status status, MethodLocation methodLocation) {
146+
CapturedContext context,
147+
CapturedContext.Status status,
148+
MethodLocation methodLocation,
149+
boolean singleProbe) {
147150
for (Decoration decoration : decorations) {
148151
if (decoration.when != null) {
149152
try {

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/TriggerProbe.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ public TriggerProbe setProbeCondition(ProbeCondition probeCondition) {
9696

9797
@Override
9898
public void evaluate(
99-
CapturedContext context, CapturedContext.Status status, MethodLocation location) {
99+
CapturedContext context,
100+
CapturedContext.Status status,
101+
MethodLocation location,
102+
boolean singleProbe) {
100103

101104
Sampling sampling = getSampling();
102105
if (sampling == null || !sampling.inCoolDown()) {

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/SnapshotSerializationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public void roundTripCapturedValue() throws IOException, URISyntaxException {
127127
new ProbeImplementation.NoopProbeImplementation(PROBE_ID, PROBE_LOCATION),
128128
String.class.getTypeName(),
129129
-1,
130-
MethodLocation.EXIT);
130+
MethodLocation.EXIT,
131+
false);
131132
snapshot.setExit(context);
132133
String buffer = adapter.toJson(snapshot);
133134
Snapshot deserializedSnapshot = adapter.fromJson(buffer);

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/exception/DefaultExceptionDebuggerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ private void evalAndCommitProbe(RuntimeException exception, ExceptionProbe excep
367367
exceptionProbe.buildLocation(null);
368368
CapturedContext capturedContext = new CapturedContext();
369369
capturedContext.addThrowable(exception);
370-
capturedContext.evaluate(exceptionProbe, "", System.currentTimeMillis(), MethodLocation.EXIT);
370+
capturedContext.evaluate(
371+
exceptionProbe, "", System.currentTimeMillis(), MethodLocation.EXIT, false);
371372
exceptionProbe.commit(CapturedContext.EMPTY_CAPTURING_CONTEXT, capturedContext, emptyList());
372373
}
373374

0 commit comments

Comments
 (0)