diff --git a/packages/browser/src/tracing/browserTracingIntegration.ts b/packages/browser/src/tracing/browserTracingIntegration.ts index 9d5421f697cd..d8f6796b5b1b 100644 --- a/packages/browser/src/tracing/browserTracingIntegration.ts +++ b/packages/browser/src/tracing/browserTracingIntegration.ts @@ -398,7 +398,7 @@ export const browserTracingIntegration = ((_options: Partial): void { +export function instrumentOutgoingRequests(client: Client, _options?: Partial): void { const { traceFetch, traceXHR, shouldCreateSpanForRequest, enableHTTPTimings, tracePropagationTargets } = { traceFetch: defaultRequestInstrumentationOptions.traceFetch, traceXHR: defaultRequestInstrumentationOptions.traceXHR, @@ -117,27 +117,24 @@ export function instrumentOutgoingRequests(_options?: Partial shouldAttachHeaders(url, tracePropagationTargets); const spans: Record = {}; - const client = getClient(); if (traceFetch) { - if (client) { - // Keeping track of http requests, whose body payloads resolved later than the intial resolved request - // e.g. streaming using server sent events (SSE) - client.addEventProcessor(event => { - if (event.type === 'transaction' && event.spans) { - event.spans.forEach(span => { - if (span.op === 'http.client') { - const updatedTimestamp = spanIdToEndTimestamp.get(span.span_id); - if (updatedTimestamp) { - span.timestamp = updatedTimestamp / 1000; - spanIdToEndTimestamp.delete(span.span_id); - } + // Keeping track of http requests, whose body payloads resolved later than the intial resolved request + // e.g. streaming using server sent events (SSE) + client.addEventProcessor(event => { + if (event.type === 'transaction' && event.spans) { + event.spans.forEach(span => { + if (span.op === 'http.client') { + const updatedTimestamp = spanIdToEndTimestamp.get(span.span_id); + if (updatedTimestamp) { + span.timestamp = updatedTimestamp / 1000; + spanIdToEndTimestamp.delete(span.span_id); } - }); - } - return event; - }); - } + } + }); + } + return event; + }); addFetchEndInstrumentationHandler(handlerData => { if (handlerData.response) { diff --git a/packages/browser/test/unit/tracing/request.test.ts b/packages/browser/test/unit/tracing/request.test.ts index 47174c7d9016..384d62bf52e2 100644 --- a/packages/browser/test/unit/tracing/request.test.ts +++ b/packages/browser/test/unit/tracing/request.test.ts @@ -1,4 +1,5 @@ import * as browserUtils from '@sentry-internal/browser-utils'; +import type { Client } from '@sentry/types'; import * as utils from '@sentry/utils'; import { WINDOW } from '../../../src/helpers'; @@ -10,16 +11,27 @@ beforeAll(() => { global.Request = {}; }); +class MockClient implements Partial { + public addEventProcessor: () => void; + constructor() { + // Mock addEventProcessor function + this.addEventProcessor = jest.fn(); + } +} + describe('instrumentOutgoingRequests', () => { + let client: Client; + beforeEach(() => { jest.clearAllMocks(); + client = new MockClient() as unknown as Client; }); it('instruments fetch and xhr requests', () => { const addFetchSpy = jest.spyOn(utils, 'addFetchInstrumentationHandler'); const addXhrSpy = jest.spyOn(browserUtils, 'addXhrInstrumentationHandler'); - instrumentOutgoingRequests(); + instrumentOutgoingRequests(client); expect(addFetchSpy).toHaveBeenCalledWith(expect.any(Function)); expect(addXhrSpy).toHaveBeenCalledWith(expect.any(Function)); @@ -28,7 +40,7 @@ describe('instrumentOutgoingRequests', () => { it('does not instrument fetch requests if traceFetch is false', () => { const addFetchSpy = jest.spyOn(utils, 'addFetchInstrumentationHandler'); - instrumentOutgoingRequests({ traceFetch: false }); + instrumentOutgoingRequests(client, { traceFetch: false }); expect(addFetchSpy).not.toHaveBeenCalled(); }); @@ -36,7 +48,7 @@ describe('instrumentOutgoingRequests', () => { it('does not instrument xhr requests if traceXHR is false', () => { const addXhrSpy = jest.spyOn(browserUtils, 'addXhrInstrumentationHandler'); - instrumentOutgoingRequests({ traceXHR: false }); + instrumentOutgoingRequests(client, { traceXHR: false }); expect(addXhrSpy).not.toHaveBeenCalled(); });