|
| 1 | +package io.sentry.samples.console.kafka; |
| 2 | + |
| 3 | +import io.sentry.ISentryLifecycleToken; |
| 4 | +import io.sentry.ITransaction; |
| 5 | +import io.sentry.Sentry; |
| 6 | +import io.sentry.kafka.SentryKafkaConsumerInterceptor; |
| 7 | +import io.sentry.kafka.SentryKafkaProducerInterceptor; |
| 8 | +import java.time.Duration; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.Properties; |
| 11 | +import java.util.UUID; |
| 12 | +import java.util.concurrent.CountDownLatch; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | +import org.apache.kafka.clients.consumer.ConsumerConfig; |
| 15 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 16 | +import org.apache.kafka.clients.consumer.KafkaConsumer; |
| 17 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 18 | +import org.apache.kafka.clients.producer.ProducerConfig; |
| 19 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 20 | +import org.apache.kafka.common.serialization.StringDeserializer; |
| 21 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 22 | + |
| 23 | +public final class KafkaShowcase { |
| 24 | + |
| 25 | + public static final String TOPIC = "sentry-topic-console-sample"; |
| 26 | + |
| 27 | + private KafkaShowcase() {} |
| 28 | + |
| 29 | + public static void runKafkaWithSentryInterceptors(final String bootstrapServers) { |
| 30 | + final CountDownLatch consumedLatch = new CountDownLatch(1); |
| 31 | + final Thread consumerThread = |
| 32 | + startConsumerWithSentryInterceptor(bootstrapServers, consumedLatch); |
| 33 | + final Properties producerProperties = createProducerPropertiesWithSentry(bootstrapServers); |
| 34 | + |
| 35 | + final ITransaction transaction = Sentry.startTransaction("kafka-demo", "demo"); |
| 36 | + try (ISentryLifecycleToken ignored = transaction.makeCurrent()) { |
| 37 | + try (KafkaProducer<String, String> producer = new KafkaProducer<>(producerProperties)) { |
| 38 | + Thread.sleep(500); |
| 39 | + producer.send(new ProducerRecord<>(TOPIC, "sentry-kafka sample message")).get(); |
| 40 | + } catch (InterruptedException e) { |
| 41 | + Thread.currentThread().interrupt(); |
| 42 | + } catch (Exception ignoredException) { |
| 43 | + // local broker may not be available when running the sample |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + consumedLatch.await(5, TimeUnit.SECONDS); |
| 48 | + } catch (InterruptedException e) { |
| 49 | + Thread.currentThread().interrupt(); |
| 50 | + } |
| 51 | + } finally { |
| 52 | + consumerThread.interrupt(); |
| 53 | + try { |
| 54 | + consumerThread.join(1000); |
| 55 | + } catch (InterruptedException e) { |
| 56 | + Thread.currentThread().interrupt(); |
| 57 | + } |
| 58 | + transaction.finish(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static Properties createProducerPropertiesWithSentry(final String bootstrapServers) { |
| 63 | + final Properties producerProperties = new Properties(); |
| 64 | + producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); |
| 65 | + producerProperties.put( |
| 66 | + ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); |
| 67 | + producerProperties.put( |
| 68 | + ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); |
| 69 | + |
| 70 | + // Required for Sentry queue tracing in kafka-clients producer setup. |
| 71 | + producerProperties.put( |
| 72 | + ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, SentryKafkaProducerInterceptor.class.getName()); |
| 73 | + |
| 74 | + // Optional tuning for sample stability in CI/local runs. |
| 75 | + producerProperties.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 2000); |
| 76 | + producerProperties.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, 2000); |
| 77 | + producerProperties.put(ProducerConfig.DELIVERY_TIMEOUT_MS_CONFIG, 3000); |
| 78 | + |
| 79 | + return producerProperties; |
| 80 | + } |
| 81 | + |
| 82 | + public static Properties createConsumerPropertiesWithSentry(final String bootstrapServers) { |
| 83 | + final Properties consumerProperties = new Properties(); |
| 84 | + consumerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); |
| 85 | + consumerProperties.put( |
| 86 | + ConsumerConfig.GROUP_ID_CONFIG, "sentry-console-sample-" + UUID.randomUUID()); |
| 87 | + consumerProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); |
| 88 | + consumerProperties.put( |
| 89 | + ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); |
| 90 | + consumerProperties.put( |
| 91 | + ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); |
| 92 | + |
| 93 | + // Required for Sentry queue tracing in kafka-clients consumer setup. |
| 94 | + consumerProperties.put( |
| 95 | + ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, SentryKafkaConsumerInterceptor.class.getName()); |
| 96 | + |
| 97 | + // Optional tuning for sample stability in CI/local runs. |
| 98 | + consumerProperties.put(ConsumerConfig.DEFAULT_API_TIMEOUT_MS_CONFIG, 2000); |
| 99 | + consumerProperties.put(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG, 2000); |
| 100 | + |
| 101 | + return consumerProperties; |
| 102 | + } |
| 103 | + |
| 104 | + private static Thread startConsumerWithSentryInterceptor( |
| 105 | + final String bootstrapServers, final CountDownLatch consumedLatch) { |
| 106 | + final Thread consumerThread = |
| 107 | + new Thread( |
| 108 | + () -> { |
| 109 | + final Properties consumerProperties = |
| 110 | + createConsumerPropertiesWithSentry(bootstrapServers); |
| 111 | + |
| 112 | + try (KafkaConsumer<String, String> consumer = |
| 113 | + new KafkaConsumer<>(consumerProperties)) { |
| 114 | + consumer.subscribe(Collections.singletonList(TOPIC)); |
| 115 | + |
| 116 | + while (!Thread.currentThread().isInterrupted() && consumedLatch.getCount() > 0) { |
| 117 | + final ConsumerRecords<String, String> records = |
| 118 | + consumer.poll(Duration.ofMillis(500)); |
| 119 | + if (!records.isEmpty()) { |
| 120 | + consumedLatch.countDown(); |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + } catch (Exception ignored) { |
| 125 | + // local broker may not be available when running the sample |
| 126 | + } |
| 127 | + }, |
| 128 | + "sentry-kafka-sample-consumer"); |
| 129 | + consumerThread.start(); |
| 130 | + return consumerThread; |
| 131 | + } |
| 132 | +} |
0 commit comments