Skip to content

Commit 007d27f

Browse files
adinauerclaude
andcommitted
feat(kafka): Add no-arg producer interceptor for Kafka config
Allow kafka-clients to instantiate SentryKafkaProducerInterceptor via interceptor.classes by adding a no-arg constructor that uses ScopesAdapter. This makes native Kafka interceptor wiring work out of the box in applications and samples.\n\nAlso add a Kafka tracing example to the console sample with a transaction-scoped producer send, and cover no-arg constructor behavior in sentry-kafka tests. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0734938 commit 007d27f

5 files changed

Lines changed: 74 additions & 1 deletion

File tree

‎sentry-kafka/api/sentry-kafka.api‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public final class io/sentry/kafka/SentryKafkaConsumerInterceptor : org/apache/k
1515
public final class io/sentry/kafka/SentryKafkaProducerInterceptor : org/apache/kafka/clients/producer/ProducerInterceptor {
1616
public static final field SENTRY_ENQUEUED_TIME_HEADER Ljava/lang/String;
1717
public static final field TRACE_ORIGIN Ljava/lang/String;
18+
public fun <init> ()V
1819
public fun <init> (Lio/sentry/IScopes;)V
1920
public fun <init> (Lio/sentry/IScopes;Ljava/lang/String;)V
2021
public fun close ()V

‎sentry-kafka/src/main/java/io/sentry/kafka/SentryKafkaProducerInterceptor.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.sentry.DateUtils;
55
import io.sentry.IScopes;
66
import io.sentry.ISpan;
7+
import io.sentry.ScopesAdapter;
78
import io.sentry.SentryTraceHeader;
89
import io.sentry.SpanDataConvention;
910
import io.sentry.SpanOptions;
@@ -28,6 +29,10 @@ public final class SentryKafkaProducerInterceptor<K, V> implements ProducerInter
2829
private final @NotNull IScopes scopes;
2930
private final @NotNull String traceOrigin;
3031

32+
public SentryKafkaProducerInterceptor() {
33+
this(ScopesAdapter.getInstance(), TRACE_ORIGIN);
34+
}
35+
3136
public SentryKafkaProducerInterceptor(final @NotNull IScopes scopes) {
3237
this(scopes, TRACE_ORIGIN);
3338
}

‎sentry-kafka/src/test/kotlin/io/sentry/kafka/SentryKafkaProducerInterceptorTest.kt‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.sentry.kafka
22

33
import io.sentry.IScopes
4+
import io.sentry.ISentryLifecycleToken
45
import io.sentry.Sentry
56
import io.sentry.SentryOptions
67
import io.sentry.SentryTraceHeader
@@ -26,7 +27,11 @@ class SentryKafkaProducerInterceptorTest {
2627

2728
@BeforeTest
2829
fun setup() {
29-
initForTest { it.dsn = "https://key@sentry.io/proj" }
30+
initForTest {
31+
it.dsn = "https://key@sentry.io/proj"
32+
it.isEnableQueueTracing = true
33+
it.tracesSampleRate = 1.0
34+
}
3035
scopes = mock()
3136
options =
3237
SentryOptions().apply {
@@ -95,4 +100,27 @@ class SentryKafkaProducerInterceptorTest {
95100

96101
assertSame(record, result)
97102
}
103+
104+
@Test
105+
fun `no-arg constructor uses current scopes`() {
106+
val transaction = Sentry.startTransaction("tx", "op")
107+
val record = ProducerRecord("my-topic", "key", "value")
108+
109+
try {
110+
val token: ISentryLifecycleToken = transaction.makeCurrent()
111+
try {
112+
val interceptor = SentryKafkaProducerInterceptor<String, String>()
113+
interceptor.onSend(record)
114+
} finally {
115+
token.close()
116+
}
117+
} finally {
118+
transaction.finish()
119+
}
120+
121+
assertNotNull(record.headers().lastHeader(SentryTraceHeader.SENTRY_TRACE_HEADER))
122+
assertNotNull(
123+
record.headers().lastHeader(SentryKafkaProducerInterceptor.SENTRY_ENQUEUED_TIME_HEADER)
124+
)
125+
}
98126
}

‎sentry-samples/sentry-samples-console/build.gradle.kts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ dependencies {
3636
implementation(projects.sentry)
3737
implementation(projects.sentryAsyncProfiler)
3838
implementation(projects.sentryJcache)
39+
implementation(projects.sentryKafka)
3940
implementation(libs.jcache)
4041
implementation(libs.caffeine.jcache)
42+
implementation(libs.kafka.clients)
4143

4244
testImplementation(kotlin(Config.kotlinStdLib))
4345
testImplementation(projects.sentry)

‎sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
import io.sentry.*;
44
import io.sentry.clientreport.DiscardReason;
55
import io.sentry.jcache.SentryJCacheWrapper;
6+
import io.sentry.kafka.SentryKafkaProducerInterceptor;
67
import io.sentry.protocol.Message;
78
import io.sentry.protocol.User;
89
import java.util.Collections;
10+
import java.util.Properties;
911
import javax.cache.Cache;
1012
import javax.cache.CacheManager;
1113
import javax.cache.Caching;
1214
import javax.cache.configuration.MutableConfiguration;
15+
import org.apache.kafka.clients.producer.KafkaProducer;
16+
import org.apache.kafka.clients.producer.ProducerConfig;
17+
import org.apache.kafka.clients.producer.ProducerRecord;
18+
import org.apache.kafka.common.serialization.StringSerializer;
1319

1420
public class Main {
1521

@@ -95,6 +101,7 @@ public static void main(String[] args) throws InterruptedException {
95101

96102
// Enable cache tracing to create spans for cache operations
97103
options.setEnableCacheTracing(true);
104+
options.setEnableQueueTracing(true);
98105

99106
// Determine traces sample rate based on the sampling context
100107
// options.setTracesSampler(
@@ -178,6 +185,12 @@ public static void main(String[] args) throws InterruptedException {
178185
// cache.remove, and cache.flush spans as children of the active transaction.
179186
demonstrateCacheTracing();
180187

188+
// Kafka queue tracing with kafka-clients interceptors.
189+
//
190+
// This uses the native producer interceptor from sentry-kafka.
191+
// If no local Kafka broker is available, this block exits quietly.
192+
demonstrateKafkaTracing();
193+
181194
// Performance feature
182195
//
183196
// Transactions collect execution time of the piece of code that's executed between the start
@@ -247,6 +260,30 @@ private static void captureMetrics() {
247260
Sentry.metrics().distribution("distributionMetric", 7.0);
248261
}
249262

263+
private static void demonstrateKafkaTracing() {
264+
final Properties producerProperties = new Properties();
265+
producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
266+
producerProperties.put(
267+
ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
268+
producerProperties.put(
269+
ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
270+
producerProperties.put(
271+
ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, SentryKafkaProducerInterceptor.class.getName());
272+
273+
final ITransaction transaction = Sentry.startTransaction("kafka-demo", "demo");
274+
try (ISentryLifecycleToken ignored = transaction.makeCurrent()) {
275+
try (KafkaProducer<String, String> producer = new KafkaProducer<>(producerProperties)) {
276+
producer.send(new ProducerRecord<>("sentry-topic", "sentry-kafka sample message")).get();
277+
} catch (InterruptedException e) {
278+
Thread.currentThread().interrupt();
279+
} catch (Exception ignoredException) {
280+
// local broker may not be available when running the sample
281+
}
282+
} finally {
283+
transaction.finish();
284+
}
285+
}
286+
250287
private static class SomeEventProcessor implements EventProcessor {
251288
@Override
252289
public SentryEvent process(SentryEvent event, Hint hint) {

0 commit comments

Comments
 (0)