Skip to content

Commit d819a4a

Browse files
authored
Merge pull request #5291 from getsentry/fix/queue-instrumentation-kafka-system-test-runner
fix(test): [Queue Instrumentation 16] Enable Kafka profile in system runner
2 parents d764864 + bd9d3b5 commit d819a4a

3 files changed

Lines changed: 37 additions & 3 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

test/system-test-runner.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@
6868

6969
KAFKA_CONTAINER_NAME = "sentry-java-system-test-kafka"
7070
KAFKA_BOOTSTRAP_SERVERS = "localhost:9092"
71+
KAFKA_BROKER_REQUIRED_MODULES = {
72+
"sentry-samples-console",
73+
"sentry-samples-spring-boot-jakarta",
74+
}
75+
KAFKA_PROFILE_REQUIRED_MODULES = {
76+
"sentry-samples-spring-boot-jakarta",
77+
}
7178

7279
class ServerType(Enum):
7380
TOMCAT = 0
@@ -202,7 +209,10 @@ def kill_process(self, pid: int, name: str) -> None:
202209
print(f"Process {pid} was already dead")
203210

204211
def module_requires_kafka(self, sample_module: str) -> bool:
205-
return sample_module == "sentry-samples-console"
212+
return sample_module in KAFKA_BROKER_REQUIRED_MODULES
213+
214+
def module_requires_kafka_profile(self, sample_module: str) -> bool:
215+
return sample_module in KAFKA_PROFILE_REQUIRED_MODULES
206216

207217
def wait_for_port(self, host: str, port: int, max_attempts: int = 20) -> bool:
208218
for _ in range(max_attempts):
@@ -423,6 +433,12 @@ def start_spring_server(self, sample_module: str, java_agent: str, java_agent_au
423433
env.update(SENTRY_ENVIRONMENT_VARIABLES)
424434
env["SENTRY_AUTO_INIT"] = java_agent_auto_init
425435

436+
if self.module_requires_kafka_profile(sample_module):
437+
env["SPRING_PROFILES_ACTIVE"] = "kafka"
438+
print("Enabling Spring profile: kafka")
439+
else:
440+
env.pop("SPRING_PROFILES_ACTIVE", None)
441+
426442
# Build command
427443
jar_path = f"sentry-samples/{sample_module}/build/libs/{sample_module}-0.0.1-SNAPSHOT.jar"
428444
cmd = ["java"]

0 commit comments

Comments
 (0)