|
| 1 | +package datadog.trace.instrumentation.gradle; |
| 2 | + |
| 3 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface; |
| 4 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; |
| 5 | + |
| 6 | +import com.google.auto.service.AutoService; |
| 7 | +import datadog.trace.agent.tooling.Instrumenter; |
| 8 | +import datadog.trace.agent.tooling.InstrumenterModule; |
| 9 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 10 | +import java.util.Map; |
| 11 | +import net.bytebuddy.asm.Advice; |
| 12 | +import net.bytebuddy.description.type.TypeDescription; |
| 13 | +import net.bytebuddy.matcher.ElementMatcher; |
| 14 | + |
| 15 | +/** |
| 16 | + * This instrumentation targets Gradle Launcher, which is the process that is started with |
| 17 | + * `gradle`/`gradlew` commands. The launcher starts Gradle Daemon (if not started yet), which is a |
| 18 | + * long-lived process that actually runs builds. The instrumentation injects the tracer and its |
| 19 | + * config properties into Gradle Daemon JVM settings when the daemon is started. |
| 20 | + */ |
| 21 | +@AutoService(InstrumenterModule.class) |
| 22 | +public class GradleLauncherInstrumentation extends InstrumenterModule.CiVisibility |
| 23 | + implements Instrumenter.ForTypeHierarchy { |
| 24 | + |
| 25 | + public GradleLauncherInstrumentation() { |
| 26 | + super("gradle", "gradle-daemon-jvm-options"); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public String hierarchyMarkerType() { |
| 31 | + return "org.gradle.launcher.configuration.AllProperties"; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public ElementMatcher<TypeDescription> hierarchyMatcher() { |
| 36 | + return implementsInterface(named(hierarchyMarkerType())); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void methodAdvice(MethodTransformer transformer) { |
| 41 | + transformer.applyAdvice( |
| 42 | + named("getProperties"), |
| 43 | + GradleLauncherInstrumentation.class.getName() + "$PropertiesAugmentationAdvice"); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public String[] helperClassNames() { |
| 48 | + return new String[] { |
| 49 | + packageName + ".GradleDaemonInjectionUtils", |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressFBWarnings( |
| 54 | + value = "UC_USELESS_OBJECT", |
| 55 | + justification = "jvmOptions is the return value of the original method") |
| 56 | + public static class PropertiesAugmentationAdvice { |
| 57 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 58 | + public static void addJavaagentToGradleDaemonProperties( |
| 59 | + @Advice.Return(readOnly = false) Map<String, String> jvmOptions) { |
| 60 | + jvmOptions = GradleDaemonInjectionUtils.addJavaagentToGradleDaemonProperties(jvmOptions); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments