MCPcopy Create free account
hub / github.com/getsentry/sentry-javascript / setup

Function setup

packages/browser/src/integrations/spanstreaming.ts:27–69  ·  view source on GitHub ↗
(client)

Source from the content-addressed store, hash-verified

25 },
26
27 setup(client) {
28 const initialMessage = 'SpanStreaming integration requires';
29 const fallbackMsg = 'Falling back to static trace lifecycle.';
30 const clientOptions = client.getOptions();
31
32 if (!hasSpanStreamingEnabled(client)) {
33 clientOptions.traceLifecycle = 'static';
34 DEBUG_BUILD && debug.warn(`${initialMessage} \`traceLifecycle\` to be set to "stream"! ${fallbackMsg}`);
35 return;
36 }
37
38 const beforeSendSpan = clientOptions.beforeSendSpan;
39 // If users misconfigure their SDK by opting into span streaming but
40 // using an incompatible beforeSendSpan callback, we fall back to the static trace lifecycle.
41 if (beforeSendSpan && !isStreamedBeforeSendSpanCallback(beforeSendSpan)) {
42 clientOptions.traceLifecycle = 'static';
43 DEBUG_BUILD &&
44 debug.warn(`${initialMessage} a beforeSendSpan callback using \`withStreamedSpan\`! ${fallbackMsg}`);
45 return;
46 }
47
48 const buffer = new SpanBuffer(client);
49
50 client.on('afterSpanEnd', span => {
51 // Negatively sampled spans must not be captured.
52 // This happens because OTel and we create non-recording spans for negatively sampled spans
53 // that go through the same life cycle as recording spans.
54 if (!spanIsSampled(span)) {
55 return;
56 }
57 buffer.add(captureSpan(span, client));
58 });
59
60 // In addition to capturing the span, we also flush the trace when the segment
61 // span ends to ensure things are sent timely. We never know when the browser
62 // is closed, users navigate away, etc.
63 client.on('afterSegmentSpanEnd', segmentSpan => {
64 const traceId = segmentSpan.spanContext().traceId;
65 setTimeout(() => {
66 buffer.flush(traceId);
67 }, 500);
68 });
69 },
70 };
71}) satisfies IntegrationFn;

Callers

nothing calls this directly

Calls 11

addMethod · 0.95
flushMethod · 0.95
hasSpanStreamingEnabledFunction · 0.85
spanIsSampledFunction · 0.85
captureSpanFunction · 0.85
setTimeoutFunction · 0.85
getOptionsMethod · 0.65
warnMethod · 0.65
onMethod · 0.65
spanContextMethod · 0.65

Tested by

no test coverage detected