|
3 | 3 | import io.sentry.*; |
4 | 4 | import io.sentry.clientreport.DiscardReason; |
5 | 5 | import io.sentry.jcache.SentryJCacheWrapper; |
| 6 | +import io.sentry.kafka.SentryKafkaProducerInterceptor; |
6 | 7 | import io.sentry.protocol.Message; |
7 | 8 | import io.sentry.protocol.User; |
8 | 9 | import java.util.Collections; |
| 10 | +import java.util.Properties; |
9 | 11 | import javax.cache.Cache; |
10 | 12 | import javax.cache.CacheManager; |
11 | 13 | import javax.cache.Caching; |
12 | 14 | 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; |
13 | 19 |
|
14 | 20 | public class Main { |
15 | 21 |
|
@@ -95,6 +101,7 @@ public static void main(String[] args) throws InterruptedException { |
95 | 101 |
|
96 | 102 | // Enable cache tracing to create spans for cache operations |
97 | 103 | options.setEnableCacheTracing(true); |
| 104 | + options.setEnableQueueTracing(true); |
98 | 105 |
|
99 | 106 | // Determine traces sample rate based on the sampling context |
100 | 107 | // options.setTracesSampler( |
@@ -178,6 +185,12 @@ public static void main(String[] args) throws InterruptedException { |
178 | 185 | // cache.remove, and cache.flush spans as children of the active transaction. |
179 | 186 | demonstrateCacheTracing(); |
180 | 187 |
|
| 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 | + |
181 | 194 | // Performance feature |
182 | 195 | // |
183 | 196 | // Transactions collect execution time of the piece of code that's executed between the start |
@@ -247,6 +260,30 @@ private static void captureMetrics() { |
247 | 260 | Sentry.metrics().distribution("distributionMetric", 7.0); |
248 | 261 | } |
249 | 262 |
|
| 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 | + |
250 | 287 | private static class SomeEventProcessor implements EventProcessor { |
251 | 288 | @Override |
252 | 289 | public SentryEvent process(SentryEvent event, Hint hint) { |
|
0 commit comments