Skip to content

Commit

Permalink
some tidying, fixing of json structures
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Sep 9, 2024
1 parent 8cd4b40 commit 87d0a4b
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package datadog.trace.bootstrap.debugger.spanorigin;

import static datadog.trace.bootstrap.debugger.DebuggerContext.captureCodeOrigin;
import static java.util.Arrays.stream;

import datadog.trace.api.InstrumenterConfig;
Expand All @@ -13,13 +14,13 @@ public static void entry(Method method) {
stream(method.getParameterTypes())
.map(Class::getName)
.collect(Collectors.joining(",", "(", ")"));
// captureCodeOrigin(signature);
captureCodeOrigin(signature);
}
}

public static void exit() {
if (InstrumenterConfig.get().isCodeOriginEnabled()) {
// captureCodeOrigin(null);
captureCodeOrigin(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ private void applyNewConfiguration(Configuration newConfiguration) {
currentConfiguration = newConfiguration;
if (changes.hasProbeRelatedChanges()) {
LOGGER.info("Applying new probe configuration, changes: {}", changes);
System.out.println(
"****** ConfigurationUpdater.applyNewConfiguration newConfiguration = "
+ newConfiguration);
handleProbesChanges(changes, newConfiguration);
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,6 @@ private void notifyBlockedDefinitions(

private InstrumentationResult applyInstrumentation(
MethodInfo methodInfo, List<ProbeDefinition> definitions) {

Map<ProbeId, List<DiagnosticMessage>> diagnostics = new HashMap<>();
definitions.forEach(
probeDefinition -> diagnostics.put(probeDefinition.getProbeId(), new ArrayList<>()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ public String createProbeForFrame(String signature) {
addFingerprint(fingerprint, probe);

installProbe(probe);
if (AgentTracer.get().activeSpan() != null) {
probe.commit(
CapturedContext.EMPTY_CONTEXT, CapturedContext.EMPTY_CONTEXT, Collections.emptyList());
}

} else {
probe = fingerprints.get(fingerprint);
}

if (AgentTracer.get().activeSpan() != null) {
probe.commit(
CapturedContext.EMPTY_CONTEXT, CapturedContext.EMPTY_CONTEXT, Collections.emptyList());
}
return probe.getId();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.datadog.debugger.probe;

import static com.datadog.debugger.codeorigin.DebuggerConfiguration.isDebuggerEnabled;
import static datadog.trace.api.DDTags.DD_CODE_ORIGIN_SNAPSHOT_ID;
import static datadog.trace.api.DDTags.DD_STACK_CODE_ORIGIN_FRAME;
import static datadog.trace.api.DDTags.DD_STACK_CODE_ORIGIN_TYPE;
import static java.lang.String.format;
Expand Down Expand Up @@ -74,61 +75,72 @@ public void commit(
LOGGER.debug("Could not find the exit span for probeId {}", id);
return;
}
applySpanOriginTags(span);
String snapshotId = null;
if (isDebuggerEnabled(span)) {
Snapshot snapshot = createSnapshot();
if (fillSnapshot(entryContext, exitContext, caughtExceptions, snapshot)) {
snapshotId = snapshot.getId();
LOGGER.debug(
"committing exception probe id={}, snapshot id={}, exception id={}",
id,
snapshot.getId(),
snapshotId,
snapshot.getExceptionId());

addSnapshotId(span, snapshot.getId());
commitSnapshot(snapshot, DebuggerAgent.getSink());
}
}
applySpanOriginTags(span, snapshotId);
}

private void applySpanOriginTags(AgentSpan span) {
private void applySpanOriginTags(AgentSpan span, String snapshotId) {
List<StackTraceElement> entries = getUserStackFrames();
if (!entries.isEmpty()) {
Set<AgentSpan> spans = new LinkedHashSet<>();
spans.add(span);
AgentSpan rootSpan = span.getLocalRootSpan() != null ? span.getLocalRootSpan() : span;
if (rootSpan != null) {
spans.add(rootSpan);
}
int stackIndex = countExistingStacks(span);
if (entrySpanProbe) {
AgentSpan rootSpan = span.getLocalRootSpan() != null ? span.getLocalRootSpan() : span;
if (rootSpan != null) {
spans.add(rootSpan);
}
spans.add(span);
StackTraceElement entry = entries.get(0);
for (AgentSpan s : spans) {
s.setTag(DDTags.DD_CODE_ORIGIN_FILE, toFileName(entry.getClassName()));
s.setTag(DDTags.DD_CODE_ORIGIN_METHOD, entry.getMethodName());
s.setTag(DDTags.DD_CODE_ORIGIN_LINE, entry.getLineNumber());
s.setTag(DDTags.DD_CODE_ORIGIN_TYPE, entry.getClassName());
s.setTag(DDTags.DD_CODE_ORIGIN_METHOD_SIGNATURE, signature);
if (snapshotId != null) {
s.setTag(DD_CODE_ORIGIN_SNAPSHOT_ID, snapshotId);
}
}
}
for (AgentSpan s : spans) {
s.setTag(format(DD_STACK_CODE_ORIGIN_TYPE), entrySpanProbe ? "entry" : "exit");
s.setTag(format(DD_STACK_CODE_ORIGIN_TYPE, stackIndex), entrySpanProbe ? "entry" : "exit");

for (int i = 0; i < entries.size(); i++) {
StackTraceElement info = entries.get(i);
s.setTag(format(DD_STACK_CODE_ORIGIN_FRAME, i, "file"), info.getFileName());
s.setTag(format(DD_STACK_CODE_ORIGIN_FRAME, i, "line"), info.getLineNumber());
s.setTag(format(DD_STACK_CODE_ORIGIN_FRAME, i, "method"), info.getMethodName());
s.setTag(format(DD_STACK_CODE_ORIGIN_FRAME, i, "type"), info.getClassName());
s.setTag(format(DD_STACK_CODE_ORIGIN_FRAME, stackIndex, i, "file"), info.getFileName());
s.setTag(format(DD_STACK_CODE_ORIGIN_FRAME, stackIndex, i, "line"), info.getLineNumber());
s.setTag(
format(DD_STACK_CODE_ORIGIN_FRAME, stackIndex, i, "method"), info.getMethodName());
s.setTag(format(DD_STACK_CODE_ORIGIN_FRAME, stackIndex, i, "type"), info.getClassName());
if (i == 0 && snapshotId != null) {
s.setTag(format(DD_STACK_CODE_ORIGIN_FRAME, stackIndex, i, "snapshot_id"), snapshotId);
}
}
}
}
}

private void addSnapshotId(AgentSpan span, String snapshotId) {
span.setTag(format(DD_STACK_CODE_ORIGIN_FRAME, 0, "snapshot_id"), snapshotId);
if (entrySpanProbe) {
span.getLocalRootSpan()
.setTag(format(DD_STACK_CODE_ORIGIN_FRAME, 0, "snapshot_id"), snapshotId);
private int countExistingStacks(AgentSpan span) {
Set<String> keys = span.getLocalRootSpan().getTags().keySet();
int count = 0;
while (keys.contains(format(DD_STACK_CODE_ORIGIN_FRAME, count, 0, "file"))) {
count++;
}

return count;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ public ElementMatcher<TypeDescription> hierarchyMatcher() {

@Override
public void methodAdvice(MethodTransformer transformer) {
/*
transformer.applyAdvice(
HierarchyMatchers.isAnnotatedWith(matcher),
"datadog.trace.instrumentation.codeorigin.EntrySpanOriginAdvice");
*/
transformer.applyAdvice(
HierarchyMatchers.isAnnotatedWith(matcher),
"datadog.trace.instrumentation.codeorigin.EntrySpanOriginAdvice");
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package datadog.trace.instrumentation.codeorigin;

import datadog.trace.bootstrap.debugger.spanorigin.CodeOriginInfo;
import java.lang.reflect.Method;
import net.bytebuddy.asm.Advice;

public class EntrySpanOriginAdvice {

// @Advice.OnMethodEnter
@Advice.OnMethodEnter
public static void onEnter(@Advice.Origin final Method method) {
// CodeOriginInfo.entry(method);
CodeOriginInfo.entry(method);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package datadog.trace.instrumentation.springdata;

import datadog.trace.api.Config;
import datadog.trace.bootstrap.debugger.spanorigin.CodeOriginInfo;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import datadog.trace.bootstrap.instrumentation.decorator.ClientDecorator;
Expand Down Expand Up @@ -45,6 +46,6 @@ public void onOperation(
span.setResourceName(spanNameForMethod(method));
}

// CodeOriginInfo.exit();
CodeOriginInfo.exit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import datadog.trace.api.InstrumenterConfig;
import datadog.trace.api.Trace;
import datadog.trace.bootstrap.debugger.spanorigin.CodeOriginInfo;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import datadog.trace.bootstrap.instrumentation.decorator.AsyncResultDecorator;
Expand Down Expand Up @@ -90,7 +91,7 @@ public AgentSpan startMethodSpan(Method method) {
afterStart(span);
span.setResourceName(resourceName);

// CodeOriginInfo.entry(method);
CodeOriginInfo.entry(method);
if (measured || InstrumenterConfig.get().isMethodMeasured(method)) {
span.setMeasured(true);
}
Expand Down
6 changes: 3 additions & 3 deletions dd-trace-api/src/main/java/datadog/trace/api/DDTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ public class DDTags {

private static final String DD_LD_PREFIX = "_dd.ld.";

public static final String DD_STACK_CODE_ORIGIN = "_dd.stack.code_origin";
public static final String DD_STACK_CODE_ORIGIN_TYPE = "_dd.stack.code_origin.type";
public static final String DD_STACK_CODE_ORIGIN = "_dd.stack.code_origin.%d.";
public static final String DD_STACK_CODE_ORIGIN_TYPE = DD_STACK_CODE_ORIGIN + "type";
// _dd.stack.code_origin.frame.%d.file|line|method|type|snapshot_id
public static final String DD_STACK_CODE_ORIGIN_FRAME = "_dd.stack.code_origin.frame.%d.%s";
public static final String DD_STACK_CODE_ORIGIN_FRAME = DD_STACK_CODE_ORIGIN + "frame.%d.%s";

public static final String DD_CODE_ORIGIN_FILE = DD_LD_PREFIX + "code_origin.file";
public static final String DD_CODE_ORIGIN_METHOD = DD_LD_PREFIX + "code_origin.method";
Expand Down

0 comments on commit 87d0a4b

Please sign in to comment.