Skip to content

Commit 714c4f8

Browse files
committed
Add correlation Ids for dynamic logs
Adding trace id and span id for both snapshot and log probes
1 parent 4053b2a commit 714c4f8

4 files changed

Lines changed: 37 additions & 9 deletions

File tree

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/agent/JsonSnapshotSerializer.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ private void handleDuration(Snapshot snapshot, IntakeRequest request) {
4545
}
4646

4747
private void handleCorrelationFields(Snapshot snapshot, IntakeRequest request) {
48+
request.traceId = snapshot.getTraceId();
49+
request.spanId = snapshot.getSpanId();
4850
CapturedContext entry = snapshot.getCaptures().getEntry();
4951
if (entry != null) {
50-
addTraceSpanId(entry, request);
5152
removeTraceSpanId(entry);
5253
}
5354
if (snapshot.getCaptures().getLines() != null) {
5455
for (CapturedContext context : snapshot.getCaptures().getLines().values()) {
55-
addTraceSpanId(context, request);
5656
removeTraceSpanId(context);
5757
}
5858
}
@@ -71,11 +71,6 @@ private void removeTraceSpanId(CapturedContext context) {
7171
fields.remove(DD_SPAN_ID);
7272
}
7373

74-
private void addTraceSpanId(CapturedContext context, IntakeRequest request) {
75-
request.traceId = context.getTraceId();
76-
request.spanId = context.getSpanId();
77-
}
78-
7974
public static class IntakeRequest {
8075
private final String service;
8176
private final DebuggerIntakeRequestData debugger;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ public void commit(
420420
return;
421421
}
422422
}
423+
snapshot.setTraceId(entryContext.getTraceId());
424+
snapshot.setSpanId(entryContext.getSpanId());
423425
if (isCaptureSnapshot()) {
424426
snapshot.setEntry(entryContext);
425427
snapshot.setExit(exitContext);
@@ -494,6 +496,8 @@ public void commit(CapturedContext lineContext, int line) {
494496
return;
495497
}
496498
}
499+
snapshot.setTraceId(lineContext.getTraceId());
500+
snapshot.setSpanId(lineContext.getSpanId());
497501
if (isCaptureSnapshot()) {
498502
snapshot.addLine(lineContext, line);
499503
}

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/sink/Snapshot.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public class Snapshot {
2626
private final ProbeImplementation probe;
2727
private final String language;
2828
private final transient CapturedThread thread;
29-
private String traceId; // trace_id
30-
private String spanId; // span_id
29+
private transient String traceId;
30+
private transient String spanId;
3131
private List<EvaluationError> evaluationErrors;
3232
private transient String message;
3333
private final transient int maxDepth;
@@ -176,6 +176,14 @@ public void recordStackTrace(int offset) {
176176
}
177177
}
178178

179+
public void setTraceId(String traceId) {
180+
this.traceId = traceId;
181+
}
182+
183+
public void setSpanId(String spanId) {
184+
this.spanId = spanId;
185+
}
186+
179187
public enum Kind {
180188
ENTER,
181189
RETURN,

dd-smoke-tests/debugger-integration-tests/src/test/java/datadog/smoketest/TracerDebuggerIntegrationTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ void testTracer() throws Exception {
6262
logHasErrors(logFilePath, it -> it.contains("TypePool$Resolution$NoSuchTypeException")));
6363
}
6464

65+
@Test
66+
@DisplayName("testTracerDynamicLog")
67+
void testTracerDynamicLog() throws Exception {
68+
LogProbe logProbe =
69+
LogProbe.builder()
70+
.probeId(PROBE_ID)
71+
.where(
72+
"org.springframework.web.servlet.DispatcherServlet",
73+
"doService",
74+
"(HttpServletRequest, HttpServletResponse)")
75+
.captureSnapshot(false)
76+
.build();
77+
JsonSnapshotSerializer.IntakeRequest request = doTestTracer(logProbe);
78+
Snapshot snapshot = request.getDebugger().getSnapshot();
79+
assertEquals("123356536", snapshot.getProbe().getId());
80+
assertTrue(Pattern.matches("[0-9a-f]+", request.getTraceId()));
81+
assertTrue(Pattern.matches("\\d+", request.getSpanId()));
82+
assertFalse(
83+
logHasErrors(logFilePath, it -> it.contains("TypePool$Resolution$NoSuchTypeException")));
84+
}
85+
6586
@Test
6687
@DisplayName("testTracerSameMethod")
6788
void testTracerSameMethod() throws Exception {

0 commit comments

Comments
 (0)