-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instrument Gradle Launcher to avoid overwriting org.gradle.jvmargs pr…
…operty
- Loading branch information
1 parent
adf9784
commit 0bff977
Showing
3 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...in/groovy/datadog/trace/instrumentation/gradle/GradleDaemonJvmOptionsInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package datadog.trace.instrumentation.gradle; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import datadog.trace.api.Config; | ||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import net.bytebuddy.asm.Advice; | ||
import org.gradle.process.internal.JvmOptions; | ||
|
||
/** | ||
* This instrumentation targets Gradle Launcher, which is the process that is started with | ||
* `gradle`/`gradlew` commands. The launcher starts Gradle Daemon (if not started yet), which is a | ||
* long-lived process that actually runs builds. The instrumentation injects the tracer and its | ||
* config properties into Gradle Daemon JVM settings when the daemon is started. | ||
*/ | ||
@AutoService(InstrumenterModule.class) | ||
public class GradleDaemonJvmOptionsInstrumentation extends InstrumenterModule.CiVisibility | ||
implements Instrumenter.ForSingleType { | ||
|
||
public GradleDaemonJvmOptionsInstrumentation() { | ||
super("gradle", "gradle-daemon-jvm-options"); | ||
} | ||
|
||
@Override | ||
public String instrumentedType() { | ||
return "org.gradle.launcher.daemon.configuration.DaemonJvmOptions"; | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
transformer.applyAdvice( | ||
isConstructor(), | ||
GradleDaemonJvmOptionsInstrumentation.class.getName() + "$InjectJavaAgent"); | ||
} | ||
|
||
public static class InjectJavaAgent { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void injectJavaAgent(@Advice.This final JvmOptions daemonJvmOptions) { | ||
File agentJar = Config.get().getCiVisibilityAgentJarFile(); | ||
Path agentJarPath = agentJar.toPath(); | ||
|
||
StringBuilder agentArg = new StringBuilder("-javaagent:").append(agentJarPath).append('='); | ||
|
||
Properties systemProperties = System.getProperties(); | ||
for (Map.Entry<Object, Object> e : systemProperties.entrySet()) { | ||
String propertyName = (String) e.getKey(); | ||
Object propertyValue = e.getValue(); | ||
if (propertyName.startsWith(Config.PREFIX)) { | ||
daemonJvmOptions.systemProperty(propertyName, propertyValue); | ||
agentArg.append(propertyName).append('=').append(propertyValue).append(','); | ||
} | ||
} | ||
|
||
daemonJvmOptions.jvmArgs(agentArg); | ||
} | ||
} | ||
} |