Skip to content

Commit e37b42a

Browse files
committed
Add Micronaut 4 support for code origin for spans
Make Code Origin for spans feature independent from dynamic instrumentation so it could be enabled while DI is not Add in Status Logger the code origin feature status
1 parent 9f01834 commit e37b42a

File tree

7 files changed

+61
-33
lines changed

7 files changed

+61
-33
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/Agent.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ private enum AgentFeature {
105105
DEBUGGER(propertyNameToSystemPropertyName(DebuggerConfig.DEBUGGER_ENABLED), false),
106106
EXCEPTION_DEBUGGING(
107107
propertyNameToSystemPropertyName(DebuggerConfig.EXCEPTION_REPLAY_ENABLED), false),
108+
SPAN_ORIGIN(
109+
propertyNameToSystemPropertyName(TraceInstrumentationConfig.CODE_ORIGIN_FOR_SPANS_ENABLED),
110+
false),
108111
DATA_JOBS(propertyNameToSystemPropertyName(GeneralConfig.DATA_JOBS_ENABLED), false),
109112
AGENTLESS_LOG_SUBMISSION(
110113
propertyNameToSystemPropertyName(GeneralConfig.AGENTLESS_LOG_SUBMISSION_ENABLED), false);
@@ -152,6 +155,7 @@ public boolean isEnabledByDefault() {
152155
private static boolean telemetryEnabled = true;
153156
private static boolean debuggerEnabled = false;
154157
private static boolean exceptionDebuggingEnabled = false;
158+
private static boolean spanOriginEnabled = false;
155159
private static boolean agentlessLogSubmissionEnabled = false;
156160

157161
/**
@@ -263,6 +267,7 @@ public static void start(
263267
telemetryEnabled = isFeatureEnabled(AgentFeature.TELEMETRY);
264268
debuggerEnabled = isFeatureEnabled(AgentFeature.DEBUGGER);
265269
exceptionDebuggingEnabled = isFeatureEnabled(AgentFeature.EXCEPTION_DEBUGGING);
270+
spanOriginEnabled = isFeatureEnabled(AgentFeature.SPAN_ORIGIN);
266271
agentlessLogSubmissionEnabled = isFeatureEnabled(AgentFeature.AGENTLESS_LOG_SUBMISSION);
267272

268273
if (profilingEnabled) {
@@ -1073,7 +1078,7 @@ private static void shutdownProfilingAgent(final boolean sync) {
10731078
}
10741079

10751080
private static void maybeStartDebugger(Instrumentation inst, Class<?> scoClass, Object sco) {
1076-
if (!debuggerEnabled && !exceptionDebuggingEnabled) {
1081+
if (!debuggerEnabled && !exceptionDebuggingEnabled && !spanOriginEnabled) {
10771082
return;
10781083
}
10791084
if (!remoteConfigEnabled) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public static synchronized void run(
100100
DebuggerContext.initExceptionDebugger(defaultExceptionDebugger);
101101
}
102102
if (config.isDebuggerCodeOriginEnabled()) {
103+
LOGGER.info("Starting Code Origin for spans");
103104
DebuggerContext.initCodeOrigin(new DefaultCodeOriginRecorder(config, configurationUpdater));
104105
}
105106
if (config.isDebuggerInstrumentTheWorld()) {

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/codeorigin/DefaultCodeOriginRecorder.java

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class DefaultCodeOriginRecorder implements CodeOriginRecorder {
3030

3131
private final ConfigurationUpdater configurationUpdater;
3232

33-
private final Map<String, CodeOriginProbe> fingerprints = new HashMap<>();
33+
private final Map<String, CodeOriginProbe> probesByFingerprint = new HashMap<>();
3434

3535
private final Map<String, CodeOriginProbe> probes = new ConcurrentHashMap<>();
3636

@@ -45,36 +45,28 @@ public DefaultCodeOriginRecorder(Config config, ConfigurationUpdater configurati
4545
public String captureCodeOrigin(boolean entry) {
4646
StackTraceElement element = findPlaceInStack();
4747
String fingerprint = Fingerprinter.fingerprint(element);
48-
CodeOriginProbe probe;
49-
50-
if (isAlreadyInstrumented(fingerprint)) {
51-
probe = fingerprints.get(fingerprint);
52-
} else {
53-
probe =
54-
createProbe(
55-
fingerprint,
56-
entry,
57-
Where.of(
58-
element.getClassName(),
59-
element.getMethodName(),
60-
null,
61-
String.valueOf(element.getLineNumber())));
48+
CodeOriginProbe probe = probesByFingerprint.get(fingerprint);
49+
if (probe == null) {
50+
Where where =
51+
Where.of(
52+
element.getClassName(),
53+
element.getMethodName(),
54+
null,
55+
String.valueOf(element.getLineNumber()));
56+
probe = createProbe(fingerprint, entry, where);
57+
LOG.debug("Creating probe for location {}", where);
6258
}
63-
6459
return probe.getId();
6560
}
6661

6762
@Override
6863
public String captureCodeOrigin(Method method, boolean entry) {
69-
CodeOriginProbe probe;
70-
71-
String fingerPrint = method.toString();
72-
if (isAlreadyInstrumented(fingerPrint)) {
73-
probe = fingerprints.get(fingerPrint);
74-
} else {
75-
probe = createProbe(fingerPrint, entry, Where.of(method));
64+
String fingerprint = method.toString();
65+
CodeOriginProbe probe = probesByFingerprint.get(fingerprint);
66+
if (probe == null) {
67+
probe = createProbe(fingerprint, entry, Where.of(method));
68+
LOG.debug("Creating probe for method {}", fingerprint);
7669
}
77-
7870
return probe.getId();
7971
}
8072

@@ -106,22 +98,16 @@ private StackTraceElement findPlaceInStack() {
10698
.orElse(null));
10799
}
108100

109-
public boolean isAlreadyInstrumented(String fingerprint) {
110-
return fingerprints.containsKey(fingerprint);
111-
}
112-
113101
void addFingerprint(String fingerprint, CodeOriginProbe probe) {
114-
fingerprints.putIfAbsent(fingerprint, probe);
102+
probesByFingerprint.putIfAbsent(fingerprint, probe);
115103
}
116104

117-
public String installProbe(CodeOriginProbe probe) {
105+
public void installProbe(CodeOriginProbe probe) {
118106
CodeOriginProbe installed = probes.putIfAbsent(probe.getId(), probe);
119107
if (installed == null) {
120108
AgentTaskScheduler.INSTANCE.execute(
121109
() -> configurationUpdater.accept(CODE_ORIGIN, getProbes()));
122-
return probe.getId();
123110
}
124-
return installed.getId();
125111
}
126112

127113
public CodeOriginProbe getProbe(String probeId) {

dd-java-agent/instrumentation/micronaut/http-server-netty-4.0/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ addTestSuiteForDir('latestDepTest', 'test')
3030

3131
dependencies {
3232
main_java17CompileOnly group: 'io.micronaut', name: 'micronaut-http-server-netty', version: '4.0.0'
33+
implementation project(':dd-java-agent:instrumentation:span-origin')
3334

3435
// Added to ensure cross compatibility:
3536
testImplementation project(':dd-java-agent:instrumentation:micronaut:http-server-netty-2.0')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package datadog.trace.instrumentation.micronaut.v4_0;
2+
3+
import com.google.auto.service.AutoService;
4+
import datadog.trace.agent.tooling.InstrumenterModule;
5+
import datadog.trace.instrumentation.codeorigin.CodeOriginInstrumentation;
6+
import java.util.Arrays;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
@AutoService(InstrumenterModule.class)
11+
public class MicronautCodeOriginInstrumentation extends CodeOriginInstrumentation {
12+
13+
public static final String IO_MICRONAUT_HTTP_ANNOTATION = "io.micronaut.http.annotation.";
14+
15+
public MicronautCodeOriginInstrumentation() {
16+
super("micronaut-span-origin");
17+
}
18+
19+
@Override
20+
protected Set<String> getAnnotations() {
21+
return new HashSet<>(
22+
Arrays.asList(
23+
IO_MICRONAUT_HTTP_ANNOTATION + "Get",
24+
IO_MICRONAUT_HTTP_ANNOTATION + "Post",
25+
IO_MICRONAUT_HTTP_ANNOTATION + "Put",
26+
IO_MICRONAUT_HTTP_ANNOTATION + "Delete",
27+
IO_MICRONAUT_HTTP_ANNOTATION + "Patch",
28+
IO_MICRONAUT_HTTP_ANNOTATION + "Head",
29+
IO_MICRONAUT_HTTP_ANNOTATION + "Options"));
30+
}
31+
}

dd-trace-core/src/main/java/datadog/trace/core/StatusLogger.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ public void toJson(JsonWriter writer, Config config) throws IOException {
114114
writer.value(config.isDebuggerEnabled());
115115
writer.name("debugger_exception_enabled");
116116
writer.value(config.isDebuggerExceptionEnabled());
117+
writer.name("debugger_span_origin_enabled");
118+
writer.value(config.isDebuggerCodeOriginEnabled());
117119
writer.name("appsec_enabled");
118120
writer.value(config.getAppSecActivation().toString());
119121
writer.name("appsec_rules_file_path");

internal-api/src/main/java/datadog/trace/api/Config.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,6 +4326,8 @@ public String toString() {
43264326
+ debuggerSymbolIncludes
43274327
+ ", debuggerExceptionEnabled="
43284328
+ debuggerExceptionEnabled
4329+
+ ", debuggerCodeOriginEnabled="
4330+
+ debuggerCodeOriginEnabled
43294331
+ ", awsPropagationEnabled="
43304332
+ awsPropagationEnabled
43314333
+ ", sqsPropagationEnabled="

0 commit comments

Comments
 (0)