Skip to content

Commit 710964d

Browse files
authored
Introduce id-based line probe source matching (#8275)
line probe in unit tests are created by searching probe id (UUID) inside the targeted source file. uuid are added as comment on the line we want to put a line probe. Add helper method to search a uuid inside the targeted source file and returns the line number used to create the line probe
1 parent 71309b3 commit 710964d

25 files changed

+382
-267
lines changed

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

Lines changed: 93 additions & 58 deletions
Large diffs are not rendered by default.

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

Lines changed: 81 additions & 51 deletions
Large diffs are not rendered by default.

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

Lines changed: 119 additions & 85 deletions
Large diffs are not rendered by default.

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

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.mockito.Mockito.verify;
2121
import static org.mockito.Mockito.when;
2222
import static utils.InstrumentationTestHelper.compileAndLoadClass;
23+
import static utils.InstrumentationTestHelper.getLineForLineProbe;
2324

2425
import com.datadog.debugger.el.DSL;
2526
import com.datadog.debugger.el.ProbeCondition;
@@ -60,6 +61,8 @@ public class SpanDecorationProbeInstrumentationTest extends ProbeInstrumentation
6061
private static final ProbeId PROBE_ID2 = new ProbeId("beae1807-f3b0-4ea8-a74f-826790c5e6f7", 0);
6162
private static final ProbeId PROBE_ID3 = new ProbeId("beae1807-f3b0-4ea8-a74f-826790c5e6f8", 0);
6263
private static final ProbeId PROBE_ID4 = new ProbeId("beae1807-f3b0-4ea8-a74f-826790c5e6f9", 0);
64+
private static final ProbeId LINE_PROBE_ID1 =
65+
new ProbeId("beae1817-f3b0-4ea8-a74f-000000000001", 0);
6366

6467
private TestTraceInterceptor traceInterceptor = new TestTraceInterceptor();
6568

@@ -259,14 +262,16 @@ public void methodActiveSpanSynthException() throws IOException, URISyntaxExcept
259262
public void lineActiveSpanSimpleTag() throws IOException, URISyntaxException {
260263
final String CLASS_NAME = "com.datadog.debugger.CapturedSnapshot20";
261264
SpanDecorationProbe.Decoration decoration = createDecoration("tag1", "{arg}");
262-
installSingleSpanDecoration(CLASS_NAME, ACTIVE, decoration, "CapturedSnapshot20.java", 47);
265+
int line = getLineForLineProbe(CLASS_NAME, LINE_PROBE_ID1);
266+
installSingleSpanDecoration(
267+
LINE_PROBE_ID1, CLASS_NAME, ACTIVE, decoration, "CapturedSnapshot20.java", line);
263268
Class<?> testClass = compileAndLoadClass(CLASS_NAME);
264269
int result = Reflect.on(testClass).call("main", "1").get();
265270
assertEquals(84, result);
266271
MutableSpan span = traceInterceptor.getFirstSpan();
267272
assertEquals("1", span.getTags().get("tag1"));
268-
assertEquals(PROBE_ID.getId(), span.getTags().get("_dd.di.tag1.probe_id"));
269-
verify(probeStatusSink).addEmitting(ArgumentMatchers.eq(PROBE_ID));
273+
assertEquals(LINE_PROBE_ID1.getId(), span.getTags().get("_dd.di.tag1.probe_id"));
274+
verify(probeStatusSink).addEmitting(ArgumentMatchers.eq(LINE_PROBE_ID1));
270275
}
271276

272277
@Test
@@ -275,8 +280,14 @@ public void lineRootSpanTagList() throws IOException, URISyntaxException {
275280
SpanDecorationProbe.Decoration deco1 = createDecoration("tag1", "{arg}");
276281
SpanDecorationProbe.Decoration deco2 = createDecoration("tag2", "{this.intField}");
277282
SpanDecorationProbe.Decoration deco3 = createDecoration("tag3", "{strField}");
283+
int line = getLineForLineProbe(CLASS_NAME, LINE_PROBE_ID1);
278284
installSingleSpanDecoration(
279-
CLASS_NAME, ROOT, asList(deco1, deco2, deco3), "CapturedSnapshot21.java", 67);
285+
LINE_PROBE_ID1,
286+
CLASS_NAME,
287+
ROOT,
288+
asList(deco1, deco2, deco3),
289+
"CapturedSnapshot21.java",
290+
line);
280291
Class<?> testClass = compileAndLoadClass(CLASS_NAME);
281292
int result = Reflect.on(testClass).call("main", "1").get();
282293
assertEquals(45, result);
@@ -292,7 +303,9 @@ public void lineActiveSpanCondition() throws IOException, URISyntaxException {
292303
final String CLASS_NAME = "com.datadog.debugger.CapturedSnapshot20";
293304
SpanDecorationProbe.Decoration decoration =
294305
createDecoration(eq(ref("arg"), value("5")), "arg == '5'", "tag1", "{arg}");
295-
installSingleSpanDecoration(CLASS_NAME, ACTIVE, decoration, "CapturedSnapshot20.java", 47);
306+
int line = getLineForLineProbe(CLASS_NAME, LINE_PROBE_ID1);
307+
installSingleSpanDecoration(
308+
LINE_PROBE_ID1, CLASS_NAME, ACTIVE, decoration, "CapturedSnapshot20.java", line);
296309
Class<?> testClass = compileAndLoadClass(CLASS_NAME);
297310
for (int i = 0; i < 10; i++) {
298311
int result = Reflect.on(testClass).call("main", String.valueOf(i)).get();
@@ -308,7 +321,9 @@ public void lineActiveSpanInvalidCondition() throws IOException, URISyntaxExcept
308321
final String CLASS_NAME = "com.datadog.debugger.CapturedSnapshot20";
309322
SpanDecorationProbe.Decoration decoration =
310323
createDecoration(eq(ref("noarg"), value("5")), "arg == '5'", "tag1", "{arg}");
311-
installSingleSpanDecoration(CLASS_NAME, ACTIVE, decoration, "CapturedSnapshot20.java", 47);
324+
int line = getLineForLineProbe(CLASS_NAME, LINE_PROBE_ID1);
325+
installSingleSpanDecoration(
326+
LINE_PROBE_ID1, CLASS_NAME, ACTIVE, decoration, "CapturedSnapshot20.java", line);
312327
Class<?> testClass = compileAndLoadClass(CLASS_NAME);
313328
int result = Reflect.on(testClass).call("main", "5").get();
314329
assertEquals(84, result);
@@ -338,24 +353,12 @@ public void mixedWithLogProbes() throws IOException, URISyntaxException {
338353
SpanDecorationProbe.Decoration decoration2 = createDecoration("tag2", "{arg}");
339354
SpanDecorationProbe spanDecoProbe1 =
340355
createProbeBuilder(
341-
PROBE_ID1,
342-
ACTIVE,
343-
singletonList(decoration1),
344-
CLASS_NAME,
345-
"process",
346-
null,
347-
(String[]) null)
356+
PROBE_ID1, ACTIVE, singletonList(decoration1), CLASS_NAME, "process", null)
348357
.evaluateAt(MethodLocation.EXIT)
349358
.build();
350359
SpanDecorationProbe spanDecoProbe2 =
351360
createProbeBuilder(
352-
PROBE_ID2,
353-
ACTIVE,
354-
singletonList(decoration2),
355-
CLASS_NAME,
356-
"process",
357-
null,
358-
(String[]) null)
361+
PROBE_ID2, ACTIVE, singletonList(decoration2), CLASS_NAME, "process", null)
359362
.evaluateAt(MethodLocation.ENTRY)
360363
.build();
361364
LogProbe logProbe1 =
@@ -403,24 +406,12 @@ public void mixedEntryExit() throws IOException, URISyntaxException {
403406
SpanDecorationProbe.Decoration decoration2 = createDecoration("tag2", "{arg}");
404407
SpanDecorationProbe spanDecoProbe1 =
405408
createProbeBuilder(
406-
PROBE_ID1,
407-
ACTIVE,
408-
singletonList(decoration1),
409-
CLASS_NAME,
410-
"process",
411-
null,
412-
(String[]) null)
409+
PROBE_ID1, ACTIVE, singletonList(decoration1), CLASS_NAME, "process", null)
413410
.evaluateAt(MethodLocation.EXIT)
414411
.build();
415412
SpanDecorationProbe spanDecoProbe2 =
416413
createProbeBuilder(
417-
PROBE_ID2,
418-
ACTIVE,
419-
singletonList(decoration2),
420-
CLASS_NAME,
421-
"process",
422-
null,
423-
(String[]) null)
414+
PROBE_ID2, ACTIVE, singletonList(decoration2), CLASS_NAME, "process", null)
424415
.evaluateAt(MethodLocation.ENTRY)
425416
.build();
426417
Configuration configuration =
@@ -624,12 +615,14 @@ private void installSingleSpanDecoration(
624615
}
625616

626617
private void installSingleSpanDecoration(
618+
ProbeId probeId,
627619
String typeName,
628620
SpanDecorationProbe.TargetSpan targetSpan,
629621
SpanDecorationProbe.Decoration decoration,
630622
String sourceFile,
631623
int line) {
632-
installSingleSpanDecoration(typeName, targetSpan, asList(decoration), sourceFile, line);
624+
installSingleSpanDecoration(
625+
probeId, typeName, targetSpan, asList(decoration), sourceFile, line);
633626
}
634627

635628
private void installSingleSpanDecoration(
@@ -645,12 +638,13 @@ private void installSingleSpanDecoration(
645638
}
646639

647640
private void installSingleSpanDecoration(
641+
ProbeId probeId,
648642
String typeName,
649643
SpanDecorationProbe.TargetSpan targetSpan,
650644
List<SpanDecorationProbe.Decoration> decorations,
651645
String sourceFile,
652646
int line) {
653-
SpanDecorationProbe probe = createProbe(PROBE_ID, targetSpan, decorations, sourceFile, line);
647+
SpanDecorationProbe probe = createProbe(probeId, targetSpan, decorations, sourceFile, line);
654648
installSpanDecorationProbes(
655649
typeName, Configuration.builder().setService(SERVICE_NAME).add(probe).build());
656650
}
@@ -661,10 +655,8 @@ private static SpanDecorationProbe createProbe(
661655
List<SpanDecorationProbe.Decoration> decorationList,
662656
String typeName,
663657
String methodName,
664-
String signature,
665-
String... lines) {
666-
return createProbeBuilder(
667-
id, targetSpan, decorationList, typeName, methodName, signature, lines)
658+
String signature) {
659+
return createProbeBuilder(id, targetSpan, decorationList, typeName, methodName, signature)
668660
.evaluateAt(MethodLocation.EXIT)
669661
.build();
670662
}
@@ -684,12 +676,11 @@ private static SpanDecorationProbe.Builder createProbeBuilder(
684676
List<SpanDecorationProbe.Decoration> decorationList,
685677
String typeName,
686678
String methodName,
687-
String signature,
688-
String... lines) {
679+
String signature) {
689680
return SpanDecorationProbe.builder()
690681
.language(LANGUAGE)
691682
.probeId(id)
692-
.where(typeName, methodName, signature, lines)
683+
.where(typeName, methodName, signature)
693684
.evaluateAt(MethodLocation.EXIT)
694685
.targetSpan(targetSpan)
695686
.decorate(decorationList);

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/trigger/TriggerProbeTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,8 @@ public static TriggerProbe createTriggerProbe(
173173
String methodName,
174174
String signature,
175175
ProbeCondition probeCondition,
176-
Sampling sampling,
177-
String... lines) {
178-
return new TriggerProbe(id, Where.of(typeName, methodName, signature, lines))
176+
Sampling sampling) {
177+
return new TriggerProbe(id, Where.of(typeName, methodName, signature))
179178
.setSessionId(sessionId)
180179
.setProbeCondition(probeCondition)
181180
.setSampling(sampling);

dd-java-agent/agent-debugger/src/test/java/utils/InstrumentationTestHelper.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package utils;
22

33
import static utils.TestHelper.getFixtureContent;
4+
import static utils.TestHelper.getFixtureLines;
45

56
import com.datadog.debugger.agent.CapturedSnapshotTest;
7+
import datadog.trace.bootstrap.debugger.ProbeId;
68
import java.io.IOException;
79
import java.net.MalformedURLException;
810
import java.net.URISyntaxException;
@@ -11,6 +13,7 @@
1113
import java.nio.file.Files;
1214
import java.nio.file.Paths;
1315
import java.util.HashMap;
16+
import java.util.List;
1417
import java.util.Map;
1518

1619
public class InstrumentationTestHelper {
@@ -66,4 +69,21 @@ public static Class<?> loadClassFromJar(String className, String jarFileName)
6669
new URLClassLoader(new URL[] {new URL("file://" + jarFileName)});
6770
return jarClassLoader.loadClass(className);
6871
}
72+
73+
public static int getLineForLineProbe(String className, ProbeId lineProbeId)
74+
throws IOException, URISyntaxException {
75+
return getLineForLineProbe(className, ".java", lineProbeId);
76+
}
77+
78+
public static int getLineForLineProbe(String className, String ext, ProbeId lineProbeId)
79+
throws IOException, URISyntaxException {
80+
List<String> lines = getFixtureLines("/" + className.replace('.', '/') + ext);
81+
for (int i = 0; i < lines.size(); i++) {
82+
String line = lines.get(i);
83+
if (line.contains("//") && line.contains(lineProbeId.getId())) {
84+
return i + 1;
85+
}
86+
}
87+
return -1;
88+
}
6989
}

dd-java-agent/agent-debugger/src/test/java/utils/TestHelper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
import java.net.URISyntaxException;
55
import java.nio.file.Files;
66
import java.nio.file.Paths;
7+
import java.util.List;
78

89
public class TestHelper {
910
public static String getFixtureContent(String fixture) throws IOException, URISyntaxException {
1011
return new String(Files.readAllBytes(Paths.get(TestHelper.class.getResource(fixture).toURI())));
1112
}
13+
14+
public static List<String> getFixtureLines(String fixture)
15+
throws IOException, URISyntaxException {
16+
return Files.readAllLines(Paths.get(TestHelper.class.getResource(fixture).toURI()));
17+
}
1218
}

dd-java-agent/agent-debugger/src/test/resources/CapturedSnapshot01.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public static int main(String arg) {
55
var1 = 2;
66
return var1;
77
}
8-
var1 = 3;
9-
return var1;
8+
var1 = 3; // beae1817-f3b0-4ea8-a74f-000000000001
9+
return var1; // beae1817-f3b0-4ea8-a74f-000000000002
1010
}
1111
}

dd-java-agent/agent-debugger/src/test/resources/CapturedSnapshot02.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void f() {
4343
int synchronizedBlock(int input) {
4444
int count = input;
4545
synchronized (this) {
46-
for (int i = 0; i < 10; i++) {
46+
for (int i = 0; i < 10; i++) { // beae1817-f3b0-4ea8-a74f-000000000001
4747
count += i;
4848
}
4949
}

dd-java-agent/agent-debugger/src/test/resources/CapturedSnapshot03.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
public class CapturedSnapshot03 {
22

33
int f1(int value) {
4-
return value;
4+
return value; // beae1817-f3b0-4ea8-a74f-000000000001
55
}
66

77
int f2(int value) {

0 commit comments

Comments
 (0)