diff --git a/packages/core/src/integration.ts b/packages/core/src/integration.ts index 968e97e64223..bf411b2ef359 100644 --- a/packages/core/src/integration.ts +++ b/packages/core/src/integration.ts @@ -167,10 +167,14 @@ 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: Fn, + config?: { libraryOptions?: ReadonlyArray[0]> & string> }, ): (...args: Parameters) => Integration & { name: ReturnType['name'] } { return (...args: Parameters) => { // Copied before the integration runs, so its writes into the object it was given aren't reported as the user's. @@ -178,6 +182,7 @@ export function defineIntegration( const integration = fn(...args) as ReturnType; // 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; }; diff --git a/packages/core/src/integrations/reportSdkConfig.ts b/packages/core/src/integrations/reportSdkConfig.ts index 2efdcfabb2c7..7b4160e887ba 100644 --- a/packages/core/src/integrations/reportSdkConfig.ts +++ b/packages/core/src/integrations/reportSdkConfig.ts @@ -123,7 +123,11 @@ function buildConfigAttributes(client: Client): Record { 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, + ); }); } @@ -134,7 +138,7 @@ function buildConfigAttributes(client: Client): Record { 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), ); } @@ -173,7 +177,11 @@ function describeShape(value: unknown, ancestors = new Set()): 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 }; } @@ -190,12 +198,21 @@ function describeIntegration(name: string, options: unknown): { name: string; op const described: Record = {}; 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 { diff --git a/packages/core/src/types/integration.ts b/packages/core/src/types/integration.ts index 78e4824695ec..d42ea54cdbeb 100644 --- a/packages/core/src/types/integration.ts +++ b/packages/core/src/types/integration.ts @@ -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. diff --git a/packages/core/test/lib/integration.test.ts b/packages/core/test/lib/integration.test.ts index 1702650a52b1..d92ef6e087b6 100644 --- a/packages/core/test/lib/integration.test.ts +++ b/packages/core/test/lib/integration.test.ts @@ -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']); + }); }); diff --git a/packages/core/test/lib/integrations/reportSdkConfig.test.ts b/packages/core/test/lib/integrations/reportSdkConfig.test.ts index 5e0114362da4..009ef6aa9760 100644 --- a/packages/core/test/lib/integrations/reportSdkConfig.test.ts +++ b/packages/core/test/lib/integrations/reportSdkConfig.test.ts @@ -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 => + 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' } }, + ], + }); + }); + it('reports integration options as passed, even when the integration writes into them', () => { const mutatingIntegration = defineIntegration((options: { capture?: boolean } = {}) => { options.capture = false; diff --git a/packages/react/src/react-router.ts b/packages/react/src/react-router.ts index 8a87e4904f29..a1351454fdf4 100644 --- a/packages/react/src/react-router.ts +++ b/packages/react/src/react-router.ts @@ -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`, @@ -50,4 +51,5 @@ function _reactRouterBrowserTracingIntegration( */ export const reactRouterBrowserTracingIntegration: typeof _reactRouterBrowserTracingIntegration = defineIntegration( _reactRouterBrowserTracingIntegration, + { libraryOptions: REACT_ROUTER_LIBRARY_OPTIONS }, ); diff --git a/packages/react/src/reactrouter-compat-utils/index.ts b/packages/react/src/reactrouter-compat-utils/index.ts index 426c6747eff7..4d1e3a627f86 100644 --- a/packages/react/src/reactrouter-compat-utils/index.ts +++ b/packages/react/src/reactrouter-compat-utils/index.ts @@ -12,6 +12,7 @@ export { addResolvedRoutesToParent, processResolvedRoutes, updateNavigationSpan, + REACT_ROUTER_LIBRARY_OPTIONS, } from './instrumentation'; // Utility exports diff --git a/packages/react/src/reactrouter-compat-utils/instrumentation.tsx b/packages/react/src/reactrouter-compat-utils/instrumentation.tsx index f18a90a3fb07..1c9870456254 100644 --- a/packages/react/src/reactrouter-compat-utils/instrumentation.tsx +++ b/packages/react/src/reactrouter-compat-utils/instrumentation.tsx @@ -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. */ diff --git a/packages/react/src/reactrouter.compat.tsx b/packages/react/src/reactrouter.compat.tsx index d7aaca5c76ed..83568f783ac4 100644 --- a/packages/react/src/reactrouter.compat.tsx +++ b/packages/react/src/reactrouter.compat.tsx @@ -8,6 +8,7 @@ import { createV6CompatibleWrapCreateBrowserRouter, createV6CompatibleWrapCreateMemoryRouter, createV6CompatibleWrapUseRoutes, + REACT_ROUTER_LIBRARY_OPTIONS, } from './reactrouter-compat-utils'; import type { CreateRouterFunction, Router, RouterState, UseRoutes } from './types'; @@ -25,6 +26,7 @@ function _reactRouterBrowserTracingIntegration( */ export const reactRouterBrowserTracingIntegration: typeof _reactRouterBrowserTracingIntegration = defineIntegration( _reactRouterBrowserTracingIntegration, + { libraryOptions: REACT_ROUTER_LIBRARY_OPTIONS }, ); /** diff --git a/packages/react/src/reactrouter.tsx b/packages/react/src/reactrouter.tsx index 83a0e5cbb6e1..fb5d97878180 100644 --- a/packages/react/src/reactrouter.tsx +++ b/packages/react/src/reactrouter.tsx @@ -82,6 +82,7 @@ function _reactRouterV4BrowserTracingIntegration( */ export const reactRouterV4BrowserTracingIntegration: typeof _reactRouterV4BrowserTracingIntegration = defineIntegration( _reactRouterV4BrowserTracingIntegration, + { libraryOptions: ['matchPath'] }, ); function _reactRouterV5BrowserTracingIntegration( @@ -119,6 +120,7 @@ function _reactRouterV5BrowserTracingIntegration( */ export const reactRouterV5BrowserTracingIntegration: typeof _reactRouterV5BrowserTracingIntegration = defineIntegration( _reactRouterV5BrowserTracingIntegration, + { libraryOptions: ['matchPath'] }, ); function instrumentReactRouter( diff --git a/packages/react/src/reactrouterv3.ts b/packages/react/src/reactrouterv3.ts index 87dcb323659f..043c80e889a8 100644 --- a/packages/react/src/reactrouterv3.ts +++ b/packages/react/src/reactrouterv3.ts @@ -109,6 +109,7 @@ function _reactRouterV3BrowserTracingIntegration( */ export const reactRouterV3BrowserTracingIntegration: typeof _reactRouterV3BrowserTracingIntegration = defineIntegration( _reactRouterV3BrowserTracingIntegration, + { libraryOptions: ['match'] }, ); /** diff --git a/packages/react/src/reactrouterv6.tsx b/packages/react/src/reactrouterv6.tsx index 14b5e5600a59..c5780734314c 100644 --- a/packages/react/src/reactrouterv6.tsx +++ b/packages/react/src/reactrouterv6.tsx @@ -8,6 +8,7 @@ import { createV6CompatibleWrapCreateBrowserRouter, createV6CompatibleWrapCreateMemoryRouter, createV6CompatibleWrapUseRoutes, + REACT_ROUTER_LIBRARY_OPTIONS, } from './reactrouter-compat-utils'; import type { CreateRouterFunction, Router, RouterState, UseRoutes } from './types'; @@ -25,6 +26,7 @@ function _reactRouterV6BrowserTracingIntegration( */ export const reactRouterV6BrowserTracingIntegration: typeof _reactRouterV6BrowserTracingIntegration = defineIntegration( _reactRouterV6BrowserTracingIntegration, + { libraryOptions: REACT_ROUTER_LIBRARY_OPTIONS }, ); /** diff --git a/packages/react/src/reactrouterv7.tsx b/packages/react/src/reactrouterv7.tsx index 23c0e349b615..5258fb612f2e 100644 --- a/packages/react/src/reactrouterv7.tsx +++ b/packages/react/src/reactrouterv7.tsx @@ -9,6 +9,7 @@ import { createV6CompatibleWrapCreateBrowserRouter, createV6CompatibleWrapCreateMemoryRouter, createV6CompatibleWrapUseRoutes, + REACT_ROUTER_LIBRARY_OPTIONS, } from './reactrouter-compat-utils'; import type { CreateRouterFunction, Router, RouterState, UseRoutes } from './types'; @@ -26,6 +27,7 @@ function _reactRouterV7BrowserTracingIntegration( */ export const reactRouterV7BrowserTracingIntegration: typeof _reactRouterV7BrowserTracingIntegration = defineIntegration( _reactRouterV7BrowserTracingIntegration, + { libraryOptions: REACT_ROUTER_LIBRARY_OPTIONS }, ); /** diff --git a/packages/remix/src/client/browserTracingIntegration.ts b/packages/remix/src/client/browserTracingIntegration.ts index f7b03fd8f91c..1949bfe9a71b 100644 --- a/packages/remix/src/client/browserTracingIntegration.ts +++ b/packages/remix/src/client/browserTracingIntegration.ts @@ -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'] }, +);