Skip to content

Commit db4bef1

Browse files
authored
ref: Pass client instead of hub to isSentryRequestUrl (#9869)
Step by step, eradicating the hub... Note: I haven't found a way to mark an attribute type of a function as deprecated 😬 I think it's OK here, but generally a bit annoying/tricky...
1 parent 12c146b commit db4bef1

File tree

10 files changed

+43
-27
lines changed

10 files changed

+43
-27
lines changed

packages/core/src/utils/isSentryRequestUrl.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import type { DsnComponents, Hub } from '@sentry/types';
1+
import type { Client, DsnComponents, Hub } from '@sentry/types';
22

33
/**
44
* Checks whether given url points to Sentry server
55
* @param url url to verify
6+
*
7+
* TODO(v8): Remove Hub fallback type
68
*/
7-
export function isSentryRequestUrl(url: string, hub: Hub): boolean {
8-
const client = hub.getClient();
9+
export function isSentryRequestUrl(url: string, hubOrClient: Hub | Client | undefined): boolean {
10+
const client = hubOrClient && isHub(hubOrClient) ? hubOrClient.getClient() : hubOrClient;
911
const dsn = client && client.getDsn();
1012
const tunnel = client && client.getOptions().tunnel;
1113

@@ -27,3 +29,7 @@ function checkDsn(url: string, dsn: DsnComponents | undefined): boolean {
2729
function removeTrailingSlash(str: string): string {
2830
return str[str.length - 1] === '/' ? str.slice(0, -1) : str;
2931
}
32+
33+
function isHub(hubOrClient: Hub | Client | undefined): hubOrClient is Hub {
34+
return (hubOrClient as Hub).getClient !== undefined;
35+
}

packages/core/test/lib/utils/isSentryRequestUrl.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Hub } from '@sentry/types';
1+
import type { Client, Hub } from '@sentry/types';
22

33
import { isSentryRequestUrl } from '../../../src';
44

@@ -12,15 +12,21 @@ describe('isSentryRequestUrl', () => {
1212
['http://tunnel:4200/', 'sentry-dsn.com', 'http://tunnel:4200', true],
1313
['http://tunnel:4200/a', 'sentry-dsn.com', 'http://tunnel:4200', false],
1414
])('works with url=%s, dsn=%s, tunnel=%s', (url: string, dsn: string, tunnel: string, expected: boolean) => {
15+
const client = {
16+
getOptions: () => ({ tunnel }),
17+
getDsn: () => ({ host: dsn }),
18+
} as unknown as Client;
19+
1520
const hub = {
1621
getClient: () => {
17-
return {
18-
getOptions: () => ({ tunnel }),
19-
getDsn: () => ({ host: dsn }),
20-
};
22+
return client;
2123
},
2224
} as unknown as Hub;
2325

26+
// Works with hub passed
2427
expect(isSentryRequestUrl(url, hub)).toBe(expected);
28+
29+
// Works with client passed
30+
expect(isSentryRequestUrl(url, client)).toBe(expected);
2531
});
2632
});

packages/integrations/src/httpclient.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub, isSentryRequestUrl } from '@sentry/core';
1+
import { getClient, isSentryRequestUrl } from '@sentry/core';
22
import type {
33
Event as SentryEvent,
44
EventProcessor,
@@ -348,9 +348,7 @@ export class HttpClient implements Integration {
348348
*/
349349
private _shouldCaptureResponse(status: number, url: string): boolean {
350350
return (
351-
this._isInGivenStatusRanges(status) &&
352-
this._isInGivenRequestTargets(url) &&
353-
!isSentryRequestUrl(url, getCurrentHub())
351+
this._isInGivenStatusRanges(status) && this._isInGivenRequestTargets(url) && !isSentryRequestUrl(url, getClient())
354352
);
355353
}
356354

packages/node-experimental/src/integrations/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class Http implements Integration {
102102
return false;
103103
}
104104

105-
if (isSentryRequestUrl(url, getCurrentHub())) {
105+
if (isSentryRequestUrl(url, getClient())) {
106106
return true;
107107
}
108108

packages/node/src/integrations/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ function _createWrappedRequestMethodFactory(
240240
const requestUrl = extractUrl(requestOptions);
241241

242242
// we don't want to record requests to Sentry as either breadcrumbs or spans, so just use the original method
243-
if (isSentryRequestUrl(requestUrl, getCurrentHub())) {
243+
if (isSentryRequestUrl(requestUrl, getClient())) {
244244
return originalRequestMethod.apply(httpModule, requestArgs);
245245
}
246246

packages/node/src/integrations/undici/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { getCurrentHub, getCurrentScope, getDynamicSamplingContextFromClient, isSentryRequestUrl } from '@sentry/core';
1+
import {
2+
getClient,
3+
getCurrentHub,
4+
getCurrentScope,
5+
getDynamicSamplingContextFromClient,
6+
isSentryRequestUrl,
7+
} from '@sentry/core';
28
import type { EventProcessor, Integration, Span } from '@sentry/types';
39
import {
410
LRUMap,
@@ -137,12 +143,12 @@ export class Undici implements Integration {
137143

138144
const stringUrl = request.origin ? request.origin.toString() + request.path : request.path;
139145

140-
if (isSentryRequestUrl(stringUrl, hub) || request.__sentry_span__ !== undefined) {
146+
const client = getClient<NodeClient>();
147+
if (!client) {
141148
return;
142149
}
143150

144-
const client = hub.getClient<NodeClient>();
145-
if (!client) {
151+
if (isSentryRequestUrl(stringUrl, client) || request.__sentry_span__ !== undefined) {
146152
return;
147153
}
148154

@@ -197,7 +203,7 @@ export class Undici implements Integration {
197203

198204
const stringUrl = request.origin ? request.origin.toString() + request.path : request.path;
199205

200-
if (isSentryRequestUrl(stringUrl, hub)) {
206+
if (isSentryRequestUrl(stringUrl, getClient())) {
201207
return;
202208
}
203209

@@ -237,7 +243,7 @@ export class Undici implements Integration {
237243

238244
const stringUrl = request.origin ? request.origin.toString() + request.path : request.path;
239245

240-
if (isSentryRequestUrl(stringUrl, hub)) {
246+
if (isSentryRequestUrl(stringUrl, getClient())) {
241247
return;
242248
}
243249

packages/opentelemetry-node/src/utils/isSentryRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Span as OtelSpan } from '@opentelemetry/sdk-trace-base';
22
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
3-
import { getCurrentHub, isSentryRequestUrl } from '@sentry/core';
3+
import { getClient, isSentryRequestUrl } from '@sentry/core';
44

55
/**
66
*
@@ -16,5 +16,5 @@ export function isSentryRequestSpan(otelSpan: OtelSpan): boolean {
1616
return false;
1717
}
1818

19-
return isSentryRequestUrl(httpUrl.toString(), getCurrentHub());
19+
return isSentryRequestUrl(httpUrl.toString(), getClient());
2020
}

packages/opentelemetry/src/utils/isSentryRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
2-
import { getCurrentHub, isSentryRequestUrl } from '@sentry/core';
2+
import { getClient, isSentryRequestUrl } from '@sentry/core';
33

44
import type { AbstractSpan } from '../types';
55
import { spanHasAttributes } from './spanTypes';
@@ -22,5 +22,5 @@ export function isSentryRequestSpan(span: AbstractSpan): boolean {
2222
return false;
2323
}
2424

25-
return isSentryRequestUrl(httpUrl.toString(), getCurrentHub());
25+
return isSentryRequestUrl(httpUrl.toString(), getClient());
2626
}

packages/replay/src/util/shouldFilterRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub, isSentryRequestUrl } from '@sentry/core';
1+
import { getClient, isSentryRequestUrl } from '@sentry/core';
22

33
import { DEBUG_BUILD } from '../debug-build';
44
import type { ReplayContainer } from '../types';
@@ -13,5 +13,5 @@ export function shouldFilterRequest(replay: ReplayContainer, url: string): boole
1313
return false;
1414
}
1515

16-
return isSentryRequestUrl(url, getCurrentHub());
16+
return isSentryRequestUrl(url, getClient());
1717
}

packages/vercel-edge/src/integrations/wintercg-fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class WinterCGFetch implements Integration {
5454
return;
5555
}
5656

57-
if (isSentryRequestUrl(handlerData.fetchData.url, hub)) {
57+
if (isSentryRequestUrl(handlerData.fetchData.url, getClient())) {
5858
return;
5959
}
6060

0 commit comments

Comments
 (0)