diff --git a/packages/core/src/utils/isSentryRequestUrl.ts b/packages/core/src/utils/isSentryRequestUrl.ts index 0256e3cf7835..3a31f63cf46c 100644 --- a/packages/core/src/utils/isSentryRequestUrl.ts +++ b/packages/core/src/utils/isSentryRequestUrl.ts @@ -1,11 +1,13 @@ -import type { DsnComponents, Hub } from '@sentry/types'; +import type { Client, DsnComponents, Hub } from '@sentry/types'; /** * Checks whether given url points to Sentry server * @param url url to verify + * + * TODO(v8): Remove Hub fallback type */ -export function isSentryRequestUrl(url: string, hub: Hub): boolean { - const client = hub.getClient(); +export function isSentryRequestUrl(url: string, hubOrClient: Hub | Client | undefined): boolean { + const client = hubOrClient && isHub(hubOrClient) ? hubOrClient.getClient() : hubOrClient; const dsn = client && client.getDsn(); const tunnel = client && client.getOptions().tunnel; @@ -27,3 +29,7 @@ function checkDsn(url: string, dsn: DsnComponents | undefined): boolean { function removeTrailingSlash(str: string): string { return str[str.length - 1] === '/' ? str.slice(0, -1) : str; } + +function isHub(hubOrClient: Hub | Client | undefined): hubOrClient is Hub { + return (hubOrClient as Hub).getClient !== undefined; +} diff --git a/packages/core/test/lib/utils/isSentryRequestUrl.test.ts b/packages/core/test/lib/utils/isSentryRequestUrl.test.ts index b1671b9410e8..98fd7e54207b 100644 --- a/packages/core/test/lib/utils/isSentryRequestUrl.test.ts +++ b/packages/core/test/lib/utils/isSentryRequestUrl.test.ts @@ -1,4 +1,4 @@ -import type { Hub } from '@sentry/types'; +import type { Client, Hub } from '@sentry/types'; import { isSentryRequestUrl } from '../../../src'; @@ -12,15 +12,21 @@ describe('isSentryRequestUrl', () => { ['http://tunnel:4200/', 'sentry-dsn.com', 'http://tunnel:4200', true], ['http://tunnel:4200/a', 'sentry-dsn.com', 'http://tunnel:4200', false], ])('works with url=%s, dsn=%s, tunnel=%s', (url: string, dsn: string, tunnel: string, expected: boolean) => { + const client = { + getOptions: () => ({ tunnel }), + getDsn: () => ({ host: dsn }), + } as unknown as Client; + const hub = { getClient: () => { - return { - getOptions: () => ({ tunnel }), - getDsn: () => ({ host: dsn }), - }; + return client; }, } as unknown as Hub; + // Works with hub passed expect(isSentryRequestUrl(url, hub)).toBe(expected); + + // Works with client passed + expect(isSentryRequestUrl(url, client)).toBe(expected); }); }); diff --git a/packages/integrations/src/httpclient.ts b/packages/integrations/src/httpclient.ts index 1e1ee0318861..c03cd63e6840 100644 --- a/packages/integrations/src/httpclient.ts +++ b/packages/integrations/src/httpclient.ts @@ -1,4 +1,4 @@ -import { getCurrentHub, isSentryRequestUrl } from '@sentry/core'; +import { getClient, isSentryRequestUrl } from '@sentry/core'; import type { Event as SentryEvent, EventProcessor, @@ -348,9 +348,7 @@ export class HttpClient implements Integration { */ private _shouldCaptureResponse(status: number, url: string): boolean { return ( - this._isInGivenStatusRanges(status) && - this._isInGivenRequestTargets(url) && - !isSentryRequestUrl(url, getCurrentHub()) + this._isInGivenStatusRanges(status) && this._isInGivenRequestTargets(url) && !isSentryRequestUrl(url, getClient()) ); } diff --git a/packages/node-experimental/src/integrations/http.ts b/packages/node-experimental/src/integrations/http.ts index 974828fd46fe..6fe99e90101c 100644 --- a/packages/node-experimental/src/integrations/http.ts +++ b/packages/node-experimental/src/integrations/http.ts @@ -102,7 +102,7 @@ export class Http implements Integration { return false; } - if (isSentryRequestUrl(url, getCurrentHub())) { + if (isSentryRequestUrl(url, getClient())) { return true; } diff --git a/packages/node/src/integrations/http.ts b/packages/node/src/integrations/http.ts index b61d34574457..94aa36b80901 100644 --- a/packages/node/src/integrations/http.ts +++ b/packages/node/src/integrations/http.ts @@ -240,7 +240,7 @@ function _createWrappedRequestMethodFactory( const requestUrl = extractUrl(requestOptions); // we don't want to record requests to Sentry as either breadcrumbs or spans, so just use the original method - if (isSentryRequestUrl(requestUrl, getCurrentHub())) { + if (isSentryRequestUrl(requestUrl, getClient())) { return originalRequestMethod.apply(httpModule, requestArgs); } diff --git a/packages/node/src/integrations/undici/index.ts b/packages/node/src/integrations/undici/index.ts index 7681a26ba7ca..b260f26e192d 100644 --- a/packages/node/src/integrations/undici/index.ts +++ b/packages/node/src/integrations/undici/index.ts @@ -1,4 +1,10 @@ -import { getCurrentHub, getCurrentScope, getDynamicSamplingContextFromClient, isSentryRequestUrl } from '@sentry/core'; +import { + getClient, + getCurrentHub, + getCurrentScope, + getDynamicSamplingContextFromClient, + isSentryRequestUrl, +} from '@sentry/core'; import type { EventProcessor, Integration, Span } from '@sentry/types'; import { LRUMap, @@ -137,12 +143,12 @@ export class Undici implements Integration { const stringUrl = request.origin ? request.origin.toString() + request.path : request.path; - if (isSentryRequestUrl(stringUrl, hub) || request.__sentry_span__ !== undefined) { + const client = getClient(); + if (!client) { return; } - const client = hub.getClient(); - if (!client) { + if (isSentryRequestUrl(stringUrl, client) || request.__sentry_span__ !== undefined) { return; } @@ -197,7 +203,7 @@ export class Undici implements Integration { const stringUrl = request.origin ? request.origin.toString() + request.path : request.path; - if (isSentryRequestUrl(stringUrl, hub)) { + if (isSentryRequestUrl(stringUrl, getClient())) { return; } @@ -237,7 +243,7 @@ export class Undici implements Integration { const stringUrl = request.origin ? request.origin.toString() + request.path : request.path; - if (isSentryRequestUrl(stringUrl, hub)) { + if (isSentryRequestUrl(stringUrl, getClient())) { return; } diff --git a/packages/opentelemetry-node/src/utils/isSentryRequest.ts b/packages/opentelemetry-node/src/utils/isSentryRequest.ts index 5b285bb0ec68..85cb6c9c77b9 100644 --- a/packages/opentelemetry-node/src/utils/isSentryRequest.ts +++ b/packages/opentelemetry-node/src/utils/isSentryRequest.ts @@ -1,6 +1,6 @@ import type { Span as OtelSpan } from '@opentelemetry/sdk-trace-base'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; -import { getCurrentHub, isSentryRequestUrl } from '@sentry/core'; +import { getClient, isSentryRequestUrl } from '@sentry/core'; /** * @@ -16,5 +16,5 @@ export function isSentryRequestSpan(otelSpan: OtelSpan): boolean { return false; } - return isSentryRequestUrl(httpUrl.toString(), getCurrentHub()); + return isSentryRequestUrl(httpUrl.toString(), getClient()); } diff --git a/packages/opentelemetry/src/utils/isSentryRequest.ts b/packages/opentelemetry/src/utils/isSentryRequest.ts index 361cc89d0ad7..7d146d551e12 100644 --- a/packages/opentelemetry/src/utils/isSentryRequest.ts +++ b/packages/opentelemetry/src/utils/isSentryRequest.ts @@ -1,5 +1,5 @@ import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; -import { getCurrentHub, isSentryRequestUrl } from '@sentry/core'; +import { getClient, isSentryRequestUrl } from '@sentry/core'; import type { AbstractSpan } from '../types'; import { spanHasAttributes } from './spanTypes'; @@ -22,5 +22,5 @@ export function isSentryRequestSpan(span: AbstractSpan): boolean { return false; } - return isSentryRequestUrl(httpUrl.toString(), getCurrentHub()); + return isSentryRequestUrl(httpUrl.toString(), getClient()); } diff --git a/packages/replay/src/util/shouldFilterRequest.ts b/packages/replay/src/util/shouldFilterRequest.ts index 274ad4ee488a..71a268be8d64 100644 --- a/packages/replay/src/util/shouldFilterRequest.ts +++ b/packages/replay/src/util/shouldFilterRequest.ts @@ -1,4 +1,4 @@ -import { getCurrentHub, isSentryRequestUrl } from '@sentry/core'; +import { getClient, isSentryRequestUrl } from '@sentry/core'; import { DEBUG_BUILD } from '../debug-build'; import type { ReplayContainer } from '../types'; @@ -13,5 +13,5 @@ export function shouldFilterRequest(replay: ReplayContainer, url: string): boole return false; } - return isSentryRequestUrl(url, getCurrentHub()); + return isSentryRequestUrl(url, getClient()); } diff --git a/packages/vercel-edge/src/integrations/wintercg-fetch.ts b/packages/vercel-edge/src/integrations/wintercg-fetch.ts index 7c75308c72fe..b03cae819073 100644 --- a/packages/vercel-edge/src/integrations/wintercg-fetch.ts +++ b/packages/vercel-edge/src/integrations/wintercg-fetch.ts @@ -54,7 +54,7 @@ export class WinterCGFetch implements Integration { return; } - if (isSentryRequestUrl(handlerData.fetchData.url, hub)) { + if (isSentryRequestUrl(handlerData.fetchData.url, getClient())) { return; }