Skip to content

Commit a37b588

Browse files
bric3amarziali
authored andcommitted
Make CSI plugin collect too much classpaths (#9985)
* chore: Move CSI plugin to it's own package * chore: Tweak some API to use kotlin friendly functions * fix: Restrict default CSI classpath to main and test, compile and runtime configurations. * chore: arg name rename * fix: Include the main source set output * fix: Always make sure java plugin is applied * chore: Make csi plugin not running in project.afterEvaluate * fix: Incorrect URLClassLoader initialization when url is a directory * fix: Make depends on instrument task The 'instrument' plugin changes destination around for now, it needs to be depended on for the time being. * chore: Refactor additional configurations to a `ConfigurableFileCollection` * fix: extension property should not be queried at configuration time * style: Reformat * fix: call site plugin still has to add entries to classpath after jvm test suite ran * chore: code tweaks * fix: generateCallSite was missing a few input properties for up-date checks * fix: csi plugin was patching test compile and test task classpath This forced to apply patching in afterEvaluate, instead, it's easier and cleaner to add the csi output as dependency in the appropriate configuration. * fix: unnecessary targetFolder directory creation and unnecessary transformation to string * chore: Readability of CallSiteUtils::toURL * typo: inout property type * fix: Exclude Kotlin compiler daemon files
1 parent 9fb8f43 commit a37b588

File tree

7 files changed

+344
-245
lines changed

7 files changed

+344
-245
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,6 @@ mise*.local.toml
8181
.config/mise*.toml
8282
# asdf
8383
.tool-versions
84+
85+
# Exclude kotlin build files
86+
.kotlin

buildSrc/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ gradlePlugin {
2424
}
2525
create("call-site-instrumentation-plugin") {
2626
id = "call-site-instrumentation"
27-
implementationClass = "datadog.gradle.plugin.CallSiteInstrumentationPlugin"
27+
implementationClass = "datadog.gradle.plugin.csi.CallSiteInstrumentationPlugin"
2828
}
2929
create("tracer-version-plugin") {
3030
id = "datadog.tracer-version"

buildSrc/call-site-instrumentation-plugin/src/main/java/datadog/trace/plugin/csi/util/CallSiteUtils.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.File;
44
import java.net.MalformedURLException;
55
import java.net.URL;
6+
import java.nio.file.Files;
67
import java.nio.file.Path;
78
import java.util.stream.Collectors;
89
import java.util.stream.IntStream;
@@ -47,7 +48,20 @@ public static String repeat(final char value, int count) {
4748

4849
public static URL toURL(final Path path) {
4950
try {
50-
return path.toUri().toURL();
51+
URL url = path.toUri().toURL();
52+
// URLClassLoader interprets URLs ending with '/' as directories. If the trailing '/' is
53+
// missing, a directory URL is treated as a JAR. If the path does yet exist on disk
54+
// assumes that paths not ending with ".jar" are directories.
55+
boolean shouldAddSlash =
56+
Files.exists(path) ? Files.isDirectory(path) : !path.toString().endsWith(".jar");
57+
58+
if (shouldAddSlash) {
59+
String urlString = url.toString();
60+
if (!urlString.endsWith("/")) {
61+
url = new URL(urlString + "/");
62+
}
63+
}
64+
return url;
5165
} catch (MalformedURLException e) {
5266
throw new RuntimeException(e);
5367
}

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

Lines changed: 0 additions & 243 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package datadog.gradle.plugin.csi
2+
3+
import org.gradle.api.file.ConfigurableFileCollection
4+
import org.gradle.api.file.DirectoryProperty
5+
import org.gradle.api.file.ProjectLayout
6+
import org.gradle.api.model.ObjectFactory
7+
import org.gradle.api.provider.ListProperty
8+
import org.gradle.api.provider.Property
9+
import org.gradle.jvm.toolchain.JavaLanguageVersion
10+
import org.gradle.kotlin.dsl.listProperty
11+
import org.gradle.kotlin.dsl.property
12+
import java.io.File
13+
import javax.inject.Inject
14+
15+
16+
/**
17+
* This extension allows to configure the Call Site Instrumenter plugin execution.
18+
*/
19+
abstract class CallSiteInstrumentationExtension @Inject constructor(
20+
objectFactory: ObjectFactory,
21+
layout: ProjectLayout
22+
) {
23+
companion object {
24+
const val CALL_SITE_CLASS_SUFFIX = "CallSite"
25+
const val CALL_SITE_CONSOLE_REPORTER = "CONSOLE"
26+
const val CALL_SITE_ERROR_CONSOLE_REPORTER = "ERROR_CONSOLE"
27+
}
28+
29+
/**
30+
* The location of the source code to generate call site ({@code <project>/src/main/java} by default).
31+
*/
32+
val srcFolder: DirectoryProperty = objectFactory.directoryProperty().convention(
33+
layout.projectDirectory.dir("src").dir("main").dir("java")
34+
)
35+
36+
/**
37+
* The location to generate call site source code ({@code <project>/build/generated/sources/csi} by default).
38+
*/
39+
val targetFolder: DirectoryProperty = objectFactory.directoryProperty().convention(
40+
layout.buildDirectory.dir("generated/sources/$CSI_SOURCE_SET")
41+
)
42+
43+
/**
44+
* The generated call site source file suffix (#CALL_SITE_CLASS_SUFFIX by default).
45+
*/
46+
val suffix: Property<String> = objectFactory.property<String>().convention(CALL_SITE_CLASS_SUFFIX)
47+
48+
/**
49+
* The reporters to use after call site instrumenter run (only #CALL_SITE_CONSOLE_REPORTER and #CALL_SITE_ERROR_CONSOLE_REPORTER supported for now).
50+
*/
51+
val reporters: ListProperty<String> = objectFactory.listProperty<String>().convention(
52+
listOf(
53+
CALL_SITE_ERROR_CONSOLE_REPORTER
54+
)
55+
)
56+
57+
/**
58+
* The location of the dd-trace-java project to look for the call site instrumenter (optional, current project root folder used if not set).
59+
*/
60+
abstract val rootFolder: Property<File>
61+
62+
/**
63+
* The JVM to use to run the call site instrumenter (optional, default JVM used if not set).
64+
*/
65+
val javaVersion: Property<JavaLanguageVersion> =
66+
objectFactory.property<JavaLanguageVersion>().convention(JavaLanguageVersion.current())
67+
68+
/**
69+
* The JVM arguments to run the call site instrumenter.
70+
*/
71+
val jvmArgs: ListProperty<String> =
72+
objectFactory.listProperty<String>().convention(listOf("-Xmx128m", "-Xms64m"))
73+
74+
/**
75+
* The paths used to look for the call site instrumenter dependencies.
76+
*
77+
* The plugin includes by default **only** the `main` and `test` source sets, and their
78+
* related compilation classpath. As we don't want other test configurations by default.
79+
*
80+
* However, it's possible to contribute additional paths to look for dependencies.
81+
*/
82+
val additionalPaths: ConfigurableFileCollection = objectFactory.fileCollection()
83+
}

0 commit comments

Comments
 (0)