Skip to content

Commit d8705ab

Browse files
committed
move Trace/SpanId capture at commit time
capture of traceId/SpanId was directly instrumented. it means that every execution of the probe try to fetch from the span the traceId and the spanId with eventual conversion to make it a string. While in the end we may have a condition that is not met or the sampling does not generate a snapshot. which is wasteful. We just move the capture at the commit time when we are sure to send a snapshot with these information.
1 parent 03478c8 commit d8705ab

File tree

3 files changed

+5
-132
lines changed

3 files changed

+5
-132
lines changed

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public class CapturedContext implements ValueReferenceResolver {
3232
private Map<String, CapturedValue> staticFields;
3333
private Limits limits = Limits.DEFAULT;
3434
private String thisClassName;
35-
private String traceId;
36-
private String spanId;
3735
private long duration;
3836
private final Map<String, Status> statusByProbeId = new LinkedHashMap<>();
3937
private Map<String, CapturedValue> watches;
@@ -239,19 +237,6 @@ public void addStaticFields(CapturedValue[] values) {
239237
}
240238
}
241239

242-
public void addTraceId(CapturedValue capturedValue) {
243-
traceId = extractStringId(capturedValue);
244-
}
245-
246-
public void addSpanId(CapturedValue capturedValue) {
247-
spanId = extractStringId(capturedValue);
248-
}
249-
250-
private String extractStringId(CapturedValue capturedValue) {
251-
Object value = capturedValue.getValue();
252-
return value instanceof String ? (String) value : null;
253-
}
254-
255240
public void setLimits(
256241
int maxReferenceDepth, int maxCollectionSize, int maxLength, int maxFieldCount) {
257242
this.limits = new Limits(maxReferenceDepth, maxCollectionSize, maxLength, maxFieldCount);
@@ -285,14 +270,6 @@ public String getThisClassName() {
285270
return thisClassName;
286271
}
287272

288-
public String getTraceId() {
289-
return traceId;
290-
}
291-
292-
public String getSpanId() {
293-
return spanId;
294-
}
295-
296273
/**
297274
* 'Freeze' the context. The contained arguments, locals and fields are converted from their Java
298275
* instance representation into the corresponding string value.

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/instrumentation/CapturedContextInstrumentor.java

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import static com.datadog.debugger.instrumentation.Types.CAPTURED_VALUE;
1515
import static com.datadog.debugger.instrumentation.Types.CAPTURE_THROWABLE_TYPE;
1616
import static com.datadog.debugger.instrumentation.Types.CLASS_TYPE;
17-
import static com.datadog.debugger.instrumentation.Types.CORRELATION_ACCESS_TYPE;
1817
import static com.datadog.debugger.instrumentation.Types.DEBUGGER_CONTEXT_TYPE;
1918
import static com.datadog.debugger.instrumentation.Types.METHOD_LOCATION_TYPE;
2019
import static com.datadog.debugger.instrumentation.Types.OBJECT_TYPE;
@@ -34,7 +33,6 @@
3433
import com.datadog.debugger.sink.Snapshot;
3534
import com.datadog.debugger.util.ClassFileLines;
3635
import datadog.trace.api.Config;
37-
import datadog.trace.bootstrap.debugger.CorrelationAccess;
3836
import datadog.trace.bootstrap.debugger.Limits;
3937
import datadog.trace.bootstrap.debugger.MethodLocation;
4038
import datadog.trace.bootstrap.debugger.ProbeId;
@@ -778,8 +776,6 @@ private InsnList collectCapturedContext(Snapshot.Kind kind, AbstractInsnNode loc
778776
// stack: [capturedcontext]
779777
collectStaticFields(insnList);
780778
// stack: [capturedcontext]
781-
collectCorrelationInfo(insnList);
782-
// stack: [capturedcontext]
783779
/*
784780
* It makes no sense collecting local variables for exceptions - the ones contributing to the exception
785781
* are most likely to be outside of the scope in the exception handler block and there is no way to figure
@@ -1096,42 +1092,6 @@ private void collectStaticFields(InsnList insnList) {
10961092
// stack: [capturedcontext]
10971093
}
10981094

1099-
private void collectCorrelationInfo(InsnList insnList) {
1100-
// expected stack top: [capturedcontext]
1101-
/*
1102-
* We are cheating a bit with CorrelationAccess - utilizing the knowledge that it is a singleton loaded by the
1103-
* bootstrap class loader we can assume that the availability will not change during the app life time.
1104-
* As a side effect, we happen to initialize the access here and not from the injected code.
1105-
*/
1106-
boolean correlationAvailable = CorrelationAccess.instance().isAvailable();
1107-
if (isStatic && !correlationAvailable) {
1108-
// static method and no correlation info, no need to capture fields
1109-
return;
1110-
}
1111-
extractSpecialId(insnList, "dd.trace_id", "getTraceId", "addTraceId");
1112-
// stack: [capturedcontext]
1113-
extractSpecialId(insnList, "dd.span_id", "getSpanId", "addSpanId");
1114-
// stack: [capturedcontext]
1115-
}
1116-
1117-
private void extractSpecialId(
1118-
InsnList insnList, String fieldName, String getMethodName, String addMethodName) {
1119-
insnList.add(new InsnNode(Opcodes.DUP));
1120-
// stack: [capturedcontext, capturedcontext]
1121-
ldc(insnList, fieldName);
1122-
// stack: [capturedcontext, capturedcontext, name]
1123-
ldc(insnList, STRING_TYPE.getClassName());
1124-
// stack: [capturedcontext, capturedcontext, name, type_name]
1125-
invokeStatic(insnList, CORRELATION_ACCESS_TYPE, "instance", CORRELATION_ACCESS_TYPE);
1126-
// stack: [capturedcontext, capturedcontext, name, type_name, access]
1127-
invokeVirtual(insnList, CORRELATION_ACCESS_TYPE, getMethodName, STRING_TYPE);
1128-
// stack: [capturedcontext, capturedcontext, name, type_name, id]
1129-
addCapturedValueOf(insnList, limits);
1130-
// stack: [capturedcontext, capturedcontext, captured_value]
1131-
invokeVirtual(insnList, CAPTURED_CONTEXT_TYPE, addMethodName, Type.VOID_TYPE, CAPTURED_VALUE);
1132-
// stack: [capturedcontext]
1133-
}
1134-
11351095
private static boolean isAccessible(FieldNode fieldNode) {
11361096
Object value = fieldNode.value;
11371097
if (value instanceof Field) {
@@ -1140,67 +1100,6 @@ private static boolean isAccessible(FieldNode fieldNode) {
11401100
return true;
11411101
}
11421102

1143-
private static List<FieldNode> extractInstanceField(
1144-
ClassNode classNode, boolean isStatic, ClassLoader classLoader, Limits limits) {
1145-
List<FieldNode> results = new ArrayList<>();
1146-
if (CorrelationAccess.instance().isAvailable()) {
1147-
results.add(
1148-
new FieldNode(
1149-
Opcodes.ACC_PRIVATE, "dd.trace_id", STRING_TYPE.getDescriptor(), null, null));
1150-
results.add(
1151-
new FieldNode(
1152-
Opcodes.ACC_PRIVATE, "dd.span_id", STRING_TYPE.getDescriptor(), null, null));
1153-
}
1154-
if (isStatic) {
1155-
return results;
1156-
}
1157-
int fieldCount = 0;
1158-
for (FieldNode fieldNode : classNode.fields) {
1159-
if (isStaticField(fieldNode)) {
1160-
continue;
1161-
}
1162-
results.add(fieldNode);
1163-
fieldCount++;
1164-
if (fieldCount > limits.maxFieldCount) {
1165-
return results;
1166-
}
1167-
}
1168-
addInheritedFields(classNode, classLoader, limits, results, fieldCount);
1169-
return results;
1170-
}
1171-
1172-
private static void addInheritedFields(
1173-
ClassNode classNode,
1174-
ClassLoader classLoader,
1175-
Limits limits,
1176-
List<FieldNode> results,
1177-
int fieldCount) {
1178-
String superClassName = extractSuperClass(classNode);
1179-
while (!superClassName.equals(Object.class.getTypeName())) {
1180-
Class<?> clazz;
1181-
try {
1182-
clazz = Class.forName(superClassName, false, classLoader);
1183-
} catch (ClassNotFoundException ex) {
1184-
break;
1185-
}
1186-
for (Field field : clazz.getDeclaredFields()) {
1187-
if (isStaticField(field)) {
1188-
continue;
1189-
}
1190-
String desc = Type.getDescriptor(field.getType());
1191-
FieldNode fieldNode =
1192-
new FieldNode(field.getModifiers(), field.getName(), desc, null, field);
1193-
results.add(fieldNode);
1194-
fieldCount++;
1195-
if (fieldCount > limits.maxFieldCount) {
1196-
return;
1197-
}
1198-
}
1199-
clazz = clazz.getSuperclass();
1200-
superClassName = clazz.getTypeName();
1201-
}
1202-
}
1203-
12041103
private static List<FieldNode> extractStaticFields(
12051104
ClassNode classNode, ClassLoader classLoader, Limits limits) {
12061105
int fieldCount = 0;

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.squareup.moshi.Types;
2424
import datadog.trace.api.Config;
2525
import datadog.trace.bootstrap.debugger.CapturedContext;
26+
import datadog.trace.bootstrap.debugger.CorrelationAccess;
2627
import datadog.trace.bootstrap.debugger.DebuggerContext;
2728
import datadog.trace.bootstrap.debugger.EvaluationError;
2829
import datadog.trace.bootstrap.debugger.Limits;
@@ -549,23 +550,19 @@ protected boolean fillSnapshot(
549550
LogStatus entryStatus = convertStatus(entryContext.getStatus(probeId.getEncodedId()));
550551
LogStatus exitStatus = convertStatus(exitContext.getStatus(probeId.getEncodedId()));
551552
String message = null;
552-
String traceId = null;
553-
String spanId = null;
554553
switch (evaluateAt) {
555554
case ENTRY:
556555
case DEFAULT:
557556
message = entryStatus.getMessage();
558-
traceId = entryContext.getTraceId();
559-
spanId = entryContext.getSpanId();
560557
break;
561558
case EXIT:
562559
message = exitStatus.getMessage();
563-
traceId = exitContext.getTraceId();
564-
spanId = exitContext.getSpanId();
565560
break;
566561
}
567562
boolean shouldCommit = false;
568563
if (entryStatus.shouldSend() && exitStatus.shouldSend()) {
564+
String traceId = CorrelationAccess.instance().getTraceId();
565+
String spanId = CorrelationAccess.instance().getSpanId();
569566
snapshot.setTraceId(traceId);
570567
snapshot.setSpanId(spanId);
571568
if (isCaptureSnapshot()) {
@@ -643,8 +640,8 @@ public void commit(CapturedContext lineContext, int line) {
643640
Snapshot snapshot = createSnapshot();
644641
boolean shouldCommit = false;
645642
if (status.shouldSend()) {
646-
snapshot.setTraceId(lineContext.getTraceId());
647-
snapshot.setSpanId(lineContext.getSpanId());
643+
snapshot.setTraceId(CorrelationAccess.instance().getTraceId());
644+
snapshot.setSpanId(CorrelationAccess.instance().getSpanId());
648645
snapshot.setMessage(status.getMessage());
649646
shouldCommit = true;
650647
}

0 commit comments

Comments
 (0)