-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
SentrySpanProcessor.java
355 lines (308 loc) · 12.4 KB
/
SentrySpanProcessor.java
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package io.sentry.opentelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import io.sentry.Baggage;
import io.sentry.DateUtils;
import io.sentry.DsnUtil;
import io.sentry.HubAdapter;
import io.sentry.IHub;
import io.sentry.ISpan;
import io.sentry.ITransaction;
import io.sentry.Instrumenter;
import io.sentry.SentryLevel;
import io.sentry.SentrySpanStorage;
import io.sentry.SentryTraceHeader;
import io.sentry.SpanId;
import io.sentry.SpanStatus;
import io.sentry.TransactionContext;
import io.sentry.TransactionOptions;
import io.sentry.protocol.SentryId;
import io.sentry.protocol.TransactionNameSource;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class SentrySpanProcessor implements SpanProcessor {
private final @NotNull List<SpanKind> spanKindsConsideredForSentryRequests =
Arrays.asList(SpanKind.CLIENT, SpanKind.INTERNAL);
private final @NotNull SpanDescriptionExtractor spanDescriptionExtractor =
new SpanDescriptionExtractor();
private final @NotNull SentrySpanStorage spanStorage = SentrySpanStorage.getInstance();
private final @NotNull IHub hub;
public SentrySpanProcessor() {
this(HubAdapter.getInstance());
}
SentrySpanProcessor(final @NotNull IHub hub) {
this.hub = hub;
}
@Override
public void onStart(final @NotNull Context parentContext, final @NotNull ReadWriteSpan otelSpan) {
if (!ensurePrerequisites(otelSpan)) {
return;
}
final @NotNull TraceData traceData = getTraceData(otelSpan, parentContext);
if (isSentryRequest(otelSpan)) {
hub.getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Not forwarding OpenTelemetry span %s to Sentry as this looks like a span for a request to Sentry (trace %s).",
traceData.getSpanId(),
traceData.getTraceId());
return;
}
final @Nullable ISpan sentryParentSpan =
traceData.getParentSpanId() == null ? null : spanStorage.get(traceData.getParentSpanId());
if (sentryParentSpan != null) {
hub.getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Creating Sentry child span for OpenTelemetry span %s (trace %s). Parent span is %s.",
traceData.getSpanId(),
traceData.getTraceId(),
traceData.getParentSpanId());
final @NotNull Date startDate =
DateUtils.nanosToDate(otelSpan.toSpanData().getStartEpochNanos());
final @NotNull ISpan sentryChildSpan =
sentryParentSpan.startChild(
otelSpan.getName(), otelSpan.getName(), startDate, Instrumenter.OTEL);
spanStorage.store(traceData.getSpanId(), sentryChildSpan);
} else {
hub.getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Creating Sentry transaction for OpenTelemetry span %s (trace %s).",
traceData.getSpanId(),
traceData.getTraceId());
final @NotNull String transactionName = otelSpan.getName();
final @NotNull TransactionNameSource transactionNameSource = TransactionNameSource.CUSTOM;
final @Nullable String op = otelSpan.getName();
final SpanId spanId = new SpanId(traceData.getSpanId());
final @NotNull TransactionContext transactionContext =
traceData.getSentryTraceHeader() == null
? new TransactionContext(
transactionName,
op,
new SentryId(traceData.getTraceId()),
spanId,
transactionNameSource,
null,
null,
null)
: TransactionContext.fromSentryTrace(
transactionName,
transactionNameSource,
op,
traceData.getSentryTraceHeader(),
traceData.getBaggage(),
spanId);
;
transactionContext.setInstrumenter(Instrumenter.OTEL);
TransactionOptions transactionOptions = new TransactionOptions();
transactionOptions.setStartTimestamp(
DateUtils.nanosToDate(otelSpan.toSpanData().getStartEpochNanos()));
ISpan sentryTransaction = hub.startTransaction(transactionContext, transactionOptions);
spanStorage.store(traceData.getSpanId(), sentryTransaction);
}
}
@Override
public boolean isStartRequired() {
return true;
}
@Override
public void onEnd(final @NotNull ReadableSpan otelSpan) {
if (!ensurePrerequisites(otelSpan)) {
return;
}
final @NotNull TraceData traceData = getTraceData(otelSpan, null);
final @Nullable ISpan sentrySpan = spanStorage.removeAndGet(traceData.getSpanId());
if (sentrySpan == null) {
hub.getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Unable to find Sentry span for OpenTelemetry span %s (trace %s).",
traceData.getSpanId(),
traceData.getTraceId());
return;
}
if (isSentryRequest(otelSpan)) {
hub.getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Not forwarding OpenTelemetry span %s to Sentry as this looks like a span for a request to Sentry (trace %s).",
traceData.getSpanId(),
traceData.getTraceId());
return;
}
if (sentrySpan instanceof ITransaction) {
final @NotNull ITransaction sentryTransaction = (ITransaction) sentrySpan;
updateTransactionWithOtelData(sentryTransaction, otelSpan);
hub.getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Finishing Sentry transaction %s for OpenTelemetry span %s (trace %s).",
sentryTransaction.getEventId(),
traceData.getSpanId(),
traceData.getTraceId());
} else {
updateSpanWithOtelData(sentrySpan, otelSpan);
hub.getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Finishing Sentry span for OpenTelemetry span %s (trace %s). Parent span is %s.",
traceData.getSpanId(),
traceData.getTraceId(),
traceData.getParentSpanId());
}
final @NotNull SpanStatus sentryStatus = mapOtelStatus(otelSpan);
final @NotNull Date endTimestamp =
DateUtils.nanosToDate(otelSpan.toSpanData().getEndEpochNanos());
sentrySpan.finish(sentryStatus, endTimestamp);
}
@Override
public boolean isEndRequired() {
return true;
}
private boolean ensurePrerequisites(final @NotNull ReadableSpan otelSpan) {
if (!hasSentryBeenInitialized()) {
hub.getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Not forwarding OpenTelemetry span to Sentry as Sentry has not yet been initialized.");
return false;
}
final @NotNull Instrumenter instrumenter = hub.getOptions().getInstrumenter();
if (!Instrumenter.OTEL.equals(instrumenter)) {
hub.getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Not forwarding OpenTelemetry span to Sentry as instrumenter has been set to %s.",
instrumenter);
return false;
}
final @NotNull SpanContext otelSpanContext = otelSpan.getSpanContext();
if (!otelSpanContext.isValid()) {
hub.getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Not forwarding OpenTelemetry span to Sentry as the span is invalid.");
return false;
}
return true;
}
private boolean isSentryRequest(final @NotNull ReadableSpan otelSpan) {
final @NotNull SpanKind kind = otelSpan.getKind();
if (!spanKindsConsideredForSentryRequests.contains(kind)) {
return false;
}
final @Nullable String httpUrl = otelSpan.getAttribute(SemanticAttributes.HTTP_URL);
return DsnUtil.urlContainsDsnHost(hub.getOptions(), httpUrl);
}
private @NotNull TraceData getTraceData(
final @NotNull ReadableSpan otelSpan, final @Nullable Context parentContext) {
final @NotNull SpanContext otelSpanContext = otelSpan.getSpanContext();
final @NotNull String otelSpanId = otelSpanContext.getSpanId();
final @NotNull String otelParentSpanIdMaybeInvalid =
otelSpan.getParentSpanContext().getSpanId();
final @NotNull String otelTraceId = otelSpanContext.getTraceId();
final @Nullable String otelParentSpanId =
io.opentelemetry.api.trace.SpanId.isValid(otelParentSpanIdMaybeInvalid)
? otelParentSpanIdMaybeInvalid
: null;
@Nullable SentryTraceHeader sentryTraceHeader = null;
@Nullable Baggage baggage = null;
if (parentContext != null) {
sentryTraceHeader = parentContext.get(SentryOtelKeys.SENTRY_TRACE_KEY);
if (sentryTraceHeader != null) {
baggage = parentContext.get(SentryOtelKeys.SENTRY_BAGGAGE_KEY);
}
}
return new TraceData(otelTraceId, otelSpanId, otelParentSpanId, sentryTraceHeader, baggage);
}
private void updateTransactionWithOtelData(
final @NotNull ITransaction sentryTransaction, final @NotNull ReadableSpan otelSpan) {
final @NotNull OtelSpanInfo otelSpanInfo =
spanDescriptionExtractor.extractSpanDescription(otelSpan);
sentryTransaction.setOperation(otelSpanInfo.getOp());
sentryTransaction.setName(
otelSpanInfo.getDescription(), otelSpanInfo.getTransactionNameSource());
final @NotNull Map<String, Object> otelContext = toOtelContext(otelSpan);
sentryTransaction.setContext("otel", otelContext);
}
private @NotNull Map<String, Object> toOtelContext(final @NotNull ReadableSpan otelSpan) {
final @NotNull SpanData spanData = otelSpan.toSpanData();
final @NotNull Map<String, Object> context = new HashMap<>();
context.put("attributes", toMapWithStringKeys(spanData.getAttributes()));
context.put("resource", toMapWithStringKeys(spanData.getResource().getAttributes()));
return context;
}
private void updateSpanWithOtelData(
final @NotNull ISpan sentrySpan, final @NotNull ReadableSpan otelSpan) {
final @NotNull SpanData spanData = otelSpan.toSpanData();
sentrySpan.setData("otel.kind", otelSpan.getKind());
spanData
.getAttributes()
.forEach(
(attributeKey, value) -> {
if (value != null) {
sentrySpan.setData(attributeKey.getKey(), value);
}
});
final @NotNull OtelSpanInfo otelSpanInfo =
spanDescriptionExtractor.extractSpanDescription(otelSpan);
sentrySpan.setOperation(otelSpanInfo.getOp());
sentrySpan.setDescription(otelSpanInfo.getDescription());
}
private SpanStatus mapOtelStatus(final @NotNull ReadableSpan otelSpan) {
final @NotNull SpanData otelSpanData = otelSpan.toSpanData();
final @NotNull StatusData otelStatus = otelSpanData.getStatus();
final @NotNull StatusCode otelStatusCode = otelStatus.getStatusCode();
if (StatusCode.OK.equals(otelStatusCode) || StatusCode.UNSET.equals(otelStatusCode)) {
return SpanStatus.OK;
}
final @Nullable Long httpStatus = otelSpan.getAttribute(SemanticAttributes.HTTP_STATUS_CODE);
if (httpStatus != null) {
final @Nullable SpanStatus spanStatus = SpanStatus.fromHttpStatusCode(httpStatus.intValue());
if (spanStatus != null) {
return spanStatus;
}
}
return SpanStatus.UNKNOWN_ERROR;
}
private boolean hasSentryBeenInitialized() {
return hub.isEnabled();
}
private @NotNull Map<String, Object> toMapWithStringKeys(final @Nullable Attributes attributes) {
final @NotNull Map<String, Object> mapWithStringKeys = new HashMap<>();
if (attributes != null) {
attributes.forEach(
(key, value) -> {
if (key != null) {
mapWithStringKeys.put(key.getKey(), value);
}
});
}
return mapWithStringKeys;
}
}