/
spanprocessor.ts
166 lines (140 loc) · 5.33 KB
/
spanprocessor.ts
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
import { Context } from '@opentelemetry/api';
import { Span as OtelSpan, SpanProcessor as OtelSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { getCurrentHub, withScope } from '@sentry/core';
import { Transaction } from '@sentry/tracing';
import { Span as SentrySpan, TransactionContext } from '@sentry/types';
import { logger } from '@sentry/utils';
import { isSentryRequestSpan } from './utils/is-sentry-request';
import { mapOtelStatus } from './utils/map-otel-status';
import { parseSpanDescription } from './utils/parse-otel-span-description';
export const SENTRY_SPAN_PROCESSOR_MAP: Map<SentrySpan['spanId'], SentrySpan> = new Map<
SentrySpan['spanId'],
SentrySpan
>();
/**
* Converts OpenTelemetry Spans to Sentry Spans and sends them to Sentry via
* the Sentry SDK.
*/
export class SentrySpanProcessor implements OtelSpanProcessor {
/**
* @inheritDoc
*/
public onStart(otelSpan: OtelSpan, _parentContext: Context): void {
const hub = getCurrentHub();
if (!hub) {
__DEBUG_BUILD__ && logger.error('SentrySpanProcessor has triggered onStart before a hub has been setup.');
return;
}
const scope = hub.getScope();
if (!scope) {
__DEBUG_BUILD__ && logger.error('SentrySpanProcessor has triggered onStart before a scope has been setup.');
return;
}
const otelSpanId = otelSpan.spanContext().spanId;
const otelParentSpanId = otelSpan.parentSpanId;
// Otel supports having multiple non-nested spans at the same time
// so we cannot use hub.getSpan(), as we cannot rely on this being on the current span
const sentryParentSpan = otelParentSpanId && SENTRY_SPAN_PROCESSOR_MAP.get(otelParentSpanId);
if (sentryParentSpan) {
const sentryChildSpan = sentryParentSpan.startChild({
description: otelSpan.name,
// instrumentor: 'otel',
startTimestamp: convertOtelTimeToSeconds(otelSpan.startTime),
spanId: otelSpanId,
});
SENTRY_SPAN_PROCESSOR_MAP.set(otelSpanId, sentryChildSpan);
} else {
const traceCtx = getTraceData(otelSpan);
const transaction = hub.startTransaction({
name: otelSpan.name,
...traceCtx,
// instrumentor: 'otel',
startTimestamp: convertOtelTimeToSeconds(otelSpan.startTime),
spanId: otelSpanId,
});
SENTRY_SPAN_PROCESSOR_MAP.set(otelSpanId, transaction);
}
}
/**
* @inheritDoc
*/
public onEnd(otelSpan: OtelSpan): void {
const otelSpanId = otelSpan.spanContext().spanId;
const sentrySpan = SENTRY_SPAN_PROCESSOR_MAP.get(otelSpanId);
if (!sentrySpan) {
__DEBUG_BUILD__ &&
logger.error(`SentrySpanProcessor could not find span with OTEL-spanId ${otelSpanId} to finish.`);
return;
}
// Auto-instrumentation often captures outgoing HTTP requests
// This means that Sentry HTTP requests created by this integration can, in turn, be captured by OTEL auto instrumentation,
// leading to an infinite loop.
// In this case, we do not want to finish the span, in order to avoid sending it to Sentry
if (isSentryRequestSpan(otelSpan)) {
// Make sure to remove any references, so this can be GCed
SENTRY_SPAN_PROCESSOR_MAP.delete(otelSpanId);
return;
}
if (sentrySpan instanceof Transaction) {
updateTransactionWithOtelData(sentrySpan, otelSpan);
finishTransactionWithContextFromOtelData(sentrySpan, otelSpan);
} else {
updateSpanWithOtelData(sentrySpan, otelSpan);
sentrySpan.finish(convertOtelTimeToSeconds(otelSpan.endTime));
}
SENTRY_SPAN_PROCESSOR_MAP.delete(otelSpanId);
}
/**
* @inheritDoc
*/
public shutdown(): Promise<void> {
return Promise.resolve();
}
/**
* @inheritDoc
*/
public async forceFlush(): Promise<void> {
const client = getCurrentHub().getClient();
if (client) {
return client.flush().then();
}
return Promise.resolve();
}
}
function getTraceData(otelSpan: OtelSpan): Partial<TransactionContext> {
const spanContext = otelSpan.spanContext();
const traceId = spanContext.traceId;
const spanId = spanContext.spanId;
const parentSpanId = otelSpan.parentSpanId;
return { spanId, traceId, parentSpanId };
}
function finishTransactionWithContextFromOtelData(transaction: Transaction, otelSpan: OtelSpan): void {
withScope(scope => {
scope.setContext('otel', {
attributes: otelSpan.attributes,
resource: otelSpan.resource.attributes,
});
transaction.finish(convertOtelTimeToSeconds(otelSpan.endTime));
});
}
function updateSpanWithOtelData(sentrySpan: SentrySpan, otelSpan: OtelSpan): void {
const { attributes, kind } = otelSpan;
sentrySpan.setStatus(mapOtelStatus(otelSpan));
sentrySpan.setData('otel.kind', kind.valueOf());
Object.keys(attributes).forEach(prop => {
const value = attributes[prop];
sentrySpan.setData(prop, value);
});
const { op, description } = parseSpanDescription(otelSpan);
sentrySpan.op = op;
sentrySpan.description = description;
}
function updateTransactionWithOtelData(transaction: Transaction, otelSpan: OtelSpan): void {
transaction.setStatus(mapOtelStatus(otelSpan));
const { op, description } = parseSpanDescription(otelSpan);
transaction.op = op;
transaction.name = description;
}
function convertOtelTimeToSeconds([seconds, nano]: [number, number]): number {
return seconds + nano / 1_000_000_000;
}