Skip to content

Commit 4db1f19

Browse files
HazATkamilogorek
authored andcommitted
fix: Detect internal sentry requests
1 parent b34e42d commit 4db1f19

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

packages/core/src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class API {
2828
public getStoreEndpointWithUrlEncodedAuth(): string {
2929
const dsn = this._dsnObject;
3030
const auth = {
31-
sentry_key: dsn.user,
31+
sentry_key: dsn.user, // sentry_key is currently used in tracing integration to identify internal sentry requests
3232
sentry_version: SENTRY_API_VERSION,
3333
};
3434
// Auth is intentionally sent as part of query string (NOT as custom HTTP header)

packages/hub/test/span.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ describe('Span', () => {
2323
const from = Span.fromTraceparent('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bbbbbbbbbbbbbbbb-0') as any;
2424
expect(from.sampled).toBeFalsy();
2525
});
26+
27+
test('just sample rate', () => {
28+
const from = Span.fromTraceparent('0') as any;
29+
expect(from._traceId).toHaveLength(32);
30+
expect(from._spanId).toHaveLength(16);
31+
expect(from.sampled).toBeFalsy();
32+
33+
const from2 = Span.fromTraceparent('1') as any;
34+
expect(from2._traceId).toHaveLength(32);
35+
expect(from2._spanId).toHaveLength(16);
36+
expect(from2.sampled).toBeTruthy();
37+
});
2638
});
2739

2840
test('fromTraceparent - invalid', () => {

packages/integrations/src/tracing.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,16 @@ export class Tracing implements Integration {
9393
const url = self._xhrUrl;
9494
const headers = getCurrentHub().traceHeaders();
9595
// tslint:disable-next-line: prefer-for-of
96-
const isWhitelisted = self._options.tracingOrigins.some((origin: string | RegExp) =>
96+
let isWhitelisted = self._options.tracingOrigins.some((origin: string | RegExp) =>
9797
isMatchingPattern(url, origin),
9898
);
9999

100+
if (isMatchingPattern(url, 'sentry_key')) {
101+
// If sentry_key is in the url, it's an internal store request to sentry
102+
// we do not want to add the trace header to store requests
103+
isWhitelisted = false;
104+
}
105+
100106
if (isWhitelisted && this.setRequestHeader) {
101107
Object.keys(headers).forEach(key => {
102108
this.setRequestHeader(key, headers[key]);
@@ -121,30 +127,37 @@ export class Tracing implements Integration {
121127
fill(getGlobalObject<Window>(), 'fetch', function(originalFetch: () => void): () => void {
122128
return function(...args: any[]): void {
123129
// @ts-ignore
124-
const self = getCurrentHub().getIntegration(Tracing);
130+
const hub = getCurrentHub();
131+
const self = hub.getIntegration(Tracing);
125132
if (self && self._options.tracingOrigins) {
126133
const url = args[0] as string;
127134
const options = (args[1] = (args[1] as { [key: string]: any }) || {});
128135

129-
let whiteListed = false;
136+
let isWhitelisted = false;
130137
self._options.tracingOrigins.forEach((whiteListUrl: string | RegExp) => {
131-
if (!whiteListed) {
132-
whiteListed = isMatchingPattern(url, whiteListUrl);
138+
if (!isWhitelisted) {
139+
isWhitelisted = isMatchingPattern(url, whiteListUrl);
133140
}
134141
});
135142

136-
if (whiteListed) {
143+
if (isMatchingPattern(url, 'sentry_key')) {
144+
// If sentry_key is in the url, it's an internal store request to sentry
145+
// we do not want to add the trace header to store requests
146+
isWhitelisted = false;
147+
}
148+
149+
if (isWhitelisted) {
137150
if (options.headers) {
138151
if (Array.isArray(options.headers)) {
139-
options.headers = [...options.headers, ...Object.entries(getCurrentHub().traceHeaders())];
152+
options.headers = [...options.headers, ...Object.entries(hub.traceHeaders())];
140153
} else {
141154
options.headers = {
142155
...options.headers,
143-
...getCurrentHub().traceHeaders(),
156+
...hub.traceHeaders(),
144157
};
145158
}
146159
} else {
147-
options.headers = getCurrentHub().traceHeaders();
160+
options.headers = hub.traceHeaders();
148161
}
149162
}
150163
}

0 commit comments

Comments
 (0)