UNPKG

@sentry/core

Version:
270 lines (232 loc) 8.63 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const currentScopes = require('./currentScopes.js'); const semanticAttributes = require('./semanticAttributes.js'); require('./tracing/errors.js'); const is = require('./utils-hoist/is.js'); require('./utils-hoist/debug-build.js'); require('./utils-hoist/logger.js'); require('./debug-build.js'); require('./utils-hoist/time.js'); const hasSpansEnabled = require('./utils/hasSpansEnabled.js'); const spanUtils = require('./utils/spanUtils.js'); const baggage = require('./utils-hoist/baggage.js'); const sentryNonRecordingSpan = require('./tracing/sentryNonRecordingSpan.js'); const spanstatus = require('./tracing/spanstatus.js'); const trace = require('./tracing/trace.js'); const url = require('./utils-hoist/url.js'); const traceData = require('./utils/traceData.js'); /** * Create and track fetch request spans for usage in combination with `addFetchInstrumentationHandler`. * * @returns Span if a span was created, otherwise void. */ function instrumentFetchRequest( handlerData, shouldCreateSpan, shouldAttachHeaders, spans, spanOrigin = 'auto.http.browser', ) { if (!handlerData.fetchData) { return undefined; } const { method, url } = handlerData.fetchData; const shouldCreateSpanResult = hasSpansEnabled.hasSpansEnabled() && shouldCreateSpan(url); if (handlerData.endTimestamp && shouldCreateSpanResult) { const spanId = handlerData.fetchData.__span; if (!spanId) return; const span = spans[spanId]; if (span) { endSpan(span, handlerData); // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete spans[spanId]; } return undefined; } const hasParent = !!spanUtils.getActiveSpan(); const span = shouldCreateSpanResult && hasParent ? trace.startInactiveSpan(getSpanStartOptions(url, method, spanOrigin)) : new sentryNonRecordingSpan.SentryNonRecordingSpan(); handlerData.fetchData.__span = span.spanContext().spanId; spans[span.spanContext().spanId] = span; if (shouldAttachHeaders(handlerData.fetchData.url)) { const request = handlerData.args[0]; const options = handlerData.args[1] || {}; const headers = _addTracingHeadersToFetchRequest( request, options, // If performance is disabled (TWP) or there's no active root span (pageload/navigation/interaction), // we do not want to use the span as base for the trace headers, // which means that the headers will be generated from the scope and the sampling decision is deferred hasSpansEnabled.hasSpansEnabled() && hasParent ? span : undefined, ); if (headers) { // Ensure this is actually set, if no options have been passed previously handlerData.args[1] = options; options.headers = headers; } } const client = currentScopes.getClient(); if (client) { const fetchHint = { input: handlerData.args, response: handlerData.response, startTimestamp: handlerData.startTimestamp, endTimestamp: handlerData.endTimestamp, } ; client.emit('beforeOutgoingRequestSpan', span, fetchHint); } return span; } /** * Adds sentry-trace and baggage headers to the various forms of fetch headers. */ function _addTracingHeadersToFetchRequest( request, fetchOptionsObj , span, ) { const traceHeaders = traceData.getTraceData({ span }); const sentryTrace = traceHeaders['sentry-trace']; const baggage = traceHeaders.baggage; // Nothing to do, when we return undefined here, the original headers will be used if (!sentryTrace) { return undefined; } const headers = fetchOptionsObj.headers || (is.isRequest(request) ? request.headers : undefined); if (!headers) { return { ...traceHeaders }; } else if (isHeaders(headers)) { const newHeaders = new Headers(headers); newHeaders.set('sentry-trace', sentryTrace); if (baggage) { const prevBaggageHeader = newHeaders.get('baggage'); if (prevBaggageHeader) { const prevHeaderStrippedFromSentryBaggage = stripBaggageHeaderOfSentryBaggageValues(prevBaggageHeader); newHeaders.set( 'baggage', // If there are non-sentry entries (i.e. if the stripped string is non-empty/truthy) combine the stripped header and sentry baggage header // otherwise just set the sentry baggage header prevHeaderStrippedFromSentryBaggage ? `${prevHeaderStrippedFromSentryBaggage},${baggage}` : baggage, ); } else { newHeaders.set('baggage', baggage); } } return newHeaders; } else if (Array.isArray(headers)) { const newHeaders = [ ...headers // Remove any existing sentry-trace headers .filter(header => { return !(Array.isArray(header) && header[0] === 'sentry-trace'); }) // Get rid of previous sentry baggage values in baggage header .map(header => { if (Array.isArray(header) && header[0] === 'baggage' && typeof header[1] === 'string') { const [headerName, headerValue, ...rest] = header; return [headerName, stripBaggageHeaderOfSentryBaggageValues(headerValue), ...rest]; } else { return header; } }), // Attach the new sentry-trace header ['sentry-trace', sentryTrace], ]; if (baggage) { // If there are multiple entries with the same key, the browser will merge the values into a single request header. // Its therefore safe to simply push a "baggage" entry, even though there might already be another baggage header. newHeaders.push(['baggage', baggage]); } return newHeaders ; } else { const existingBaggageHeader = 'baggage' in headers ? headers.baggage : undefined; let newBaggageHeaders = []; if (Array.isArray(existingBaggageHeader)) { newBaggageHeaders = existingBaggageHeader .map(headerItem => typeof headerItem === 'string' ? stripBaggageHeaderOfSentryBaggageValues(headerItem) : headerItem, ) .filter(headerItem => headerItem === ''); } else if (existingBaggageHeader) { newBaggageHeaders.push(stripBaggageHeaderOfSentryBaggageValues(existingBaggageHeader)); } if (baggage) { newBaggageHeaders.push(baggage); } return { ...(headers ), 'sentry-trace': sentryTrace, baggage: newBaggageHeaders.length > 0 ? newBaggageHeaders.join(',') : undefined, }; } } function endSpan(span, handlerData) { if (handlerData.response) { spanstatus.setHttpStatus(span, handlerData.response.status); const contentLength = handlerData.response?.headers && handlerData.response.headers.get('content-length'); if (contentLength) { const contentLengthNum = parseInt(contentLength); if (contentLengthNum > 0) { span.setAttribute('http.response_content_length', contentLengthNum); } } } else if (handlerData.error) { span.setStatus({ code: spanstatus.SPAN_STATUS_ERROR, message: 'internal_error' }); } span.end(); } function stripBaggageHeaderOfSentryBaggageValues(baggageHeader) { return ( baggageHeader .split(',') // eslint-disable-next-line @typescript-eslint/no-non-null-assertion .filter(baggageEntry => !baggageEntry.split('=')[0].startsWith(baggage.SENTRY_BAGGAGE_KEY_PREFIX)) .join(',') ); } function isHeaders(headers) { return typeof Headers !== 'undefined' && is.isInstanceOf(headers, Headers); } function getSpanStartOptions( url$1, method, spanOrigin, ) { const parsedUrl = url.parseStringToURLObject(url$1); return { name: parsedUrl ? `${method} ${url.getSanitizedUrlStringFromUrlObject(parsedUrl)}` : method, attributes: getFetchSpanAttributes(url$1, parsedUrl, method, spanOrigin), }; } function getFetchSpanAttributes( url$1, parsedUrl, method, spanOrigin, ) { const attributes = { url: url$1, type: 'fetch', 'http.method': method, [semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: spanOrigin, [semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.client', }; if (parsedUrl) { if (!url.isURLObjectRelative(parsedUrl)) { attributes['http.url'] = parsedUrl.href; attributes['server.address'] = parsedUrl.host; } if (parsedUrl.search) { attributes['http.query'] = parsedUrl.search; } if (parsedUrl.hash) { attributes['http.fragment'] = parsedUrl.hash; } } return attributes; } exports.instrumentFetchRequest = instrumentFetchRequest; //# sourceMappingURL=fetch.js.map