Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ plugins {
alias(libs.plugins.spotless)
}

spotless {
java {
target("src/**/*.java")
removeUnusedImports()
forbidWildcardImports()
googleJavaFormat(libs.versions.google.java.format.get())
}
}

// The buildSrc still needs to target Java 8 as build time instrumentation and muzzle plugin
// allow to schedule workers on different JDK version.
java {
Expand Down Expand Up @@ -102,6 +111,7 @@ dependencies {

implementation(libs.asm)
implementation(libs.asm.tree)
implementation(libs.asm.commons)

implementation(platform("com.fasterxml.jackson:jackson-bom:2.17.2"))
implementation("com.fasterxml.jackson.core:jackson-databind")
Expand All @@ -128,6 +138,10 @@ testing {
targets.configureEach {
testTask.configure {
enabled = providers.gradleProperty("runBuildSrcTests").isPresent or providers.systemProperty("idea.active").isPresent
providers.gradleProperty("observerTestArtifact").orNull?.let {
systemProperty("observer.test.artifact", it)
systemProperty("observer.test.stock", providers.gradleProperty("observerTestStock").get())
}
}
}
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package datadog.gradle.plugin.observer;

import java.io.File;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.jar.JarFile;

/** Resolve the observer actually attached to this JVM, without caller artifact metadata. */
public final class ObserverAgentSelection {
private ObserverAgentSelection() {}

public static String configurationFingerprint() throws Exception {
return (String)
Class.forName("datadog.trace.observer.bootstrap.ObserverRuntime", false, null)
.getMethod("configurationFingerprint")
.invoke(null);
}

public static File attached() throws Exception {
Class<?> runtime;
try {
runtime = Class.forName("datadog.trace.observer.bootstrap.ObserverRuntime", false, null);
} catch (ClassNotFoundException absent) {
return null;
}
URL resource = runtime.getResource("ObserverRuntime.class");
if (resource == null || !resource.getProtocol().equals("jar")) {
throw new IllegalStateException("Attached observer has no jar resource");
}
File jar =
new File(((JarURLConnection) resource.openConnection()).getJarFileURL().toURI())
.getCanonicalFile();
try (JarFile file = new JarFile(jar)) {
if (!"2"
.equals(file.getManifest().getMainAttributes().getValue("Tracing-The-Tracer-Observer"))) {
throw new IllegalArgumentException("Not an observer artifact");
}
}
return jar;
}

public static File validate() throws Exception {
File jar = attached();
if (jar == null) {
throw new IllegalArgumentException(
"traceTracer requires an observer attached to the Gradle daemon with -javaagent");
}
return jar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package datadog.gradle.plugin.observer;

import org.gradle.api.DefaultTask;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.gradle.work.DisableCachingByDefault;

/** Explicit developer artifact: never included in the published distribution. */
@DisableCachingByDefault(because = "Experimental offline transformation of the locally built agent")
public abstract class ObserverAgentTask extends DefaultTask {
@InputFile
@PathSensitive(PathSensitivity.NONE)
public abstract RegularFileProperty getStockAgent();

@OutputFile
public abstract RegularFileProperty getObserverAgent();

@TaskAction
public void rewrite() throws Exception {
new ObserverAgentRewriter()
.rewrite(getStockAgent().get().getAsFile(), getObserverAgent().get().getAsFile());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package datadog.trace.observer.bootstrap;

import java.io.File;
import java.lang.instrument.Instrumentation;
import java.util.jar.JarFile;

/** Install the JDK-only bridge in bootstrap before any relocated early configuration reads. */
public final class ObserverBootstrap {
private ObserverBootstrap() {}

public static void premain(String arguments, Instrumentation instrumentation) throws Exception {
File jar =
new File(
ObserverBootstrap.class.getProtectionDomain().getCodeSource().getLocation().toURI());
instrumentation.appendToBootstrapClassLoaderSearch(new JarFile(jar));
Class.forName("datadog.trace.observer.bootstrap.ObserverRuntime", true, null)
.getMethod("initializePremain", String.class)
.invoke(null, arguments);
Class.forName("datadog.trace.observer.trace.bootstrap.AgentPreCheck", true, null)
.getMethod("premain", String.class, Instrumentation.class)
.invoke(
null,
arguments != null && arguments.startsWith("v1:") ? null : arguments,
instrumentation);
}
}
Loading
Loading