Skip to content

Commit ca975c2

Browse files
authored
fix capture limit for capture expressions (#11042)
fix capture limit for capture expressions if we set a capture at probe level and no capture for capture expressions, each capture expression should apply the the capture limits defined at probe level. Co-authored-by: jean-philippe.bempel <jean-philippe.bempel@datadoghq.com>
1 parent 2365c12 commit ca975c2

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,6 @@ private void processCaptureExpressions(CapturedContext context, LogStatus logSta
730730
captureExpression.getName(), Object.class.getTypeName(), null));
731731
} else {
732732
if (captureExpression.capture != null) {
733-
Value.toCapturedSnapshot(captureExpression.getName(), result);
734733
context.addCaptureExpression(
735734
Value.toCapturedSnapshot(
736735
captureExpression.getName(),
@@ -740,8 +739,20 @@ private void processCaptureExpressions(CapturedContext context, LogStatus logSta
740739
captureExpression.capture.maxLength,
741740
captureExpression.capture.maxFieldCount));
742741
} else {
743-
context.addCaptureExpression(
744-
Value.toCapturedSnapshot(captureExpression.getName(), result));
742+
// inherit from probe capture field because no specific capture
743+
if (capture != null) {
744+
context.addCaptureExpression(
745+
Value.toCapturedSnapshot(
746+
captureExpression.getName(),
747+
result,
748+
capture.maxReferenceDepth,
749+
capture.maxCollectionSize,
750+
capture.maxLength,
751+
capture.maxFieldCount));
752+
} else {
753+
context.addCaptureExpression(
754+
Value.toCapturedSnapshot(captureExpression.getName(), result));
755+
}
745756
}
746757
}
747758
} catch (EvaluationException ex) {

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,6 +3028,47 @@ public void captureExpressionsWithCaptureLimits() throws IOException, URISyntaxE
30283028
assertEquals("depth", fldValue.getNotCapturedReason());
30293029
}
30303030

3031+
@Test
3032+
public void captureExpressionsWithInheritedCaptureLimits()
3033+
throws IOException, URISyntaxException {
3034+
final String CLASS_NAME = "CapturedSnapshot08";
3035+
LogProbe probe =
3036+
createProbeBuilder(PROBE_ID, CLASS_NAME, "doit", null)
3037+
.evaluateAt(MethodLocation.EXIT)
3038+
.captureSnapshot(false)
3039+
.capture(new LogProbe.Capture(1, 10, 3, 1))
3040+
.captureExpressions(
3041+
Arrays.asList(
3042+
new LogProbe.CaptureExpression(
3043+
"typed_fld_fld_msg",
3044+
new ValueScript(
3045+
DSL.getMember(
3046+
DSL.getMember(DSL.getMember(DSL.ref("typed"), "fld"), "fld"),
3047+
"msg"),
3048+
"typed.fld.fld.msg"),
3049+
null),
3050+
new LogProbe.CaptureExpression(
3051+
"typed", new ValueScript(DSL.ref("typed"), "typed"), null)))
3052+
.build();
3053+
TestSnapshotListener listener = installProbes(probe);
3054+
Class<?> testClass = compileAndLoadClass(CLASS_NAME);
3055+
int result = Reflect.onClass(testClass).call("main", "1").get();
3056+
assertEquals(3, result);
3057+
Snapshot snapshot = assertOneSnapshot(listener);
3058+
CapturedContext.CapturedValue msgValue =
3059+
deserializeCapturedValue(
3060+
snapshot.getCaptures().getReturn().getCaptureExpressions().get("typed_fld_fld_msg"));
3061+
assertEquals("hel", msgValue.getValue());
3062+
assertEquals("truncated", msgValue.getNotCapturedReason());
3063+
CapturedContext.CapturedValue typedValue =
3064+
deserializeCapturedValue(
3065+
snapshot.getCaptures().getReturn().getCaptureExpressions().get("typed"));
3066+
Map<String, CapturedContext.CapturedValue> fields =
3067+
(Map<String, CapturedContext.CapturedValue>) typedValue.getValue();
3068+
CapturedContext.CapturedValue fldValue = fields.get("fld");
3069+
assertEquals("depth", fldValue.getNotCapturedReason());
3070+
}
3071+
30313072
@Test
30323073
public void methodParametersAttribute() throws Exception {
30333074
final String CLASS_NAME = "CapturedSnapshot01";

0 commit comments

Comments
 (0)