-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathSentryKafkaConsumerTracing.java
More file actions
255 lines (220 loc) · 8.67 KB
/
SentryKafkaConsumerTracing.java
File metadata and controls
255 lines (220 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package io.sentry.kafka;
import io.sentry.BaggageHeader;
import io.sentry.DateUtils;
import io.sentry.IScopes;
import io.sentry.ISentryLifecycleToken;
import io.sentry.ITransaction;
import io.sentry.ScopesAdapter;
import io.sentry.SentryTraceHeader;
import io.sentry.SpanDataConvention;
import io.sentry.SpanStatus;
import io.sentry.TransactionContext;
import io.sentry.TransactionOptions;
import io.sentry.util.SpanUtils;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.common.header.Header;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** Helper methods for instrumenting raw Kafka consumer record processing. */
@ApiStatus.Experimental
public final class SentryKafkaConsumerTracing {
public static final @NotNull String TRACE_ORIGIN = "manual.queue.kafka.consumer";
private static final @NotNull String CREATOR = "SentryKafkaConsumerTracing";
private static final @NotNull String DELIVERY_ATTEMPT_HEADER = "kafka_deliveryAttempt";
private static final @NotNull String MESSAGE_ID_HEADER = "messaging.message.id";
private final @NotNull IScopes scopes;
SentryKafkaConsumerTracing(final @NotNull IScopes scopes) {
this.scopes = scopes;
}
/**
* Runs the provided {@link Callable} with a Kafka consumer processing transaction for the given
* record.
*
* @param record the Kafka record being processed
* @param callable the processing callback
* @return the return value of the callback
* @param <K> the Kafka record key type
* @param <V> the Kafka record value type
* @param <U> the callback return type
*/
public static <K, V, U> U withTracing(
final @NotNull ConsumerRecord<K, V> record, final @NotNull Callable<U> callable)
throws Exception {
return new SentryKafkaConsumerTracing(ScopesAdapter.getInstance())
.withTracingImpl(record, callable);
}
/**
* Runs the provided {@link Runnable} with a Kafka consumer processing transaction for the given
* record.
*
* @param record the Kafka record being processed
* @param runnable the processing callback
* @param <K> the Kafka record key type
* @param <V> the Kafka record value type
*/
public static <K, V> void withTracing(
final @NotNull ConsumerRecord<K, V> record, final @NotNull Runnable runnable) {
new SentryKafkaConsumerTracing(ScopesAdapter.getInstance()).withTracingImpl(record, runnable);
}
<K, V, U> U withTracingImpl(
final @NotNull ConsumerRecord<K, V> record, final @NotNull Callable<U> callable)
throws Exception {
if (!scopes.getOptions().isEnableQueueTracing() || isIgnored()) {
return callable.call();
}
final @NotNull IScopes forkedScopes;
final @NotNull ISentryLifecycleToken lifecycleToken;
try {
forkedScopes = scopes.forkedRootScopes(CREATOR);
lifecycleToken = forkedScopes.makeCurrent();
} catch (Throwable ignored) {
return callable.call();
}
try (final @NotNull ISentryLifecycleToken ignored = lifecycleToken) {
final @Nullable ITransaction transaction = startTransaction(forkedScopes, record);
boolean didError = false;
@Nullable Throwable callbackThrowable = null;
try {
return callable.call();
} catch (Throwable t) {
didError = true;
callbackThrowable = t;
throw t;
} finally {
finishTransaction(
transaction, didError ? SpanStatus.INTERNAL_ERROR : SpanStatus.OK, callbackThrowable);
}
}
}
<K, V> void withTracingImpl(
final @NotNull ConsumerRecord<K, V> record, final @NotNull Runnable runnable) {
try {
withTracingImpl(
record,
() -> {
runnable.run();
return null;
});
} catch (Throwable t) {
throwUnchecked(t);
}
}
@SuppressWarnings("unchecked")
private static <T extends Throwable> void throwUnchecked(final @NotNull Throwable throwable)
throws T {
throw (T) throwable;
}
private boolean isIgnored() {
return SpanUtils.isIgnored(scopes.getOptions().getIgnoredSpanOrigins(), TRACE_ORIGIN);
}
private <K, V> @Nullable ITransaction startTransaction(
final @NotNull IScopes forkedScopes, final @NotNull ConsumerRecord<K, V> record) {
try {
final @Nullable TransactionContext continued = continueTrace(forkedScopes, record);
if (!forkedScopes.getOptions().isTracingEnabled()) {
return null;
}
final @NotNull TransactionContext txContext =
continued != null ? continued : new TransactionContext("queue.process", "queue.process");
txContext.setName("queue.process");
txContext.setOperation("queue.process");
final @NotNull TransactionOptions txOptions = new TransactionOptions();
txOptions.setOrigin(TRACE_ORIGIN);
txOptions.setBindToScope(true);
final @NotNull ITransaction transaction = forkedScopes.startTransaction(txContext, txOptions);
if (transaction.isNoOp()) {
return null;
}
transaction.setData(SpanDataConvention.MESSAGING_SYSTEM, "kafka");
transaction.setData(SpanDataConvention.MESSAGING_DESTINATION_NAME, record.topic());
final @Nullable String messageId = headerValue(record, MESSAGE_ID_HEADER);
if (messageId != null) {
transaction.setData(SpanDataConvention.MESSAGING_MESSAGE_ID, messageId);
}
final int bodySize = record.serializedValueSize();
if (bodySize >= 0) {
transaction.setData(SpanDataConvention.MESSAGING_MESSAGE_BODY_SIZE, bodySize);
}
final @Nullable Integer retryCount = retryCount(record);
if (retryCount != null) {
transaction.setData(SpanDataConvention.MESSAGING_MESSAGE_RETRY_COUNT, retryCount);
}
final @Nullable Long receiveLatency = receiveLatency(record);
if (receiveLatency != null) {
transaction.setData(SpanDataConvention.MESSAGING_MESSAGE_RECEIVE_LATENCY, receiveLatency);
}
return transaction;
} catch (Throwable ignored) {
return null;
}
}
private void finishTransaction(
final @Nullable ITransaction transaction,
final @NotNull SpanStatus status,
final @Nullable Throwable throwable) {
if (transaction == null || transaction.isNoOp()) {
return;
}
try {
transaction.setStatus(status);
if (throwable != null) {
transaction.setThrowable(throwable);
}
transaction.finish();
} catch (Throwable ignored) {
// Instrumentation must never break customer processing.
}
}
private <K, V> @Nullable TransactionContext continueTrace(
final @NotNull IScopes forkedScopes, final @NotNull ConsumerRecord<K, V> record) {
final @Nullable String sentryTrace = headerValue(record, SentryTraceHeader.SENTRY_TRACE_HEADER);
final @Nullable String baggage = headerValue(record, BaggageHeader.BAGGAGE_HEADER);
final @Nullable List<String> baggageHeaders =
baggage != null ? Collections.singletonList(baggage) : null;
return forkedScopes.continueTrace(sentryTrace, baggageHeaders);
}
private <K, V> @Nullable Integer retryCount(final @NotNull ConsumerRecord<K, V> record) {
final @Nullable Header header = record.headers().lastHeader(DELIVERY_ATTEMPT_HEADER);
if (header == null) {
return null;
}
final byte[] value = header.value();
if (value == null || value.length != Integer.BYTES) {
return null;
}
final int attempt = ByteBuffer.wrap(value).getInt();
if (attempt <= 0) {
return null;
}
return attempt - 1;
}
private <K, V> @Nullable Long receiveLatency(final @NotNull ConsumerRecord<K, V> record) {
final @Nullable String enqueuedTimeStr =
headerValue(record, SentryKafkaProducerInterceptor.SENTRY_ENQUEUED_TIME_HEADER);
if (enqueuedTimeStr == null) {
return null;
}
try {
final double enqueuedTimeSeconds = Double.parseDouble(enqueuedTimeStr);
final double nowSeconds = DateUtils.millisToSeconds(System.currentTimeMillis());
final long latencyMs = (long) ((nowSeconds - enqueuedTimeSeconds) * 1000);
return latencyMs >= 0 ? latencyMs : null;
} catch (NumberFormatException ignored) {
return null;
}
}
private <K, V> @Nullable String headerValue(
final @NotNull ConsumerRecord<K, V> record, final @NotNull String headerName) {
final @Nullable Header header = record.headers().lastHeader(headerName);
if (header == null || header.value() == null) {
return null;
}
return new String(header.value(), StandardCharsets.UTF_8);
}
}