Skip to content

Commit bd9d3b5

Browse files
adinauerclaude
andcommitted
fix(spring): Guard Kafka auto-config on sentry-kafka
Require the sentry-kafka producer interceptor class before activating Spring Boot Jakarta queue auto-configuration. This keeps sentry-kafka optional for customers who only use the starter without Kafka queue tracing support on the classpath. Add a regression test that hides sentry-kafka from the classloader and verifies the Kafka bean post-processors are skipped instead of being registered. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 327df95 commit bd9d3b5

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryAutoConfiguration.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
import org.springframework.core.annotation.Order;
7878
import org.springframework.core.env.Environment;
7979
import org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter;
80-
import org.springframework.kafka.core.KafkaTemplate;
8180
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
8281
import org.springframework.security.core.context.SecurityContextHolder;
8382
import org.springframework.web.client.RestClient;
@@ -250,7 +249,11 @@ static class SentryCacheConfiguration {
250249
}
251250

252251
@Configuration(proxyBeanMethods = false)
253-
@ConditionalOnClass(KafkaTemplate.class)
252+
@ConditionalOnClass(
253+
name = {
254+
"org.springframework.kafka.core.KafkaTemplate",
255+
"io.sentry.kafka.SentryKafkaProducerInterceptor"
256+
})
254257
@ConditionalOnProperty(name = "sentry.enable-queue-tracing", havingValue = "true")
255258
@ConditionalOnMissingClass("io.sentry.opentelemetry.SentryAutoConfigurationCustomizerProvider")
256259
@Open

sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryKafkaAutoConfigurationTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry.spring.boot.jakarta
22

3+
import io.sentry.kafka.SentryKafkaProducerInterceptor
34
import io.sentry.opentelemetry.SentryAutoConfigurationCustomizerProvider
45
import io.sentry.spring.jakarta.kafka.SentryKafkaConsumerBeanPostProcessor
56
import io.sentry.spring.jakarta.kafka.SentryKafkaProducerBeanPostProcessor
@@ -30,6 +31,9 @@ class SentryKafkaAutoConfigurationTest {
3031
private val noOtelClassLoader =
3132
FilteredClassLoader(SentryAutoConfigurationCustomizerProvider::class.java)
3233

34+
private val noSentryKafkaClassLoader =
35+
FilteredClassLoader(SentryKafkaProducerInterceptor::class.java)
36+
3337
@Test
3438
fun `registers Kafka BPPs when queue tracing is enabled`() {
3539
contextRunner
@@ -49,6 +53,17 @@ class SentryKafkaAutoConfigurationTest {
4953
}
5054
}
5155

56+
@Test
57+
fun `does not register Kafka BPPs when sentry-kafka is not present`() {
58+
contextRunner
59+
.withClassLoader(noSentryKafkaClassLoader)
60+
.withPropertyValues("sentry.enable-queue-tracing=true")
61+
.run { context ->
62+
assertThat(context).doesNotHaveBean(SentryKafkaProducerBeanPostProcessor::class.java)
63+
assertThat(context).doesNotHaveBean(SentryKafkaConsumerBeanPostProcessor::class.java)
64+
}
65+
}
66+
5267
@Test
5368
fun `does not register Kafka BPPs when queue tracing is explicitly false`() {
5469
contextRunner

0 commit comments

Comments
 (0)