Skip to content

Commit 237a5fa

Browse files
runningcodeclaude
andauthored
build: Move OpenTelemetry agent test wiring into a convention plugin (#6551)
The Spring Boot 2, 3 and 4 modules each copied the setup that runs the agent tests in a child JVM. The io.sentry.otel-agent-tests plugin now owns it, so the modules keep only the dependencies of their Spring Boot line. Fixes #6378 Fixes JAVA-801 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6644050 commit 237a5fa

7 files changed

Lines changed: 138 additions & 184 deletions

File tree

‎.cursor/rules/e2e_tests.mdc‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ match on values only they produce.
5656
## The OpenTelemetry agent
5757

5858
The agent must not run in the test's JVM, because it would instrument the test's own HTTP client
59-
and server. The Spring Boot modules have an `agentFixture` source set with the app. `AgentApp`
60-
starts it once per test JVM in a child process, with `ChildJvmApp` and the agent jar passed through
59+
and server. The `io.sentry.otel-agent-tests` plugin in `build-logic` gives the Spring Boot modules
60+
an `agentFixture` source set with the app, the `integrationTestOtelAgent` suite and the built agent
61+
jar; each module adds only the dependencies of its Spring Boot line. `AgentApp` starts the app once
62+
per test JVM in a child process, with `ChildJvmApp` and the agent jar passed through
6163
`ChildJvmTestArguments`. The suite's task runs with the agent initializing Sentry, and
6264
`integrationTestOtelAgentWithoutAutoInit` runs the same tests with the Spring Boot starter doing
63-
it. Gradle runs the two in parallel.
65+
it. Gradle runs the two in parallel. `otelAgentTests()` in a `springBootMatrix` block registers
66+
both on each version.
6467

6568
## Running them
6669

‎build-logic/build.gradle.kts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,9 @@ gradlePlugin {
4646
id = "io.sentry.spring-boot-matrix"
4747
implementationClass = "io.sentry.gradle.SpringBootMatrixPlugin"
4848
}
49+
register("sentryOtelAgentTests") {
50+
id = "io.sentry.otel-agent-tests"
51+
implementationClass = "io.sentry.gradle.OtelAgentTestsPlugin"
52+
}
4953
}
5054
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package io.sentry.gradle
2+
3+
import org.gradle.api.Plugin
4+
import org.gradle.api.Project
5+
import org.gradle.api.attributes.Bundling
6+
import org.gradle.api.attributes.Usage
7+
import org.gradle.api.file.FileCollection
8+
import org.gradle.api.plugins.JavaPlugin
9+
import org.gradle.api.plugins.jvm.JvmTestSuite
10+
import org.gradle.api.tasks.SourceSetContainer
11+
import org.gradle.api.tasks.testing.Test
12+
import org.gradle.language.base.plugins.LifecycleBasePlugin
13+
import org.gradle.testing.base.TestingExtension
14+
15+
private const val AGENT_FIXTURE = "agentFixture"
16+
private const val OTEL_AGENT_JAR = "otelAgentJar"
17+
private const val SUITE = "integrationTestOtelAgent"
18+
private const val WITHOUT_AUTO_INIT = "${SUITE}WithoutAutoInit"
19+
20+
/**
21+
* Runs a Spring Boot module's OpenTelemetry agent tests against an app in a child JVM, with the
22+
* built agent attached.
23+
*
24+
* The agent must not run in the test's JVM, because it would instrument the test's own HTTP client
25+
* and server. So the app lives in the `agentFixture` source set and runs on its own classpath, like
26+
* a real app. The `integrationTestOtelAgent` suite's task runs the tests with the agent
27+
* initializing Sentry, and `integrationTestOtelAgentWithoutAutoInit` runs them with the Spring Boot
28+
* starter doing it. Each test JVM starts one app, so the second mode needs a task of its own, which
29+
* Gradle runs in parallel with the suite's.
30+
*
31+
* The module adds the app's and the suite's dependencies, which differ per Spring Boot line.
32+
*/
33+
class OtelAgentTestsPlugin : Plugin<Project> {
34+
override fun apply(project: Project) {
35+
project.pluginManager.apply(JavaPlugin::class.java)
36+
val sourceSets = project.extensions.getByType(SourceSetContainer::class.java)
37+
val agentFixture = sourceSets.create(AGENT_FIXTURE)
38+
39+
// A plain project dependency on the agent module resolves to its unshadowed jar, which holds
40+
// only AgentMarker. The agent itself is the shadowed jar, which already bundles its
41+
// dependencies.
42+
project.configurations.create(OTEL_AGENT_JAR) {
43+
isCanBeResolved = true
44+
isCanBeConsumed = false
45+
isTransitive = false
46+
attributes {
47+
attribute(
48+
Usage.USAGE_ATTRIBUTE,
49+
project.objects.named(Usage::class.java, Usage.JAVA_RUNTIME),
50+
)
51+
attribute(
52+
Bundling.BUNDLING_ATTRIBUTE,
53+
project.objects.named(Bundling::class.java, Bundling.SHADOWED),
54+
)
55+
}
56+
}
57+
project.dependencies.add(
58+
OTEL_AGENT_JAR,
59+
project.dependencies.project(
60+
mapOf("path" to ":sentry-opentelemetry:sentry-opentelemetry-agent")
61+
),
62+
)
63+
64+
val testing = project.extensions.getByType(TestingExtension::class.java)
65+
testing.suites.register(SUITE, JvmTestSuite::class.java) {
66+
useJUnit()
67+
targets.all {
68+
testTask.configure { runAgentFixture(autoInit = true, agentFixture.runtimeClasspath) }
69+
}
70+
}
71+
72+
project.tasks.register(WITHOUT_AUTO_INIT, Test::class.java) {
73+
description = "Runs the OpenTelemetry agent tests with SENTRY_AUTO_INIT=false."
74+
group = LifecycleBasePlugin.VERIFICATION_GROUP
75+
val sources = sourceSets.getByName(SUITE)
76+
testClassesDirs = sources.output.classesDirs
77+
classpath = sources.runtimeClasspath
78+
useJUnit()
79+
runAgentFixture(autoInit = false, agentFixture.runtimeClasspath)
80+
}
81+
}
82+
}
83+
84+
/** Registers both OpenTelemetry agent test tasks on each Spring Boot version of the matrix. */
85+
fun SpringBootMatrixExtension.otelAgentTests() {
86+
test(SUITE) { version ->
87+
runAgentFixture(autoInit = true, runtimeClasspath(AGENT_FIXTURE, version))
88+
}
89+
test(SUITE, name = WITHOUT_AUTO_INIT) { version ->
90+
runAgentFixture(autoInit = false, runtimeClasspath(AGENT_FIXTURE, version))
91+
}
92+
}
93+
94+
/**
95+
* Has the tests start the app on [appClasspath] in a child JVM, with the built agent attached.
96+
* [autoInit] decides whether the agent initializes Sentry or, as with `SENTRY_AUTO_INIT=false`, the
97+
* Spring Boot starter.
98+
*/
99+
private fun Test.runAgentFixture(autoInit: Boolean, appClasspath: FileCollection) {
100+
jvmArgumentProviders.add(
101+
project.objects.newInstance(ChildJvmTestArguments::class.java).apply {
102+
agentJar.from(project.configurations.getByName(OTEL_AGENT_JAR))
103+
this.appClasspath.from(appClasspath)
104+
}
105+
)
106+
systemProperty("sentry.test.agentAutoInit", autoInit)
107+
}

‎sentry-opentelemetry/sentry-opentelemetry-agent/build.gradle.kts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ fun isolateClasses(jars: Iterable<File>): CopySpec = copySpec {
108108
}
109109
}
110110

111-
// Don't publish non-shadowed jar (shadowJar is in shadowRuntimeElements)
111+
// Only the agent, the shadow jar in shadowRuntimeElements, is published. The plain jar in
112+
// runtimeElements holds just AgentMarker. A project dependency on this module resolves to it, so
113+
// the Spring Boot modules' unit tests get AgentMarker on their classpath without the agent.
112114
with(components["java"] as AdhocComponentWithVariants) {
113115
configurations.forEach {
114116
withVariantsFromConfiguration(configurations["apiElements"]) { skip() }

‎sentry-spring-boot-4/build.gradle.kts‎

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import io.sentry.gradle.ChildJvmTestArguments
1+
import io.sentry.gradle.otelAgentTests
22
import net.ltgt.gradle.errorprone.errorprone
33
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
44
import org.springframework.boot.gradle.plugin.SpringBootPlugin
@@ -7,6 +7,7 @@ plugins {
77
`java-library`
88
id("io.sentry.javadoc")
99
id("io.sentry.spring-boot-matrix")
10+
id("io.sentry.otel-agent-tests")
1011
alias(libs.plugins.kotlin.jvm)
1112
alias(libs.plugins.kotlin.spring)
1213
alias(libs.plugins.errorprone)
@@ -163,22 +164,7 @@ kotlin {
163164
}
164165
}
165166

166-
// The OpenTelemetry agent tests run this app in a child JVM, on its own classpath like a real app
167-
// with the agent attached, rather than on the test's classpath.
168-
val agentFixture = sourceSets.create("agentFixture")
169-
170-
// The built agent jar, rather than the plain jar the unit tests compile against.
171-
val otelAgentJar =
172-
configurations.create("otelAgentJar") {
173-
isCanBeResolved = true
174-
isCanBeConsumed = false
175-
isTransitive = false
176-
attributes {
177-
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
178-
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.SHADOWED))
179-
}
180-
}
181-
167+
// The app the OpenTelemetry agent tests run in a child JVM.
182168
dependencies {
183169
"agentFixtureImplementation"(platform(SpringBootPlugin.BOM_COORDINATES))
184170
"agentFixtureImplementation"(projects.sentrySpringBoot4Starter)
@@ -201,8 +187,6 @@ dependencies {
201187
"agentFixtureImplementation"(libs.springboot4.starter.webclient)
202188
"agentFixtureImplementation"(libs.springboot4.starter.webflux)
203189
"agentFixtureRuntimeOnly"(libs.hsqldb)
204-
205-
otelAgentJar(projects.sentryOpentelemetry.sentryOpentelemetryAgent)
206190
}
207191

208192
// Swaps Logback for Log4j2 where a classpath has both starters, which only
@@ -337,9 +321,8 @@ testing {
337321
}
338322
}
339323

340-
// Runs agentFixture's app with the agent initializing Sentry.
341-
register<JvmTestSuite>("integrationTestOtelAgent") {
342-
useJUnit()
324+
// The OpenTelemetry agent tests, against agentFixture's app.
325+
named<JvmTestSuite>("integrationTestOtelAgent") {
343326
dependencies {
344327
implementation(platform(SpringBootPlugin.BOM_COORDINATES))
345328
implementation(projects.sentryTestSupport)
@@ -351,7 +334,6 @@ testing {
351334
implementation(libs.springboot4.starter.test)
352335
implementation(libs.spring.kafka4.test)
353336
}
354-
targets.all { testTask.configure { runAgentFixture(autoInit = true) } }
355337
}
356338

357339
// The servlet app with the OpenTelemetry Spring Boot starter, set up by
@@ -389,37 +371,6 @@ testing {
389371
}
390372
}
391373

392-
/**
393-
* Has the OpenTelemetry agent tests start agentFixture's app in a child JVM, with the built agent
394-
* attached. [autoInit] decides whether the agent initializes Sentry or, as with
395-
* `SENTRY_AUTO_INIT=false`, the Spring Boot starter. [appClasspath] is the app's classpath for
396-
* another Spring Boot version.
397-
*/
398-
fun Test.runAgentFixture(
399-
autoInit: Boolean,
400-
appClasspath: FileCollection = agentFixture.runtimeClasspath,
401-
) {
402-
jvmArgumentProviders.add(
403-
objects.newInstance<ChildJvmTestArguments>().apply {
404-
agentJar.from(otelAgentJar)
405-
this.appClasspath.from(appClasspath)
406-
}
407-
)
408-
systemProperty("sentry.test.agentAutoInit", autoInit)
409-
}
410-
411-
// The same tests with the Spring Boot starter initializing Sentry. Each test JVM starts one app, so
412-
// this mode needs a task of its own, which Gradle runs in parallel with the suite's.
413-
tasks.register<Test>("integrationTestOtelAgentWithoutAutoInit") {
414-
description = "Runs the OpenTelemetry agent tests with SENTRY_AUTO_INIT=false."
415-
group = LifecycleBasePlugin.VERIFICATION_GROUP
416-
val sources = sourceSets["integrationTestOtelAgent"]
417-
testClassesDirs = sources.output.classesDirs
418-
classpath = sources.runtimeClasspath
419-
useJUnit()
420-
runAgentFixture(autoInit = false)
421-
}
422-
423374
// The Spring Boot Gradle plugin compiles apps with parameter names, which Spring reads, for example
424375
// to resolve `#id` in a cache key.
425376
listOf(
@@ -441,10 +392,5 @@ springBootMatrix {
441392
test("integrationTestWebflux")
442393
test("integrationTestOtelNoAgent")
443394
test("integrationTestOtlp")
444-
test("integrationTestOtelAgent") { version ->
445-
runAgentFixture(autoInit = true, appClasspath = runtimeClasspath("agentFixture", version))
446-
}
447-
test("integrationTestOtelAgent", name = "integrationTestOtelAgentWithoutAutoInit") { version ->
448-
runAgentFixture(autoInit = false, appClasspath = runtimeClasspath("agentFixture", version))
449-
}
395+
otelAgentTests()
450396
}

‎sentry-spring-boot-jakarta/build.gradle.kts‎

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import io.sentry.gradle.ChildJvmTestArguments
1+
import io.sentry.gradle.otelAgentTests
22
import net.ltgt.gradle.errorprone.errorprone
33
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
44
import org.springframework.boot.gradle.plugin.SpringBootPlugin
@@ -7,6 +7,7 @@ plugins {
77
`java-library`
88
id("io.sentry.javadoc")
99
id("io.sentry.spring-boot-matrix")
10+
id("io.sentry.otel-agent-tests")
1011
alias(libs.plugins.kotlin.jvm)
1112
alias(libs.plugins.errorprone)
1213
alias(libs.plugins.gradle.versions)
@@ -146,22 +147,7 @@ tasks.jar {
146147
}
147148
}
148149

149-
// The OpenTelemetry agent tests run this app in a child JVM, on its own classpath like a real app
150-
// with the agent attached, rather than on the test's classpath.
151-
val agentFixture = sourceSets.create("agentFixture")
152-
153-
// The built agent jar, rather than the plain jar the unit tests compile against.
154-
val otelAgentJar =
155-
configurations.create("otelAgentJar") {
156-
isCanBeResolved = true
157-
isCanBeConsumed = false
158-
isTransitive = false
159-
attributes {
160-
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
161-
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.SHADOWED))
162-
}
163-
}
164-
150+
// The app the OpenTelemetry agent tests run in a child JVM.
165151
dependencies {
166152
"agentFixtureImplementation"(platform(SpringBootPlugin.BOM_COORDINATES))
167153
"agentFixtureImplementation"(projects.sentrySpringBootStarterJakarta)
@@ -182,8 +168,6 @@ dependencies {
182168
"agentFixtureImplementation"(libs.springboot3.starter.webflux)
183169
"agentFixtureImplementation"(libs.spring.kafka3)
184170
"agentFixtureRuntimeOnly"(libs.hsqldb)
185-
186-
otelAgentJar(projects.sentryOpentelemetry.sentryOpentelemetryAgent)
187171
}
188172

189173
// Swaps Logback for Log4j2 where a classpath has both starters, which only
@@ -311,9 +295,8 @@ testing {
311295
}
312296
}
313297

314-
// Runs agentFixture's app with the agent initializing Sentry.
315-
register<JvmTestSuite>("integrationTestOtelAgent") {
316-
useJUnit()
298+
// The OpenTelemetry agent tests, against agentFixture's app.
299+
named<JvmTestSuite>("integrationTestOtelAgent") {
317300
dependencies {
318301
implementation(platform(SpringBootPlugin.BOM_COORDINATES))
319302
implementation(projects.sentryTestSupport)
@@ -324,42 +307,10 @@ testing {
324307
implementation(libs.springboot3.starter.web)
325308
implementation(libs.spring.kafka3.test)
326309
}
327-
targets.all { testTask.configure { runAgentFixture(autoInit = true) } }
328310
}
329311
}
330312
}
331313

332-
/**
333-
* Has the OpenTelemetry agent tests start agentFixture's app in a child JVM, with the built agent
334-
* attached. [autoInit] decides whether the agent initializes Sentry or, as with
335-
* `SENTRY_AUTO_INIT=false`, the Spring Boot starter. [appClasspath] is the app's classpath for
336-
* another Spring Boot version.
337-
*/
338-
fun Test.runAgentFixture(
339-
autoInit: Boolean,
340-
appClasspath: FileCollection = agentFixture.runtimeClasspath,
341-
) {
342-
jvmArgumentProviders.add(
343-
objects.newInstance<ChildJvmTestArguments>().apply {
344-
agentJar.from(otelAgentJar)
345-
this.appClasspath.from(appClasspath)
346-
}
347-
)
348-
systemProperty("sentry.test.agentAutoInit", autoInit)
349-
}
350-
351-
// The same tests with the Spring Boot starter initializing Sentry. Each test JVM starts one app, so
352-
// this mode needs a task of its own, which Gradle runs in parallel with the suite's.
353-
tasks.register<Test>("integrationTestOtelAgentWithoutAutoInit") {
354-
description = "Runs the OpenTelemetry agent tests with SENTRY_AUTO_INIT=false."
355-
group = LifecycleBasePlugin.VERIFICATION_GROUP
356-
val sources = sourceSets["integrationTestOtelAgent"]
357-
testClassesDirs = sources.output.classesDirs
358-
classpath = sources.runtimeClasspath
359-
useJUnit()
360-
runAgentFixture(autoInit = false)
361-
}
362-
363314
// The Spring Boot Gradle plugin compiles apps with parameter names, which Spring reads, for example
364315
// to resolve `#id` in a cache key.
365316
listOf(
@@ -379,10 +330,5 @@ springBootMatrix {
379330
test("integrationTestLog4j2")
380331
test("integrationTestWebflux")
381332
test("integrationTestOtelNoAgent")
382-
test("integrationTestOtelAgent") { version ->
383-
runAgentFixture(autoInit = true, appClasspath = runtimeClasspath("agentFixture", version))
384-
}
385-
test("integrationTestOtelAgent", name = "integrationTestOtelAgentWithoutAutoInit") { version ->
386-
runAgentFixture(autoInit = false, appClasspath = runtimeClasspath("agentFixture", version))
387-
}
333+
otelAgentTests()
388334
}

0 commit comments

Comments
 (0)