@@ -62,3 +62,111 @@ tasks {
6262 relocate(" io.opentelemetry.extension.aws" , " io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws" )
6363 }
6464}
65+
66+ subprojects {
67+ class JavaagentTestArgumentsProvider (
68+ @InputFile
69+ @PathSensitive(org.gradle.api.tasks.PathSensitivity .RELATIVE )
70+ val agentShadowJar : File ,
71+
72+ @InputFile
73+ @PathSensitive(org.gradle.api.tasks.PathSensitivity .RELATIVE )
74+ val shadowJar : File ,
75+
76+ @InputFile
77+ @PathSensitive(org.gradle.api.tasks.PathSensitivity .RELATIVE )
78+ val extensionJar : File ,
79+ ) : CommandLineArgumentProvider {
80+ override fun asArguments (): Iterable <String > = listOf (
81+ " -Dotel.javaagent.debug=true" ,
82+ " -javaagent:${agentShadowJar.absolutePath} " ,
83+ " -Dht.javaagent.filter.jar.paths=${extensionJar.absolutePath} " ,
84+ " -Dotel.exporter.otlp.protocol=http/protobuf" ,
85+ " -Dotel.exporter.otlp.traces.endpoint=http://localhost:4318/v1/traces" ,
86+ " -Dotel.metrics.exporter=none" ,
87+ // make the path to the javaagent available to tests
88+ " -Dotel.javaagent.testing.javaagent-jar-path=${agentShadowJar.absolutePath} " ,
89+ " -Dotel.javaagent.experimental.initializer.jar=${shadowJar.absolutePath} " ,
90+
91+ // prevent sporadic gradle deadlocks, see SafeLogger for more details
92+ " -Dotel.javaagent.testing.transform-safe-logging.enabled=true" ,
93+ // Reduce noise in assertion messages since we don't need to verify this in most tests. We check
94+ // in smoke tests instead.
95+ " -Dotel.javaagent.add-thread-details=false" ,
96+ // suppress repeated logging of "No metric data to export - skipping export."
97+ // since PeriodicMetricReader is configured with a short interval
98+ " -Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.opentelemetry.sdk.metrics.export.PeriodicMetricReader=INFO" ,
99+ // suppress a couple of verbose ClassNotFoundException stack traces logged at debug level
100+ " -Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.internal.ServerImplBuilder=INFO" ,
101+ " -Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.internal.ManagedChannelImplBuilder=INFO" ,
102+ " -Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.perfmark.PerfMark=INFO" ,
103+ " -Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.Context=INFO"
104+ )
105+ }
106+
107+ tasks.withType<Test >().configureEach {
108+ val instShadowTask: Jar = project(" :instrumentation" ).tasks.named<Jar >(" shadowJar" ).get()
109+ inputs.files(layout.files(instShadowTask))
110+ val instShadowJar = instShadowTask.archiveFile.get().asFile
111+
112+ val shadowTask: Jar = project(" :javaagent" ).tasks.named<Jar >(" shadowJar" ).get()
113+ inputs.files(layout.files(shadowTask))
114+ val agentShadowJar = shadowTask.archiveFile.get().asFile
115+
116+ val extensionBuild: Jar = project(" :tests-extension" ).tasks.named<Jar >(" shadowJar" ).get()
117+ inputs.files(layout.files(extensionBuild))
118+ val extensionJar = extensionBuild.archiveFile.get().asFile
119+
120+ dependsOn(" :instrumentation:shadowJar" )
121+
122+ dependsOn(" :javaagent:shadowJar" )
123+
124+ dependsOn(" :tests-extension:shadowJar" )
125+
126+ jvmArgumentProviders.add(JavaagentTestArgumentsProvider (agentShadowJar, instShadowJar, extensionJar))
127+
128+ // We do fine-grained filtering of the classpath of this codebase's sources since Gradle's
129+ // configurations will include transitive dependencies as well, which tests do often need.
130+ classpath = classpath.filter {
131+ if (file(layout.buildDirectory.dir(" resources/main" )).equals(it) || file(layout.buildDirectory.dir(" classes/java/main" )).equals(
132+ it
133+ )
134+ ) {
135+ // The sources are packaged into the testing jar, so we need to exclude them from the test
136+ // classpath, which automatically inherits them, to ensure our shaded versions are used.
137+ return @filter false
138+ }
139+
140+ val lib = it.absoluteFile
141+ if (lib.name.startsWith(" opentelemetry-javaagent-" )) {
142+ // These dependencies are packaged into the testing jar, so we need to exclude them from the test
143+ // classpath, which automatically inherits them, to ensure our shaded versions are used.
144+ return @filter false
145+ }
146+ if (lib.name.startsWith(" javaagent-core" )) {
147+ // These dependencies are packaged into the testing jar, so we need to exclude them from the test
148+ // classpath, which automatically inherits them, to ensure our shaded versions are used.
149+ return @filter false
150+ }
151+ if (lib.name.startsWith(" filter-api" )) {
152+ // These dependencies are packaged into the testing jar, so we need to exclude them from the test
153+ // classpath, which automatically inherits them, to ensure our shaded versions are used.
154+ return @filter false
155+ }
156+ if (lib.name.startsWith(" opentelemetry-" ) && lib.name.contains(" -autoconfigure-" )) {
157+ // These dependencies should not be on the test classpath, because they will auto-instrument
158+ // the library and the tests could pass even if the javaagent instrumentation fails to apply
159+ return @filter false
160+ }
161+ return @filter true
162+ }
163+ }
164+
165+ configurations.configureEach {
166+ if (name.endsWith(" testruntimeclasspath" , ignoreCase = true )) {
167+ // Added by agent, don't let Gradle bring it in when running tests.
168+ exclude(" io.opentelemetry.javaagent" , " opentelemetry-javaagent-bootstrap" )
169+ }
170+ }
171+
172+ }
0 commit comments