Skip to content

Commit

Permalink
feat: tracePropagationTargets defaults to all targets on mobile and…
Browse files Browse the repository at this point in the history
… same origin on the web
  • Loading branch information
krystofwoldrich committed Sep 12, 2024
1 parent 1ba1b31 commit 87ee67e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

### Changes

- `tracePropagationTargets` defaults to all targets on mobile and same origin on the web ([#4083](https://github.com/getsentry/sentry-react-native/pull/4083))
- Move `_experiments.profilesSampleRate` to `profilesSampleRate` root options object [#3851](https://github.com/getsentry/sentry-react-native/pull/3851))
- Add Android Logger when new frame event is not emitted ([#4081](https://github.com/getsentry/sentry-react-native/pull/4081))
- React Native Tracing Deprecations ([#4073](https://github.com/getsentry/sentry-react-native/pull/4073))
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/js/tracing/reactnativetracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { instrumentOutgoingRequests } from '@sentry/browser';
import { getClient } from '@sentry/core';
import type { Client, Event, Integration, StartSpanOptions } from '@sentry/types';

import { isWeb } from '../utils/environment';
import { addDefaultOpForSpanFrom, defaultIdleOptions } from './span';

export const INTEGRATION_NAME = 'ReactNativeTracing';
Expand Down Expand Up @@ -60,7 +61,12 @@ export interface ReactNativeTracingOptions {
shouldCreateSpanForRequest?(this: void, url: string): boolean;
}

const DEFAULT_TRACE_PROPAGATION_TARGETS = ['localhost', /^\/(?!\/)/];
function getDefaultTracePropagationTargets(): RegExp[] | undefined {
if (isWeb()) {
return undefined;
}
return [/.*/];
}

export const defaultReactNativeTracingOptions: ReactNativeTracingOptions = {
traceFetch: true,
Expand Down Expand Up @@ -98,7 +104,7 @@ export const reactNativeTracingIntegration = (
traceFetch: finalOptions.traceFetch,
traceXHR: finalOptions.traceXHR,
shouldCreateSpanForRequest: finalOptions.shouldCreateSpanForRequest,
tracePropagationTargets: client.getOptions().tracePropagationTargets || DEFAULT_TRACE_PROPAGATION_TARGETS,
tracePropagationTargets: client.getOptions().tracePropagationTargets || getDefaultTracePropagationTargets(),
});
};

Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/js/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export function getExpoSdkVersion(): string | undefined {
return expoSdkVersion;
}

/** Checks if the current platform is web */
export function isWeb(): boolean {
return Platform.OS === 'web';
}

/** Checks if the current platform is not web */
export function notWeb(): boolean {
return Platform.OS !== 'web';
Expand Down
41 changes: 30 additions & 11 deletions packages/core/test/tracing/reactnativetracing.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
jest.mock('@sentry/utils', () => {
const originalUtils = jest.requireActual('@sentry/utils');

return {
...originalUtils,
timestampInSeconds: jest.fn(originalUtils.timestampInSeconds),
};
});

import * as SentryBrowser from '@sentry/browser';
import type { Event } from '@sentry/types';

Expand All @@ -31,7 +22,18 @@ jest.mock('../../src/js/tracing/utils', () => {
};
});

jest.mock('@sentry/utils', () => {
const originalUtils = jest.requireActual('@sentry/utils');

return {
...originalUtils,
timestampInSeconds: jest.fn(originalUtils.timestampInSeconds),
};
});

jest.mock('../../src/js/utils/environment');
import { reactNativeTracingIntegration } from '../../src/js/tracing/reactnativetracing';
import { isWeb } from '../../src/js/utils/environment';
import type { TestClient } from '../mocks/client';
import { setupTestClient } from '../mocks/client';

Expand Down Expand Up @@ -66,7 +68,24 @@ describe('ReactNativeTracing', () => {
);
});

it('uses defaults', () => {
it('uses mobile defaults', () => {
(isWeb as jest.MockedFunction<typeof isWeb>).mockReturnValue(false);
const instrumentOutgoingRequests = jest.spyOn(SentryBrowser, 'instrumentOutgoingRequests');
setupTestClient({
enableStallTracking: false,
integrations: [reactNativeTracingIntegration()],
});

expect(instrumentOutgoingRequests).toBeCalledWith(
expect.anything(),
expect.objectContaining({
tracePropagationTargets: [/.*/],
}),
);
});

it('uses web defaults', () => {
(isWeb as jest.MockedFunction<typeof isWeb>).mockReturnValue(true);
const instrumentOutgoingRequests = jest.spyOn(SentryBrowser, 'instrumentOutgoingRequests');
setupTestClient({
enableStallTracking: false,
Expand All @@ -76,7 +95,7 @@ describe('ReactNativeTracing', () => {
expect(instrumentOutgoingRequests).toBeCalledWith(
expect.anything(),
expect.objectContaining({
tracePropagationTargets: ['localhost', /^\/(?!\/)/],
tracePropagationTargets: undefined,
}),
);
});
Expand Down

0 comments on commit 87ee67e

Please sign in to comment.