-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathspanstreaming.ts
More file actions
71 lines (63 loc) · 2.64 KB
/
spanstreaming.ts
File metadata and controls
71 lines (63 loc) · 2.64 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
import type { IntegrationFn } from '@sentry/core';
import {
captureSpan,
debug,
defineIntegration,
hasSpanStreamingEnabled,
isStreamedBeforeSendSpanCallback,
SpanBuffer,
spanIsSampled,
} from '@sentry/core';
import { DEBUG_BUILD } from '../debug-build';
export const spanStreamingIntegration = defineIntegration(() => {
return {
name: 'SpanStreaming',
beforeSetup(client) {
// If users only set spanStreamingIntegration, without traceLifecycle, we set it to "stream" for them.
// This avoids the classic double-opt-in problem we'd otherwise have in the browser SDK.
const clientOptions = client.getOptions();
if (!clientOptions.traceLifecycle) {
DEBUG_BUILD && debug.warn('[SpanStreaming] set `traceLifecycle` to "stream"');
clientOptions.traceLifecycle = 'stream';
}
},
setup(client) {
const initialMessage = 'SpanStreaming integration requires';
const fallbackMsg = 'Falling back to static trace lifecycle.';
const clientOptions = client.getOptions();
if (!hasSpanStreamingEnabled(client)) {
clientOptions.traceLifecycle = 'static';
DEBUG_BUILD && debug.warn(`${initialMessage} \`traceLifecycle\` to be set to "stream"! ${fallbackMsg}`);
return;
}
const beforeSendSpan = clientOptions.beforeSendSpan;
// If users misconfigure their SDK by opting into span streaming but
// using an incompatible beforeSendSpan callback, we fall back to the static trace lifecycle.
if (beforeSendSpan && !isStreamedBeforeSendSpanCallback(beforeSendSpan)) {
clientOptions.traceLifecycle = 'static';
DEBUG_BUILD &&
debug.warn(`${initialMessage} a beforeSendSpan callback using \`withStreamedSpan\`! ${fallbackMsg}`);
return;
}
const buffer = new SpanBuffer(client);
client.on('afterSpanEnd', span => {
// Negatively sampled spans must not be captured.
// This happens because OTel and we create non-recording spans for negatively sampled spans
// that go through the same life cycle as recording spans.
if (!spanIsSampled(span)) {
return;
}
buffer.add(captureSpan(span, client));
});
// In addition to capturing the span, we also flush the trace when the segment
// span ends to ensure things are sent timely. We never know when the browser
// is closed, users navigate away, etc.
client.on('afterSegmentSpanEnd', segmentSpan => {
const traceId = segmentSpan.spanContext().traceId;
setTimeout(() => {
buffer.flush(traceId);
}, 500);
});
},
};
}) satisfies IntegrationFn;