Skip to content

Commit

Permalink
Merge pull request #4709 from DataDog/jpbempel/refactor-json-adapter
Browse files Browse the repository at this point in the history
Refactor Json deserialization for test code only
  • Loading branch information
jpbempel authored Feb 9, 2023
2 parents 380176c + 6f51363 commit b447896
Show file tree
Hide file tree
Showing 9 changed files with 535 additions and 428 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ public static class CapturedContext implements ValueReferenceResolver {
private Limits limits = Limits.DEFAULT;
private String thisClassName;
private List<EvaluationError> evaluationErrors;
private String traceId;
private String spanId;

public CapturedContext() {}

Expand Down Expand Up @@ -696,14 +698,6 @@ public Object getMember(Object target, String memberName) {
return target;
}

private Object tryRetrieveField(String name) {
if (fields == null) {
return Values.UNDEFINED_OBJECT;
}
Object field = fields.get(name);
return field != null ? field : Values.UNDEFINED_OBJECT;
}

private Object tryRetrieveSynthetic(String name) {
if (extensions == null || extensions.isEmpty()) {
return Values.UNDEFINED_OBJECT;
Expand Down Expand Up @@ -793,6 +787,17 @@ public void addFields(CapturedValue[] values) {
for (CapturedValue value : values) {
fields.put(value.name, value);
}
traceId = extractSpecialId("dd.trace_id");
spanId = extractSpecialId("dd.span_id");
}

private String extractSpecialId(String idName) {
CapturedValue capturedValue = fields.get(idName);
if (capturedValue == null) {
return null;
}
Object value = capturedValue.getValue();
return value instanceof String ? (String) value : null;
}

public void addEvaluationError(String expr, String message) {
Expand Down Expand Up @@ -842,6 +847,14 @@ public String getThisClassName() {
return thisClassName;
}

public String getTraceId() {
return traceId;
}

public String getSpanId() {
return spanId;
}

/**
* 'Freeze' the context. The contained arguments, locals and fields are converted from their
* Java instance representation into the corresponding string value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
import datadog.trace.bootstrap.debugger.DebuggerContext;
import datadog.trace.bootstrap.debugger.Snapshot;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Serializes snapshots in Json using Moshi */
public class JsonSnapshotSerializer implements DebuggerContext.SnapshotSerializer {
private static final Logger LOG = LoggerFactory.getLogger(JsonSnapshotSerializer.class);
private static final String DD_TRACE_ID = "dd.trace_id";
private static final String DD_SPAN_ID = "dd.span_id";
private static final JsonAdapter<IntakeRequest> ADAPTER =
Expand Down Expand Up @@ -74,34 +71,8 @@ private void removeTraceSpanId(Snapshot.CapturedContext context) {
}

private void addTraceSpanId(Snapshot.CapturedContext context, IntakeRequest request) {
Map<String, Snapshot.CapturedValue> fields = context.getFields();
if (fields == null) {
return;
}
request.traceId = extractCorrelationField(fields, DD_TRACE_ID);
request.spanId = extractCorrelationField(fields, DD_SPAN_ID);
}

private String extractCorrelationField(
Map<String, Snapshot.CapturedValue> fields, String fieldName) {
Snapshot.CapturedValue fieldValue = fields.get(fieldName);
if (fieldValue != null) {
return getValue(fieldValue, fieldName);
}
return null;
}

public static String getValue(Snapshot.CapturedValue capturedValue, String name) {
if (capturedValue != null) {
try {
Snapshot.CapturedValue deserializedValue =
VALUE_ADAPTER.fromJson(capturedValue.getStrValue());
return String.valueOf(deserializedValue.getValue());
} catch (Exception e) {
LOG.warn("Cannot deserialize " + name, e);
}
}
return null;
request.traceId = context.getTraceId();
request.spanId = context.getSpanId();
}

public static class IntakeRequest {
Expand Down
Loading

0 comments on commit b447896

Please sign in to comment.