|
| 1 | +package io.sentry.spring.jakarta.kafka; |
| 2 | + |
| 3 | +import io.sentry.BaggageHeader; |
| 4 | +import io.sentry.IScopes; |
| 5 | +import io.sentry.ISentryLifecycleToken; |
| 6 | +import io.sentry.ITransaction; |
| 7 | +import io.sentry.SentryTraceHeader; |
| 8 | +import io.sentry.SpanDataConvention; |
| 9 | +import io.sentry.SpanStatus; |
| 10 | +import io.sentry.TransactionContext; |
| 11 | +import io.sentry.TransactionOptions; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | +import org.apache.kafka.clients.consumer.Consumer; |
| 16 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 17 | +import org.apache.kafka.common.header.Header; |
| 18 | +import org.jetbrains.annotations.ApiStatus; |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | +import org.jetbrains.annotations.Nullable; |
| 21 | +import org.springframework.kafka.listener.RecordInterceptor; |
| 22 | + |
| 23 | +/** |
| 24 | + * A {@link RecordInterceptor} that creates {@code queue.process} transactions for incoming Kafka |
| 25 | + * records with distributed tracing support. |
| 26 | + */ |
| 27 | +@ApiStatus.Internal |
| 28 | +public final class SentryKafkaRecordInterceptor<K, V> implements RecordInterceptor<K, V> { |
| 29 | + |
| 30 | + static final String TRACE_ORIGIN = "auto.queue.spring_jakarta.kafka.consumer"; |
| 31 | + |
| 32 | + private final @NotNull IScopes scopes; |
| 33 | + private final @Nullable RecordInterceptor<K, V> delegate; |
| 34 | + |
| 35 | + private static final @NotNull ThreadLocal<SentryRecordContext> currentContext = |
| 36 | + new ThreadLocal<>(); |
| 37 | + |
| 38 | + public SentryKafkaRecordInterceptor(final @NotNull IScopes scopes) { |
| 39 | + this(scopes, null); |
| 40 | + } |
| 41 | + |
| 42 | + public SentryKafkaRecordInterceptor( |
| 43 | + final @NotNull IScopes scopes, final @Nullable RecordInterceptor<K, V> delegate) { |
| 44 | + this.scopes = scopes; |
| 45 | + this.delegate = delegate; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public @Nullable ConsumerRecord<K, V> intercept( |
| 50 | + final @NotNull ConsumerRecord<K, V> record, final @NotNull Consumer<K, V> consumer) { |
| 51 | + if (!scopes.getOptions().isEnableQueueTracing()) { |
| 52 | + return delegateIntercept(record, consumer); |
| 53 | + } |
| 54 | + |
| 55 | + final @NotNull IScopes forkedScopes = scopes.forkedScopes("SentryKafkaRecordInterceptor"); |
| 56 | + final @NotNull ISentryLifecycleToken lifecycleToken = forkedScopes.makeCurrent(); |
| 57 | + |
| 58 | + continueTrace(forkedScopes, record); |
| 59 | + |
| 60 | + final @Nullable ITransaction transaction = startTransaction(forkedScopes, record); |
| 61 | + currentContext.set(new SentryRecordContext(lifecycleToken, transaction)); |
| 62 | + |
| 63 | + return delegateIntercept(record, consumer); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void success( |
| 68 | + final @NotNull ConsumerRecord<K, V> record, final @NotNull Consumer<K, V> consumer) { |
| 69 | + try { |
| 70 | + if (delegate != null) { |
| 71 | + delegate.success(record, consumer); |
| 72 | + } |
| 73 | + } finally { |
| 74 | + finishSpan(SpanStatus.OK, null); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void failure( |
| 80 | + final @NotNull ConsumerRecord<K, V> record, |
| 81 | + final @NotNull Exception exception, |
| 82 | + final @NotNull Consumer<K, V> consumer) { |
| 83 | + try { |
| 84 | + if (delegate != null) { |
| 85 | + delegate.failure(record, exception, consumer); |
| 86 | + } |
| 87 | + } finally { |
| 88 | + finishSpan(SpanStatus.INTERNAL_ERROR, exception); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void afterRecord( |
| 94 | + final @NotNull ConsumerRecord<K, V> record, final @NotNull Consumer<K, V> consumer) { |
| 95 | + if (delegate != null) { |
| 96 | + delegate.afterRecord(record, consumer); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private @Nullable ConsumerRecord<K, V> delegateIntercept( |
| 101 | + final @NotNull ConsumerRecord<K, V> record, final @NotNull Consumer<K, V> consumer) { |
| 102 | + if (delegate != null) { |
| 103 | + return delegate.intercept(record, consumer); |
| 104 | + } |
| 105 | + return record; |
| 106 | + } |
| 107 | + |
| 108 | + private void continueTrace( |
| 109 | + final @NotNull IScopes forkedScopes, final @NotNull ConsumerRecord<K, V> record) { |
| 110 | + final @Nullable String sentryTrace = headerValue(record, SentryTraceHeader.SENTRY_TRACE_HEADER); |
| 111 | + final @Nullable String baggage = headerValue(record, BaggageHeader.BAGGAGE_HEADER); |
| 112 | + final @Nullable List<String> baggageHeaders = |
| 113 | + baggage != null ? Collections.singletonList(baggage) : null; |
| 114 | + forkedScopes.continueTrace(sentryTrace, baggageHeaders); |
| 115 | + } |
| 116 | + |
| 117 | + private @Nullable ITransaction startTransaction( |
| 118 | + final @NotNull IScopes forkedScopes, final @NotNull ConsumerRecord<K, V> record) { |
| 119 | + if (!forkedScopes.getOptions().isTracingEnabled()) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + final @NotNull TransactionOptions txOptions = new TransactionOptions(); |
| 124 | + txOptions.setOrigin(TRACE_ORIGIN); |
| 125 | + txOptions.setBindToScope(true); |
| 126 | + |
| 127 | + final @NotNull ITransaction transaction = |
| 128 | + forkedScopes.startTransaction( |
| 129 | + new TransactionContext("queue.process", "queue.process"), txOptions); |
| 130 | + |
| 131 | + if (transaction.isNoOp()) { |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + transaction.setData(SpanDataConvention.MESSAGING_SYSTEM, "kafka"); |
| 136 | + transaction.setData(SpanDataConvention.MESSAGING_DESTINATION_NAME, record.topic()); |
| 137 | + |
| 138 | + final @Nullable String messageId = headerValue(record, "messaging.message.id"); |
| 139 | + if (messageId != null) { |
| 140 | + transaction.setData(SpanDataConvention.MESSAGING_MESSAGE_ID, messageId); |
| 141 | + } |
| 142 | + |
| 143 | + final @Nullable String enqueuedTimeStr = |
| 144 | + headerValue(record, SentryKafkaProducerWrapper.SENTRY_ENQUEUED_TIME_HEADER); |
| 145 | + if (enqueuedTimeStr != null) { |
| 146 | + try { |
| 147 | + final long enqueuedTime = Long.parseLong(enqueuedTimeStr); |
| 148 | + final long latencyMs = System.currentTimeMillis() - enqueuedTime; |
| 149 | + if (latencyMs >= 0) { |
| 150 | + transaction.setData(SpanDataConvention.MESSAGING_MESSAGE_RECEIVE_LATENCY, latencyMs); |
| 151 | + } |
| 152 | + } catch (NumberFormatException ignored) { |
| 153 | + // ignore malformed header |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return transaction; |
| 158 | + } |
| 159 | + |
| 160 | + private void finishSpan(final @NotNull SpanStatus status, final @Nullable Throwable throwable) { |
| 161 | + final @Nullable SentryRecordContext ctx = currentContext.get(); |
| 162 | + if (ctx == null) { |
| 163 | + return; |
| 164 | + } |
| 165 | + currentContext.remove(); |
| 166 | + |
| 167 | + try { |
| 168 | + final @Nullable ITransaction transaction = ctx.transaction; |
| 169 | + if (transaction != null) { |
| 170 | + transaction.setStatus(status); |
| 171 | + if (throwable != null) { |
| 172 | + transaction.setThrowable(throwable); |
| 173 | + } |
| 174 | + transaction.finish(); |
| 175 | + } |
| 176 | + } finally { |
| 177 | + ctx.lifecycleToken.close(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private @Nullable String headerValue( |
| 182 | + final @NotNull ConsumerRecord<K, V> record, final @NotNull String headerName) { |
| 183 | + final @Nullable Header header = record.headers().lastHeader(headerName); |
| 184 | + if (header == null || header.value() == null) { |
| 185 | + return null; |
| 186 | + } |
| 187 | + return new String(header.value(), StandardCharsets.UTF_8); |
| 188 | + } |
| 189 | + |
| 190 | + private static final class SentryRecordContext { |
| 191 | + final @NotNull ISentryLifecycleToken lifecycleToken; |
| 192 | + final @Nullable ITransaction transaction; |
| 193 | + |
| 194 | + SentryRecordContext( |
| 195 | + final @NotNull ISentryLifecycleToken lifecycleToken, |
| 196 | + final @Nullable ITransaction transaction) { |
| 197 | + this.lifecycleToken = lifecycleToken; |
| 198 | + this.transaction = transaction; |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments