Skip to content

Commit 13f280b

Browse files
feat(core): Drop dev server spans (#4271)
* Drop dev server spans * Adds changelog * Adds tests * Revert unneeded change * Handle undefined dev server urls Co-authored-by: LucasZF <[email protected]> * Adds tests for the case when the dev server url is undefined * Use startsWith to check url matching * Moves changelog to the unreleased section --------- Co-authored-by: LucasZF <[email protected]>
1 parent 112c4f8 commit 13f280b

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
- Prevents exception capture context from being overwritten by native scope sync ([#4124](https://github.com/getsentry/sentry-react-native/pull/4124))
1414
- Excludes Dev Server and Sentry Dsn requests from Breadcrumbs ([#4240](https://github.com/getsentry/sentry-react-native/pull/4240))
15+
- Skips development server spans ([#4271](https://github.com/getsentry/sentry-react-native/pull/4271))
1516

1617
### Dependencies
1718

packages/core/src/js/tracing/reactnativetracing.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getClient } from '@sentry/core';
44
import type { Client, Event, Integration, StartSpanOptions } from '@sentry/types';
55

66
import { isWeb } from '../utils/environment';
7+
import { getDevServer } from './../integrations/debugsymbolicatorutils';
78
import { addDefaultOpForSpanFrom, defaultIdleOptions } from './span';
89

910
export const INTEGRATION_NAME = 'ReactNativeTracing';
@@ -97,6 +98,25 @@ export const reactNativeTracingIntegration = (
9798
idleTimeoutMs: options.idleTimeoutMs ?? defaultIdleOptions.idleTimeout,
9899
};
99100

101+
const userShouldCreateSpanForRequest = finalOptions.shouldCreateSpanForRequest;
102+
103+
// Drop Dev Server Spans
104+
const devServerUrl = getDevServer()?.url;
105+
const finalShouldCreateSpanForRequest =
106+
devServerUrl === undefined
107+
? userShouldCreateSpanForRequest
108+
: (url: string): boolean => {
109+
if (url.startsWith(devServerUrl)) {
110+
return false;
111+
}
112+
if (userShouldCreateSpanForRequest) {
113+
return userShouldCreateSpanForRequest(url);
114+
}
115+
return true;
116+
};
117+
118+
finalOptions.shouldCreateSpanForRequest = finalShouldCreateSpanForRequest;
119+
100120
const setup = (client: Client): void => {
101121
addDefaultOpForSpanFrom(client);
102122

packages/core/test/tracing/reactnavigation.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { DEFAULT_NAVIGATION_SPAN_NAME } from '../../src/js/tracing/span';
2424
import { RN_GLOBAL_OBJ } from '../../src/js/utils/worldwide';
2525
import { getDefaultTestClientOptions, TestClient } from '../mocks/client';
2626
import { NATIVE } from '../mockWrapper';
27+
import { getDevServer } from './../../src/js/integrations/debugsymbolicatorutils';
2728
import { createMockNavigationAndAttachTo } from './reactnavigationutils';
2829

2930
const dummyRoute = {
@@ -32,6 +33,9 @@ const dummyRoute = {
3233
};
3334

3435
jest.mock('../../src/js/wrapper.ts', () => jest.requireActual('../mockWrapper.ts'));
36+
jest.mock('./../../src/js/integrations/debugsymbolicatorutils', () => ({
37+
getDevServer: jest.fn(),
38+
}));
3539
jest.useFakeTimers({ advanceTimers: true });
3640

3741
class MockNavigationContainer {
@@ -392,6 +396,67 @@ describe('ReactNavigationInstrumentation', () => {
392396
});
393397
});
394398

399+
describe('shouldCreateSpanForRequest', () => {
400+
it('should return false for Dev Server URLs', () => {
401+
const devServerUrl = 'http://localhost:8081';
402+
(getDevServer as jest.Mock).mockReturnValue({ url: devServerUrl });
403+
404+
const rnTracing = reactNativeTracingIntegration();
405+
406+
const result = rnTracing.options.shouldCreateSpanForRequest(devServerUrl);
407+
408+
expect(result).toBe(false);
409+
});
410+
411+
it('should return true for non Dev Server URLs', () => {
412+
const devServerUrl = 'http://localhost:8081';
413+
(getDevServer as jest.Mock).mockReturnValue({ url: devServerUrl });
414+
415+
const rnTracing = reactNativeTracingIntegration();
416+
417+
const result = rnTracing.options.shouldCreateSpanForRequest('http://some-other-url.com');
418+
419+
expect(result).toBe(true);
420+
});
421+
422+
it('should chain the user defined shouldCreateSpanForRequest if defined', () => {
423+
const devServerUrl = 'http://localhost:8081';
424+
(getDevServer as jest.Mock).mockReturnValue({ url: devServerUrl });
425+
426+
const userShouldCreateSpanForRequest = (_url: string): boolean => {
427+
return false;
428+
};
429+
430+
const rnTracing = reactNativeTracingIntegration({ shouldCreateSpanForRequest: userShouldCreateSpanForRequest });
431+
432+
const result = rnTracing.options.shouldCreateSpanForRequest('http://some-other-url.com');
433+
434+
expect(result).toBe(false);
435+
});
436+
437+
it('should handle undefined devServerUrls by using only the user defined shouldCreateSpanForRequest', () => {
438+
(getDevServer as jest.Mock).mockReturnValue({ url: undefined });
439+
440+
const userShouldCreateSpanForRequest = (_url: string): boolean => {
441+
return true;
442+
};
443+
444+
const rnTracing = reactNativeTracingIntegration({ shouldCreateSpanForRequest: userShouldCreateSpanForRequest });
445+
446+
const result = rnTracing.options.shouldCreateSpanForRequest('http://any-url.com');
447+
448+
expect(result).toBe(true);
449+
});
450+
451+
it('should not set the shouldCreateSpanForRequest if not user provided and the devServerUrl is undefined', () => {
452+
(getDevServer as jest.Mock).mockReturnValue({ url: undefined });
453+
454+
const rnTracing = reactNativeTracingIntegration();
455+
456+
expect(rnTracing.options.shouldCreateSpanForRequest).toBe(undefined);
457+
});
458+
});
459+
395460
function setupTestClient(
396461
setupOptions: {
397462
beforeSpanStart?: (options: StartSpanOptions) => StartSpanOptions;

0 commit comments

Comments
 (0)