Skip to content

Commit 29e6406

Browse files
committed
feat(csi): Improve JavaExec task declaration
Avoid serializing heavy objects that blocks Gradle configuration cache. Simplify error handling to get the same result without exit code / doLast tricks.
1 parent 58c1198 commit 29e6406

1 file changed

Lines changed: 44 additions & 44 deletions

File tree

buildSrc/src/main/kotlin/datadog/gradle/plugin/CallSiteInstrumentationPlugin.kt

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import org.gradle.api.tasks.testing.Test
1919
import org.gradle.jvm.tasks.Jar
2020
import org.gradle.jvm.toolchain.JavaLanguageVersion
2121
import org.gradle.jvm.toolchain.JavaToolchainService
22-
import java.io.ByteArrayOutputStream
2322
import java.io.File
2423
import java.nio.file.Files
2524
import java.nio.file.Paths
@@ -28,7 +27,11 @@ import javax.inject.Inject
2827
private const val CALL_SITE_INSTRUMENTER_MAIN_CLASS = "datadog.trace.plugin.csi.PluginApplication"
2928
private const val CALL_SITE_CLASS_SUFFIX = "CallSite"
3029
private const val CALL_SITE_CONSOLE_REPORTER = "CONSOLE"
30+
private const val CALL_SITE_ERROR_CONSOLE_REPORTER = "ERROR_CONSOLE"
3131

32+
/**
33+
* This extension allows to configure the Call Site Instrumenter plugin execution.
34+
*/
3235
abstract class CallSiteInstrumentationExtension @Inject constructor(objectFactory: ObjectFactory) {
3336
/**
3437
* The location of the source code to generate call site ({@code src/main/java} by default).
@@ -43,10 +46,10 @@ abstract class CallSiteInstrumentationExtension @Inject constructor(objectFactor
4346
*/
4447
val suffix: Property<String> = objectFactory.property(String::class.java).convention(CALL_SITE_CLASS_SUFFIX)
4548
/**
46-
* The reporters to use after call site instrumenter run (only #CONSOLE_REPORTER supported for now).
49+
* The reporters to use after call site instrumenter run (only #CALL_SITE_CONSOLE_REPORTER and #CALL_SITE_ERROR_CONSOLE_REPORTER supported for now).
4750
*/
4851
val reporters: ListProperty<String> = objectFactory.listProperty(String::class.java).convention(listOf(
49-
CALL_SITE_CONSOLE_REPORTER
52+
CALL_SITE_ERROR_CONSOLE_REPORTER
5053
))
5154
/**
5255
* The location of the dd-trace-java project to look for the call site instrumenter (optional, current project root folder used if not set).
@@ -64,7 +67,6 @@ abstract class CallSiteInstrumentationExtension @Inject constructor(objectFactor
6467
val jvmArgs: ListProperty<String> = objectFactory.listProperty(String::class.java).convention(listOf("-Xmx128m", "-Xms64m"))
6568
}
6669

67-
6870
abstract class CallSiteInstrumentationPlugin : Plugin<Project>{
6971
@get:Inject
7072
abstract val javaToolchains: JavaToolchainService
@@ -103,7 +105,7 @@ abstract class CallSiteInstrumentationPlugin : Plugin<Project>{
103105
project.dependencies.add("testImplementation", csiSourceSet.output)
104106

105107
// include classes in final JAR
106-
project.tasks.named("jar", Jar::class.java).configure{
108+
project.tasks.named("jar", Jar::class.java).configure {
107109
from(csiSourceSet.output.classesDirs)
108110
}
109111
}
@@ -159,47 +161,45 @@ abstract class CallSiteInstrumentationPlugin : Plugin<Project>{
159161
input: DirectoryProperty,
160162
output: Provider<Directory>) {
161163
val taskName = compileTask.name.replace("compile", "generateCallSite")
162-
val callSiteGeneratorTask = project.tasks.register(taskName, JavaExec::class.java).get()
163-
val stdout = ByteArrayOutputStream()
164-
val stderr = ByteArrayOutputStream()
165-
callSiteGeneratorTask.group = "call site instrumentation"
166-
callSiteGeneratorTask.description = "Generates call sites from ${compileTask.name}"
167-
if (extension.javaVersion.isPresent) {
168-
configureLanguage(callSiteGeneratorTask, extension.javaVersion.get())
169-
}
170-
callSiteGeneratorTask.setStandardOutput(stdout)
171-
callSiteGeneratorTask.setErrorOutput(stderr)
172-
callSiteGeneratorTask.inputs.dir(input)
173-
callSiteGeneratorTask.outputs.dir(output)
174-
callSiteGeneratorTask.mainClass.set(CALL_SITE_INSTRUMENTER_MAIN_CLASS)
175-
176164
val rootFolder = extension.rootFolder.getOrElse(project.rootDir)
177-
val path = Paths.get(rootFolder.toString(),
178-
"buildSrc", "call-site-instrumentation-plugin", "build", "libs", "call-site-instrumentation-plugin-all.jar")
179-
callSiteGeneratorTask.jvmArgs(extension.jvmArgs.get())
180-
callSiteGeneratorTask.classpath(path.toFile())
181-
callSiteGeneratorTask.setIgnoreExitValue(true)
182-
// pass the arguments to the main via file to prevent issues with too long classpaths
183-
callSiteGeneratorTask.doFirst {
184-
val argumentFile = newTempFile(temporaryDir, "call-site-arguments")
185-
val arguments = mutableListOf(
186-
project.projectDir.toPath().resolve(extension.srcFolder.get()).toString(),
187-
input.get().asFile.toString(),
188-
output.get().asFile.toString(),
189-
extension.suffix.get(),
190-
extension.reporters.get().joinToString(",")
191-
)
192-
arguments.addAll(getProgramClasspath(project).map { it.toString() })
193-
Files.write(argumentFile.toPath(), arguments)
194-
callSiteGeneratorTask.args(argumentFile.toString())
195-
}
196-
callSiteGeneratorTask.doLast {
197-
project.logger.info(stdout.toString())
198-
project.logger.error(stderr.toString())
199-
if (callSiteGeneratorTask.executionResult.get().exitValue != 0) {
200-
throw GradleException("Failed to generate call site classes, check task logs for more information")
165+
val pluginJarFile = Paths.get(
166+
rootFolder.toString(),
167+
"buildSrc",
168+
"call-site-instrumentation-plugin",
169+
"build",
170+
"libs",
171+
"call-site-instrumentation-plugin-all.jar"
172+
).toFile()
173+
val sourcePath = project.projectDir.toPath().resolve(extension.srcFolder.get()) // TODO Update with Directory property
174+
val programClassPath = getProgramClasspath(project).map { it.toString() }
175+
val callSiteGeneratorTask = project.tasks.register(taskName, JavaExec::class.java) {
176+
// Task description
177+
group = "call site instrumentation"
178+
description = "Generates call sites from ${compileTask.name}"
179+
// Task input & output
180+
inputs.dir(input)
181+
outputs.dir(output)
182+
// JavaExec configuration
183+
if (extension.javaVersion.isPresent) {
184+
configureLanguage(this, extension.javaVersion.get())
201185
}
202-
}
186+
jvmArgs(extension.jvmArgs.get())
187+
classpath(pluginJarFile)
188+
mainClass.set(CALL_SITE_INSTRUMENTER_MAIN_CLASS)
189+
// Write the call site instrumenter arguments into a temporary file
190+
doFirst {
191+
val argumentFile = newTempFile(temporaryDir, "call-site-arguments")
192+
val arguments = listOf(
193+
sourcePath.toString(),
194+
input.get().asFile.toString(),
195+
output.get().asFile.toString(),
196+
extension.suffix.get(),
197+
extension.reporters.get().joinToString(",")
198+
) + programClassPath
199+
Files.write(argumentFile.toPath(), arguments)
200+
args(argumentFile.toString())
201+
}
202+
}.get()
203203

204204
// insert task after compile
205205
callSiteGeneratorTask.dependsOn(compileTask)

0 commit comments

Comments
 (0)