MCPcopy Index your code
hub / github.com/getsentry/sentry-javascript / instrumentFetchRequest

Function instrumentFetchRequest

packages/core/src/fetch.ts:71–164  ·  view source on GitHub ↗
(
  handlerData: HandlerDataFetch,
  shouldCreateSpan: (url: string) => boolean,
  shouldAttachHeaders: (url: string) => boolean,
  spans: Record<string, Span>,
  spanOriginOrOptions?: SpanOrigin | InstrumentFetchRequestOptions,
)

Source from the content-addressed store, hash-verified

69 * @returns Span if a span was created, otherwise void.
70 */
71export function instrumentFetchRequest(
72 handlerData: HandlerDataFetch,
73 shouldCreateSpan: (url: string) => boolean,
74 shouldAttachHeaders: (url: string) => boolean,
75 spans: Record<string, Span>,
76 spanOriginOrOptions?: SpanOrigin | InstrumentFetchRequestOptions,
77): Span | undefined {
78 if (!handlerData.fetchData) {
79 return undefined;
80 }
81
82 const { method, url } = handlerData.fetchData;
83
84 const shouldCreateSpanResult = hasSpansEnabled() && shouldCreateSpan(url);
85
86 if (handlerData.endTimestamp) {
87 const spanId = handlerData.fetchData.__span;
88 if (!spanId) return;
89
90 const span = spans[spanId];
91
92 if (span) {
93 // Only end the span and call hooks if we're actually recording
94 if (shouldCreateSpanResult) {
95 endSpan(span, handlerData);
96 _callOnRequestSpanEnd(span, handlerData, spanOriginOrOptions);
97 }
98
99 // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
100 delete spans[spanId];
101 }
102
103 return undefined;
104 }
105
106 // Backwards-compatible with the old signature. Needed to introduce the combined optional parameter
107 // to avoid API breakage for anyone calling this function with the optional spanOrigin parameter
108 // TODO (v11): remove this backwards-compatible code and only accept the options parameter
109 const { spanOrigin = 'auto.http.browser', propagateTraceparent = false } =
110 typeof spanOriginOrOptions === 'object' ? spanOriginOrOptions : { spanOrigin: spanOriginOrOptions };
111
112 const client = getClient();
113 const hasParent = !!getActiveSpan();
114 // With span streaming, we always emit http.client spans, even without a parent span
115 const shouldEmitSpan = hasParent || (!!client && hasSpanStreamingEnabled(client));
116
117 const span =
118 shouldCreateSpanResult && shouldEmitSpan
119 ? startInactiveSpan(getSpanStartOptions(url, method, spanOrigin))
120 : new SentryNonRecordingSpan();
121
122 if (shouldCreateSpanResult && !shouldEmitSpan) {
123 client?.recordDroppedEvent('no_parent_span', 'span');
124 }
125
126 handlerData.fetchData.__span = span.spanContext().spanId;
127 spans[span.spanContext().spanId] = span;
128

Callers 4

fetch.test.tsFile · 0.90
setupOnceFunction · 0.90
setupOnceFunction · 0.90

Calls 12

hasSpansEnabledFunction · 0.90
getClientFunction · 0.90
getActiveSpanFunction · 0.90
hasSpanStreamingEnabledFunction · 0.90
startInactiveSpanFunction · 0.90
_callOnRequestSpanEndFunction · 0.85
getSpanStartOptionsFunction · 0.85
shouldAttachHeadersFunction · 0.85
endSpanFunction · 0.70
spanContextMethod · 0.65
emitMethod · 0.65

Tested by

no test coverage detected