|
| 1 | +package io.sentry.kafka; |
| 2 | + |
| 3 | +import io.sentry.BaggageHeader; |
| 4 | +import io.sentry.IScopes; |
| 5 | +import io.sentry.ITransaction; |
| 6 | +import io.sentry.SentryTraceHeader; |
| 7 | +import io.sentry.SpanDataConvention; |
| 8 | +import io.sentry.SpanStatus; |
| 9 | +import io.sentry.TransactionContext; |
| 10 | +import io.sentry.TransactionOptions; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import org.apache.kafka.clients.consumer.ConsumerInterceptor; |
| 16 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 17 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 18 | +import org.apache.kafka.clients.consumer.OffsetAndMetadata; |
| 19 | +import org.apache.kafka.common.TopicPartition; |
| 20 | +import org.apache.kafka.common.header.Header; |
| 21 | +import org.jetbrains.annotations.ApiStatus; |
| 22 | +import org.jetbrains.annotations.NotNull; |
| 23 | +import org.jetbrains.annotations.Nullable; |
| 24 | + |
| 25 | +@ApiStatus.Internal |
| 26 | +public final class SentryKafkaConsumerInterceptor<K, V> implements ConsumerInterceptor<K, V> { |
| 27 | + |
| 28 | + public static final @NotNull String TRACE_ORIGIN = "auto.queue.kafka.consumer"; |
| 29 | + |
| 30 | + private final @NotNull IScopes scopes; |
| 31 | + |
| 32 | + public SentryKafkaConsumerInterceptor(final @NotNull IScopes scopes) { |
| 33 | + this.scopes = scopes; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public @NotNull ConsumerRecords<K, V> onConsume(final @NotNull ConsumerRecords<K, V> records) { |
| 38 | + if (!scopes.getOptions().isEnableQueueTracing() || records.isEmpty()) { |
| 39 | + return records; |
| 40 | + } |
| 41 | + |
| 42 | + final @NotNull ConsumerRecord<K, V> firstRecord = records.iterator().next(); |
| 43 | + |
| 44 | + try { |
| 45 | + final @Nullable TransactionContext continued = continueTrace(firstRecord); |
| 46 | + final @NotNull TransactionContext txContext = |
| 47 | + continued != null ? continued : new TransactionContext("queue.receive", "queue.receive"); |
| 48 | + txContext.setName("queue.receive"); |
| 49 | + txContext.setOperation("queue.receive"); |
| 50 | + |
| 51 | + final @NotNull TransactionOptions txOptions = new TransactionOptions(); |
| 52 | + txOptions.setOrigin(TRACE_ORIGIN); |
| 53 | + txOptions.setBindToScope(false); |
| 54 | + |
| 55 | + final @NotNull ITransaction transaction = scopes.startTransaction(txContext, txOptions); |
| 56 | + if (!transaction.isNoOp()) { |
| 57 | + transaction.setData(SpanDataConvention.MESSAGING_SYSTEM, "kafka"); |
| 58 | + transaction.setData(SpanDataConvention.MESSAGING_DESTINATION_NAME, firstRecord.topic()); |
| 59 | + transaction.setData("messaging.batch.message.count", records.count()); |
| 60 | + transaction.setStatus(SpanStatus.OK); |
| 61 | + transaction.finish(); |
| 62 | + } |
| 63 | + } catch (Throwable ignored) { |
| 64 | + // Instrumentation must never break the customer's Kafka poll loop. |
| 65 | + } |
| 66 | + |
| 67 | + return records; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onCommit(final @NotNull Map<TopicPartition, OffsetAndMetadata> offsets) {} |
| 72 | + |
| 73 | + @Override |
| 74 | + public void close() {} |
| 75 | + |
| 76 | + @Override |
| 77 | + public void configure(final @Nullable Map<String, ?> configs) {} |
| 78 | + |
| 79 | + private @Nullable TransactionContext continueTrace(final @NotNull ConsumerRecord<K, V> record) { |
| 80 | + final @Nullable String sentryTrace = headerValue(record, SentryTraceHeader.SENTRY_TRACE_HEADER); |
| 81 | + final @Nullable String baggage = headerValue(record, BaggageHeader.BAGGAGE_HEADER); |
| 82 | + final @Nullable List<String> baggageHeaders = |
| 83 | + baggage != null ? Collections.singletonList(baggage) : null; |
| 84 | + return scopes.continueTrace(sentryTrace, baggageHeaders); |
| 85 | + } |
| 86 | + |
| 87 | + private @Nullable String headerValue( |
| 88 | + final @NotNull ConsumerRecord<K, V> record, final @NotNull String headerName) { |
| 89 | + final @Nullable Header header = record.headers().lastHeader(headerName); |
| 90 | + if (header == null || header.value() == null) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + return new String(header.value(), StandardCharsets.UTF_8); |
| 94 | + } |
| 95 | +} |
0 commit comments