( handlerData: HandlerDataFetch, shouldCreateSpan: (url: string) => boolean, shouldAttachHeaders: (url: string) => boolean, spans: Record<string, Span>, spanOriginOrOptions?: SpanOrigin | InstrumentFetchRequestOptions, )
| 69 | * @returns Span if a span was created, otherwise void. |
| 70 | */ |
| 71 | export 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 |
no test coverage detected