/
spanprocessor.ts
115 lines (97 loc) · 3.01 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
import { Context } from '@opentelemetry/api';
import { Span as OtelSpan, SpanProcessor as OtelSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { getCurrentHub } from '@sentry/core';
import { Span as SentrySpan, TransactionContext } from '@sentry/types';
/**
* Converts OpenTelemetry Spans to Sentry Spans and sends them to Sentry via
* the Sentry SDK.
*/
export class SentrySpanProcessor implements OtelSpanProcessor {
private readonly _map: Record<SentrySpan['spanId'], [SentrySpan, SentrySpan | undefined]> = {};
/**
* @inheritDoc
*/
public onStart(otelSpan: OtelSpan, _parentContext: Context): void {
const hub = getCurrentHub();
if (!hub) {
return;
}
const scope = hub.getScope();
if (!scope) {
return;
}
// if isSentryRequest(otelSpan) return;
const otelSpanId = otelSpan.spanContext().spanId;
const sentryParentSpan = scope.getSpan();
if (sentryParentSpan) {
const sentryChildSpan = sentryParentSpan.startChild({
description: otelSpan.name,
// instrumentor: 'otel',
startTimestamp: otelSpan.startTime[0],
});
sentryChildSpan.spanId = otelSpanId;
console.log(sentryParentSpan, sentryChildSpan, otelSpan);
this._map[otelSpanId] = [sentryChildSpan, sentryParentSpan];
scope.setSpan(sentryChildSpan);
} else {
const traceCtx = getTraceData(otelSpan);
const transaction = hub.startTransaction({
name: otelSpan.name,
...traceCtx,
// instrumentor: 'otel',
startTimestamp: otelSpan.startTime[0],
});
transaction.spanId = otelSpanId;
this._map[otelSpanId] = [transaction, undefined];
scope.setSpan(transaction);
}
}
/**
* @inheritDoc
*/
public onEnd(otelSpan: OtelSpan): void {
const hub = getCurrentHub();
if (!hub) {
return;
}
const scope = hub.getScope();
if (!scope) {
return;
}
const otelSpanId = otelSpan.spanContext().spanId;
const mapVal = this._map[otelSpanId];
if (mapVal) {
const [sentrySpan, sentryParentSpan] = mapVal;
// updateSpanWithOtelData(sentrySpan, otelSpan);
sentrySpan.finish(otelSpan.endTime[0]);
scope.setSpan(sentryParentSpan);
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this._map[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 updateSpanWithOtelData(sentrySpan: SentrySpan, otelSpan: OtelSpan): void {
// }