Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Micronaut 4 support for code origin for spans #8039

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ private enum AgentFeature {
DEBUGGER(propertyNameToSystemPropertyName(DebuggerConfig.DEBUGGER_ENABLED), false),
EXCEPTION_DEBUGGING(
propertyNameToSystemPropertyName(DebuggerConfig.EXCEPTION_REPLAY_ENABLED), false),
SPAN_ORIGIN(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is feature flag. used in isFeatureEnabled function

propertyNameToSystemPropertyName(TraceInstrumentationConfig.CODE_ORIGIN_FOR_SPANS_ENABLED),
false),
DATA_JOBS(propertyNameToSystemPropertyName(GeneralConfig.DATA_JOBS_ENABLED), false),
AGENTLESS_LOG_SUBMISSION(
propertyNameToSystemPropertyName(GeneralConfig.AGENTLESS_LOG_SUBMISSION_ENABLED), false);
Expand Down Expand Up @@ -152,6 +155,7 @@ public boolean isEnabledByDefault() {
private static boolean telemetryEnabled = true;
private static boolean debuggerEnabled = false;
private static boolean exceptionDebuggingEnabled = false;
private static boolean spanOriginEnabled = false;
private static boolean agentlessLogSubmissionEnabled = false;

/**
Expand Down Expand Up @@ -263,6 +267,7 @@ public static void start(
telemetryEnabled = isFeatureEnabled(AgentFeature.TELEMETRY);
debuggerEnabled = isFeatureEnabled(AgentFeature.DEBUGGER);
exceptionDebuggingEnabled = isFeatureEnabled(AgentFeature.EXCEPTION_DEBUGGING);
spanOriginEnabled = isFeatureEnabled(AgentFeature.SPAN_ORIGIN);
agentlessLogSubmissionEnabled = isFeatureEnabled(AgentFeature.AGENTLESS_LOG_SUBMISSION);

if (profilingEnabled) {
Expand Down Expand Up @@ -1073,7 +1078,7 @@ private static void shutdownProfilingAgent(final boolean sync) {
}

private static void maybeStartDebugger(Instrumentation inst, Class<?> scoClass, Object sco) {
if (!debuggerEnabled && !exceptionDebuggingEnabled) {
if (!debuggerEnabled && !exceptionDebuggingEnabled && !spanOriginEnabled) {
return;
}
if (!remoteConfigEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public static synchronized void run(
DebuggerContext.initExceptionDebugger(defaultExceptionDebugger);
}
if (config.isDebuggerCodeOriginEnabled()) {
LOGGER.info("Starting Code Origin for spans");
DebuggerContext.initCodeOrigin(new DefaultCodeOriginRecorder(config, configurationUpdater));
}
if (config.isDebuggerInstrumentTheWorld()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class DefaultCodeOriginRecorder implements CodeOriginRecorder {

private final ConfigurationUpdater configurationUpdater;

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

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

Expand All @@ -45,36 +45,28 @@ public DefaultCodeOriginRecorder(Config config, ConfigurationUpdater configurati
public String captureCodeOrigin(boolean entry) {
StackTraceElement element = findPlaceInStack();
String fingerprint = Fingerprinter.fingerprint(element);
CodeOriginProbe probe;

if (isAlreadyInstrumented(fingerprint)) {
probe = fingerprints.get(fingerprint);
} else {
probe =
createProbe(
fingerprint,
entry,
Where.of(
element.getClassName(),
element.getMethodName(),
null,
String.valueOf(element.getLineNumber())));
CodeOriginProbe probe = probesByFingerprint.get(fingerprint);
if (probe == null) {
Where where =
Where.of(
element.getClassName(),
element.getMethodName(),
null,
String.valueOf(element.getLineNumber()));
probe = createProbe(fingerprint, entry, where);
LOG.debug("Creating probe for location {}", where);
}

return probe.getId();
}

@Override
public String captureCodeOrigin(Method method, boolean entry) {
CodeOriginProbe probe;

String fingerPrint = method.toString();
if (isAlreadyInstrumented(fingerPrint)) {
probe = fingerprints.get(fingerPrint);
} else {
probe = createProbe(fingerPrint, entry, Where.of(method));
String fingerprint = method.toString();
CodeOriginProbe probe = probesByFingerprint.get(fingerprint);
if (probe == null) {
probe = createProbe(fingerprint, entry, Where.of(method));
LOG.debug("Creating probe for method {}", fingerprint);
}

return probe.getId();
}

Expand Down Expand Up @@ -106,22 +98,16 @@ private StackTraceElement findPlaceInStack() {
.orElse(null));
}

public boolean isAlreadyInstrumented(String fingerprint) {
return fingerprints.containsKey(fingerprint);
}

void addFingerprint(String fingerprint, CodeOriginProbe probe) {
fingerprints.putIfAbsent(fingerprint, probe);
probesByFingerprint.putIfAbsent(fingerprint, probe);
}

public String installProbe(CodeOriginProbe probe) {
public void installProbe(CodeOriginProbe probe) {
CodeOriginProbe installed = probes.putIfAbsent(probe.getId(), probe);
if (installed == null) {
AgentTaskScheduler.INSTANCE.execute(
() -> configurationUpdater.accept(CODE_ORIGIN, getProbes()));
return probe.getId();
}
return installed.getId();
}

public CodeOriginProbe getProbe(String probeId) {
Expand Down
1 change: 1 addition & 0 deletions dd-java-agent/instrumentation/micronaut/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ apply from: "$rootDir/gradle/java.gradle"

dependencies {
compileOnly group: 'io.micronaut', name: 'micronaut-http-server-netty', version: '2.0.0'
implementation project(':dd-java-agent:instrumentation:span-origin')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package datadog.trace.instrumentation.micronaut;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.instrumentation.codeorigin.CodeOriginInstrumentation;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@AutoService(InstrumenterModule.class)
public class MicronautCodeOriginInstrumentation extends CodeOriginInstrumentation {

public static final String IO_MICRONAUT_HTTP_ANNOTATION = "io.micronaut.http.annotation.";

public MicronautCodeOriginInstrumentation() {
super("micronaut", "micronaut-span-origin");
}

@Override
protected Set<String> getAnnotations() {
return new HashSet<>(
Arrays.asList(
IO_MICRONAUT_HTTP_ANNOTATION + "Get",
IO_MICRONAUT_HTTP_ANNOTATION + "Post",
IO_MICRONAUT_HTTP_ANNOTATION + "Put",
IO_MICRONAUT_HTTP_ANNOTATION + "Delete",
IO_MICRONAUT_HTTP_ANNOTATION + "Patch",
IO_MICRONAUT_HTTP_ANNOTATION + "Head",
IO_MICRONAUT_HTTP_ANNOTATION + "Options"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public abstract class CodeOriginInstrumentation extends Tracing implements ForTy
private final OneOf<NamedElement> matcher;

@SuppressForbidden
public CodeOriginInstrumentation(String instrumentationName) {
super(instrumentationName);
public CodeOriginInstrumentation(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
this.matcher = NameMatchers.namedOneOf(getAnnotations());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public void toJson(JsonWriter writer, Config config) throws IOException {
writer.value(config.isDebuggerEnabled());
writer.name("debugger_exception_enabled");
writer.value(config.isDebuggerExceptionEnabled());
writer.name("debugger_span_origin_enabled");
writer.value(config.isDebuggerCodeOriginEnabled());
writer.name("appsec_enabled");
writer.value(config.getAppSecActivation().toString());
writer.name("appsec_rules_file_path");
Expand Down
2 changes: 2 additions & 0 deletions internal-api/src/main/java/datadog/trace/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -4326,6 +4326,8 @@ public String toString() {
+ debuggerSymbolIncludes
+ ", debuggerExceptionEnabled="
+ debuggerExceptionEnabled
+ ", debuggerCodeOriginEnabled="
+ debuggerCodeOriginEnabled
+ ", awsPropagationEnabled="
+ awsPropagationEnabled
+ ", sqsPropagationEnabled="
Expand Down
Loading