|
| 1 | +package io.sentry.spring.jakarta.kafka; |
| 2 | + |
| 3 | +import io.sentry.BaggageHeader; |
| 4 | +import io.sentry.IScopes; |
| 5 | +import io.sentry.ISpan; |
| 6 | +import io.sentry.SentryTraceHeader; |
| 7 | +import io.sentry.SpanDataConvention; |
| 8 | +import io.sentry.SpanOptions; |
| 9 | +import io.sentry.SpanStatus; |
| 10 | +import io.sentry.util.TracingUtils; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.Map; |
| 13 | +import org.apache.kafka.clients.producer.ProducerInterceptor; |
| 14 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 15 | +import org.apache.kafka.clients.producer.RecordMetadata; |
| 16 | +import org.apache.kafka.common.header.Headers; |
| 17 | +import org.jetbrains.annotations.ApiStatus; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | +import org.jetbrains.annotations.Nullable; |
| 20 | + |
| 21 | +/** |
| 22 | + * A Kafka {@link ProducerInterceptor} that creates {@code queue.publish} spans and injects tracing |
| 23 | + * headers into outgoing records. |
| 24 | + * |
| 25 | + * <p>The span starts and finishes synchronously in {@link #onSend(ProducerRecord)}, representing |
| 26 | + * "message enqueued" semantics. This avoids cross-thread correlation complexity since {@link |
| 27 | + * #onAcknowledgement(RecordMetadata, Exception)} runs on the Kafka I/O thread. |
| 28 | + * |
| 29 | + * <p>If the customer already has a {@link ProducerInterceptor}, the {@link |
| 30 | + * SentryKafkaProducerBeanPostProcessor} composes both using Spring's {@link |
| 31 | + * org.springframework.kafka.support.CompositeProducerInterceptor}. |
| 32 | + */ |
| 33 | +@ApiStatus.Internal |
| 34 | +public final class SentryProducerInterceptor<K, V> implements ProducerInterceptor<K, V> { |
| 35 | + |
| 36 | + static final String TRACE_ORIGIN = "auto.queue.spring_jakarta.kafka.producer"; |
| 37 | + static final String SENTRY_ENQUEUED_TIME_HEADER = "sentry-task-enqueued-time"; |
| 38 | + |
| 39 | + private final @NotNull IScopes scopes; |
| 40 | + |
| 41 | + public SentryProducerInterceptor(final @NotNull IScopes scopes) { |
| 42 | + this.scopes = scopes; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public @NotNull ProducerRecord<K, V> onSend(final @NotNull ProducerRecord<K, V> record) { |
| 47 | + if (!scopes.getOptions().isEnableQueueTracing()) { |
| 48 | + return record; |
| 49 | + } |
| 50 | + |
| 51 | + final @Nullable ISpan activeSpan = scopes.getSpan(); |
| 52 | + if (activeSpan == null || activeSpan.isNoOp()) { |
| 53 | + return record; |
| 54 | + } |
| 55 | + |
| 56 | + final @NotNull SpanOptions spanOptions = new SpanOptions(); |
| 57 | + spanOptions.setOrigin(TRACE_ORIGIN); |
| 58 | + final @NotNull ISpan span = activeSpan.startChild("queue.publish", record.topic(), spanOptions); |
| 59 | + if (span.isNoOp()) { |
| 60 | + return record; |
| 61 | + } |
| 62 | + |
| 63 | + span.setData(SpanDataConvention.MESSAGING_SYSTEM, "kafka"); |
| 64 | + span.setData(SpanDataConvention.MESSAGING_DESTINATION_NAME, record.topic()); |
| 65 | + |
| 66 | + try { |
| 67 | + injectHeaders(record.headers(), span); |
| 68 | + } catch (Throwable ignored) { |
| 69 | + // Header injection must not break the send |
| 70 | + } |
| 71 | + |
| 72 | + span.setStatus(SpanStatus.OK); |
| 73 | + span.finish(); |
| 74 | + |
| 75 | + return record; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onAcknowledgement( |
| 80 | + final @Nullable RecordMetadata metadata, final @Nullable Exception exception) {} |
| 81 | + |
| 82 | + @Override |
| 83 | + public void close() {} |
| 84 | + |
| 85 | + @Override |
| 86 | + public void configure(final @Nullable Map<String, ?> configs) {} |
| 87 | + |
| 88 | + private void injectHeaders(final @NotNull Headers headers, final @NotNull ISpan span) { |
| 89 | + final @Nullable TracingUtils.TracingHeaders tracingHeaders = |
| 90 | + TracingUtils.trace(scopes, null, span); |
| 91 | + if (tracingHeaders != null) { |
| 92 | + final @NotNull SentryTraceHeader sentryTraceHeader = tracingHeaders.getSentryTraceHeader(); |
| 93 | + headers.remove(sentryTraceHeader.getName()); |
| 94 | + headers.add( |
| 95 | + sentryTraceHeader.getName(), |
| 96 | + sentryTraceHeader.getValue().getBytes(StandardCharsets.UTF_8)); |
| 97 | + |
| 98 | + final @Nullable BaggageHeader baggageHeader = tracingHeaders.getBaggageHeader(); |
| 99 | + if (baggageHeader != null) { |
| 100 | + headers.remove(baggageHeader.getName()); |
| 101 | + headers.add( |
| 102 | + baggageHeader.getName(), baggageHeader.getValue().getBytes(StandardCharsets.UTF_8)); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + headers.remove(SENTRY_ENQUEUED_TIME_HEADER); |
| 107 | + headers.add( |
| 108 | + SENTRY_ENQUEUED_TIME_HEADER, |
| 109 | + String.valueOf(System.currentTimeMillis()).getBytes(StandardCharsets.UTF_8)); |
| 110 | + } |
| 111 | +} |
0 commit comments