Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/core/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,22 @@ export function addIntegration(integration: Integration): void {
/**
* Define an integration function that can be used to create an integration instance.
* Note that this by design hides the implementation details of the integration, as they are considered internal.
*
* `config.libraryOptions` names the options that take functions from a library, such as router hooks, rather than
* user code.
*/
/*#__NO_SIDE_EFFECTS__*/
export function defineIntegration<Fn extends IntegrationFn>(
fn: Fn,
config?: { libraryOptions?: ReadonlyArray<keyof NonNullable<Parameters<Fn>[0]> & string> },
): (...args: Parameters<Fn>) => Integration & { name: ReturnType<Fn>['name'] } {
return (...args: Parameters<Fn>) => {
// Copied before the integration runs, so its writes into the object it was given aren't reported as the user's.
const options: unknown = isPlainObject(args[0]) ? { ...args[0] } : args[0];
const integration = fn(...args) as ReturnType<Fn>;
// Always set, since a wrapper can return an inner integration that holds the options the wrapper forced.
integration._options = options;
integration._libraryOptions = config?.libraryOptions;

return integration;
};
Expand Down
25 changes: 21 additions & 4 deletions packages/core/src/integrations/reportSdkConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ function buildConfigAttributes(client: Client): Record<string, string> {
options.integrations = integrationsSetByUser.map(name => {
const integration: (Integration & { isDefaultInstance?: true }) | undefined = client.getIntegrationByName(name);
// A default instance kept by an `integrations` function got its options from the SDK, not the user.
return describeIntegration(name, integration?.isDefaultInstance ? undefined : integration?._options);
return describeIntegration(
name,
integration?.isDefaultInstance ? undefined : integration?._options,
integration?._libraryOptions,
);
});
}

Expand All @@ -134,7 +138,7 @@ function buildConfigAttributes(client: Client): Record<string, string> {
if (Array.isArray(options.defaultIntegrations)) {
// `getIntegrationsToSetup` flags these as default instances, but the user passed them.
options.defaultIntegrations = options.defaultIntegrations.map((integration: Integration) =>
describeIntegration(integration.name, integration._options),
describeIntegration(integration.name, integration._options, integration._libraryOptions),
);
}

Expand Down Expand Up @@ -173,7 +177,11 @@ function describeShape(value: unknown, ancestors = new Set<object>()): unknown {
return shape;
}

function describeIntegration(name: string, options: unknown): { name: string; options?: unknown } {
function describeIntegration(
name: string,
options: unknown,
libraryOptions: readonly string[] = [],
): { name: string; options?: unknown } {
if (options === undefined) {
return { name };
}
Expand All @@ -190,12 +198,21 @@ function describeIntegration(name: string, options: unknown): { name: string; op

const described: Record<string, unknown> = {};
for (const key of keys) {
described[key] = describeOptionValue(options[key]);
const value = options[key];
described[key] = libraryOptions.includes(key) ? describeLibraryValue(value) : describeOptionValue(value);
}

return { name, options: described };
}

/**
* Library functions and objects change with the library build and the engine, not with the config, so only their kind
* is reported. Primitives are stable, so they are kept.
*/
function describeLibraryValue(value: unknown): unknown {
return typeof value === 'function' ? '[Function]' : redactObject(value);
}

// Options can hold live objects such as a router, a store or an SDK client, which carry URLs, app state or keys.
// Arrays are kept, since lists like `levels` or `tracePropagationTargets` are configuration.
function describeOptionValue(value: unknown): unknown {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export interface Integration {
/** @internal Options captured for SDK configuration reporting. */
_options?: unknown;

/**
* @internal Options that hold functions from a library, such as router hooks, rather than user code. SDK
* configuration reporting leaves their fingerprints out, since those change with the library build, not the config.
*/
_libraryOptions?: readonly string[];

/**
* This hook is only called once, even if multiple clients are created.
* It does not receives any arguments, and should only use for e.g. global monkey patching and similar things.
Expand Down
9 changes: 9 additions & 0 deletions packages/core/test/lib/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,4 +881,13 @@ describe('defineIntegration', () => {

expect(wrapperIntegration({ sampleRate: 0.5 })._options).toEqual({ sampleRate: 0.5 });
});

it('records the options that take library functions', () => {
const routerIntegration = defineIntegration(
(_options: { useLocation?: () => void; shouldCreateSpan?: () => boolean } = {}) => ({ name: 'Router' }),
{ libraryOptions: ['useLocation'] },
);

expect(routerIntegration()._libraryOptions).toEqual(['useLocation']);
});
});
56 changes: 56 additions & 0 deletions packages/core/test/lib/integrations/reportSdkConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,62 @@ describe('experimentalReportSdkConfigIntegration', () => {
});
});

it('reports library functions in integration options without a fingerprint, unlike user callbacks', () => {
const routerIntegration = defineIntegration(
(_options: { useLocation?: () => unknown; shouldCreateSpan?: () => boolean } = {}) => ({ name: 'Router' }),
{ libraryOptions: ['useLocation'] },
);
const reportRouter = (useLocation: () => unknown): Record<string, unknown> =>
reportConfig(
getDefaultTestClientOptions({
dsn: 'https://username@domain/123',
...captureOptionsSetByUser({
integrations: [
experimentalReportSdkConfigIntegration(),
routerIntegration({ useLocation, shouldCreateSpan: () => false }),
],
}),
}),
);

const payload = reportRouter(() => 'react-router@6');

expect(payload.options).toEqual({
integrations: [
{ name: 'ExperimentalReportSdkConfig' },
{
name: 'Router',
options: { shouldCreateSpan: expect.stringMatching(/^\[Function:\w+\]$/), useLocation: '[Function]' },
},
],
});
expect(reportRouter(() => 'react-router@7')).toEqual(payload);
});

it('reports library objects as placeholders and keeps library primitives', () => {
const routerIntegration = defineIntegration(
(_options: { history?: unknown; routes?: unknown[]; version?: string; basename?: string } = {}) => ({
name: 'Router',
}),
{ libraryOptions: ['history', 'routes', 'version'] },
);
const userOptions = captureOptionsSetByUser({
integrations: [
experimentalReportSdkConfigIntegration(),
routerIntegration({ history: { length: 1 }, routes: ['/users'], version: '6.22.0', basename: '/app' }),
],
});

const payload = reportConfig(getDefaultTestClientOptions({ dsn: 'https://username@domain/123', ...userOptions }));

expect(payload.options).toEqual({
integrations: [
{ name: 'ExperimentalReportSdkConfig' },
{ name: 'Router', options: { basename: '/app', history: '[Object]', routes: '[Object]', version: '6.22.0' } },
],
});
});
Comment thread
logaretm marked this conversation as resolved.
Resolved

it('reports integration options as passed, even when the integration writes into them', () => {
const mutatingIntegration = defineIntegration((options: { capture?: boolean } = {}) => {
options.capture = false;
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/react-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Integration } from '@sentry/core';
import { defineIntegration } from '@sentry/core';
import { createRoutesFromChildren, matchRoutes, useLocation, useNavigationType } from 'react-router';
import type { ReactRouterOptions } from './reactrouter-compat-utils';
import { REACT_ROUTER_LIBRARY_OPTIONS } from './reactrouter-compat-utils';
import { reactRouterBrowserTracingIntegration as reactRouterBrowserTracingIntegrationBase } from './reactrouter.compat';

// The routing wrappers (`wrapReactRouterRouting`, `wrapUseRoutes`, `wrapCreateBrowserRouter`,
Expand Down Expand Up @@ -50,4 +51,5 @@ function _reactRouterBrowserTracingIntegration(
*/
export const reactRouterBrowserTracingIntegration: typeof _reactRouterBrowserTracingIntegration = defineIntegration(
_reactRouterBrowserTracingIntegration,
{ libraryOptions: REACT_ROUTER_LIBRARY_OPTIONS },
);
1 change: 1 addition & 0 deletions packages/react/src/reactrouter-compat-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
addResolvedRoutesToParent,
processResolvedRoutes,
updateNavigationSpan,
REACT_ROUTER_LIBRARY_OPTIONS,
} from './instrumentation';

// Utility exports
Expand Down
11 changes: 11 additions & 0 deletions packages/react/src/reactrouter-compat-utils/instrumentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,17 @@ export function createV6CompatibleWrapCreateMemoryRouter<
};
}

/**
* The options that take React Router's hooks and helpers rather than user code.
*/
export const REACT_ROUTER_LIBRARY_OPTIONS = [
'useEffect',
'useLocation',
'useNavigationType',
'createRoutesFromChildren',
'matchRoutes',
] as const;

/**
* Creates a browser tracing integration that can be used with all React Router v6 compatible versions.
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/reactrouter.compat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
createV6CompatibleWrapCreateBrowserRouter,
createV6CompatibleWrapCreateMemoryRouter,
createV6CompatibleWrapUseRoutes,
REACT_ROUTER_LIBRARY_OPTIONS,
} from './reactrouter-compat-utils';
import type { CreateRouterFunction, Router, RouterState, UseRoutes } from './types';

Expand All @@ -25,6 +26,7 @@ function _reactRouterBrowserTracingIntegration(
*/
export const reactRouterBrowserTracingIntegration: typeof _reactRouterBrowserTracingIntegration = defineIntegration(
_reactRouterBrowserTracingIntegration,
{ libraryOptions: REACT_ROUTER_LIBRARY_OPTIONS },
);

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/reactrouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function _reactRouterV4BrowserTracingIntegration(
*/
export const reactRouterV4BrowserTracingIntegration: typeof _reactRouterV4BrowserTracingIntegration = defineIntegration(
_reactRouterV4BrowserTracingIntegration,
{ libraryOptions: ['matchPath'] },
);

function _reactRouterV5BrowserTracingIntegration(
Expand Down Expand Up @@ -119,6 +120,7 @@ function _reactRouterV5BrowserTracingIntegration(
*/
export const reactRouterV5BrowserTracingIntegration: typeof _reactRouterV5BrowserTracingIntegration = defineIntegration(
_reactRouterV5BrowserTracingIntegration,
{ libraryOptions: ['matchPath'] },
);

function instrumentReactRouter(
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/reactrouterv3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function _reactRouterV3BrowserTracingIntegration(
*/
export const reactRouterV3BrowserTracingIntegration: typeof _reactRouterV3BrowserTracingIntegration = defineIntegration(
_reactRouterV3BrowserTracingIntegration,
{ libraryOptions: ['match'] },
);

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/reactrouterv6.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
createV6CompatibleWrapCreateBrowserRouter,
createV6CompatibleWrapCreateMemoryRouter,
createV6CompatibleWrapUseRoutes,
REACT_ROUTER_LIBRARY_OPTIONS,
} from './reactrouter-compat-utils';
import type { CreateRouterFunction, Router, RouterState, UseRoutes } from './types';

Expand All @@ -25,6 +26,7 @@ function _reactRouterV6BrowserTracingIntegration(
*/
export const reactRouterV6BrowserTracingIntegration: typeof _reactRouterV6BrowserTracingIntegration = defineIntegration(
_reactRouterV6BrowserTracingIntegration,
{ libraryOptions: REACT_ROUTER_LIBRARY_OPTIONS },
);

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/reactrouterv7.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createV6CompatibleWrapCreateBrowserRouter,
createV6CompatibleWrapCreateMemoryRouter,
createV6CompatibleWrapUseRoutes,
REACT_ROUTER_LIBRARY_OPTIONS,
} from './reactrouter-compat-utils';
import type { CreateRouterFunction, Router, RouterState, UseRoutes } from './types';

Expand All @@ -26,6 +27,7 @@ function _reactRouterV7BrowserTracingIntegration(
*/
export const reactRouterV7BrowserTracingIntegration: typeof _reactRouterV7BrowserTracingIntegration = defineIntegration(
_reactRouterV7BrowserTracingIntegration,
{ libraryOptions: REACT_ROUTER_LIBRARY_OPTIONS },
);

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/remix/src/client/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ function _browserTracingIntegration(options: RemixBrowserTracingIntegrationOptio
* Creates a browser tracing integration for Remix applications.
* This integration will create pageload and navigation spans.
*/
export const browserTracingIntegration: typeof _browserTracingIntegration =
defineIntegration(_browserTracingIntegration);
export const browserTracingIntegration: typeof _browserTracingIntegration = defineIntegration(
_browserTracingIntegration,
{ libraryOptions: ['useEffect', 'useLocation', 'useMatches'] },
);
Loading