-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathspanStreaming.ts
More file actions
44 lines (38 loc) · 1.65 KB
/
Copy pathspanStreaming.ts
File metadata and controls
44 lines (38 loc) · 1.65 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
import type { IntegrationFn } from '../types-hoist/integration';
import { DEBUG_BUILD } from '../debug-build';
import { defineIntegration } from '../integration';
import { isStreamedBeforeSendSpanCallback } from '../tracing/spans/beforeSendSpan';
import { captureSpan } from '../tracing/spans/captureSpan';
import { hasSpanStreamingEnabled } from '../tracing/spans/hasSpanStreamingEnabled';
import { SpanBuffer } from '../tracing/spans/spanBuffer';
import { debug } from '../utils/debug-logger';
import { spanIsSampled } from '../utils/spanUtils';
export const spanStreamingIntegration = defineIntegration(() => {
return {
name: 'SpanStreaming',
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 (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 => {
if (!spanIsSampled(span)) {
return;
}
buffer.add(captureSpan(span, client));
});
},
};
}) satisfies IntegrationFn;