Skip to content

ref(react): Streamline browser tracing integrations & remove old code #11012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2024
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
100 changes: 45 additions & 55 deletions packages/react/src/reactrouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
getRootSpan,
spanToJSON,
} from '@sentry/core';
import type { Integration, Span, StartSpanOptions, TransactionSource } from '@sentry/types';
import type { Client, Integration, Span, TransactionSource } from '@sentry/types';
import hoistNonReactStatics from 'hoist-non-react-statics';
import * as React from 'react';

import type { Action, Location, ReactRouterInstrumentation } from './types';
import type { Action, Location } from './types';

// We need to disable eslint no-explict-any because any is required for the
// react-router typings.
Expand Down Expand Up @@ -63,21 +63,15 @@ export function reactRouterV4BrowserTracingIntegration(
afterAllSetup(client) {
integration.afterAllSetup(client);

const startPageloadCallback = (startSpanOptions: StartSpanOptions): undefined => {
startBrowserTracingPageLoadSpan(client, startSpanOptions);
return undefined;
};

const startNavigationCallback = (startSpanOptions: StartSpanOptions): undefined => {
startBrowserTracingNavigationSpan(client, startSpanOptions);
return undefined;
};

const instrumentation = createReactRouterInstrumentation(history, 'reactrouter_v4', routes, matchPath);

// Now instrument page load & navigation with correct settings
instrumentation(startPageloadCallback, instrumentPageLoad, false);
instrumentation(startNavigationCallback, false, instrumentNavigation);
instrumentReactRouter(
client,
instrumentPageLoad,
instrumentNavigation,
history,
'reactrouter_v4',
routes,
matchPath,
);
},
};
}
Expand All @@ -95,38 +89,35 @@ export function reactRouterV5BrowserTracingIntegration(
instrumentNavigation: false,
});

const { history, routes, matchPath } = options;
const { history, routes, matchPath, instrumentPageLoad = true, instrumentNavigation = true } = options;

return {
...integration,
afterAllSetup(client) {
integration.afterAllSetup(client);

const startPageloadCallback = (startSpanOptions: StartSpanOptions): undefined => {
startBrowserTracingPageLoadSpan(client, startSpanOptions);
return undefined;
};

const startNavigationCallback = (startSpanOptions: StartSpanOptions): undefined => {
startBrowserTracingNavigationSpan(client, startSpanOptions);
return undefined;
};

const instrumentation = createReactRouterInstrumentation(history, 'reactrouter_v5', routes, matchPath);

// Now instrument page load & navigation with correct settings
instrumentation(startPageloadCallback, options.instrumentPageLoad, false);
instrumentation(startNavigationCallback, false, options.instrumentNavigation);
instrumentReactRouter(
client,
instrumentPageLoad,
instrumentNavigation,
history,
'reactrouter_v5',
routes,
matchPath,
);
},
};
}

function createReactRouterInstrumentation(
function instrumentReactRouter(
client: Client,
instrumentPageLoad: boolean,
instrumentNavigation: boolean,
history: RouterHistory,
instrumentationName: string,
allRoutes: RouteConfig[] = [],
matchPath?: MatchPath,
): ReactRouterInstrumentation {
): void {
function getInitPathName(): string | undefined {
if (history && history.location) {
return history.location.pathname;
Expand Down Expand Up @@ -161,12 +152,11 @@ function createReactRouterInstrumentation(
return [pathname, 'url'];
}

return (customStartTransaction, startTransactionOnPageLoad = true, startTransactionOnLocationChange = true): void => {
if (instrumentPageLoad) {
const initPathName = getInitPathName();

if (startTransactionOnPageLoad && initPathName) {
if (initPathName) {
const [name, source] = normalizeTransactionName(initPathName);
customStartTransaction({
startBrowserTracingPageLoadSpan(client, {
name,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
Expand All @@ -175,23 +165,23 @@ function createReactRouterInstrumentation(
},
});
}
}

if (startTransactionOnLocationChange && history.listen) {
history.listen((location, action) => {
if (action && (action === 'PUSH' || action === 'POP')) {
const [name, source] = normalizeTransactionName(location.pathname);
customStartTransaction({
name,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: `auto.navigation.react.${instrumentationName}`,
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
},
});
}
});
}
};
if (instrumentNavigation && history.listen) {
history.listen((location, action) => {
if (action && (action === 'PUSH' || action === 'POP')) {
const [name, source] = normalizeTransactionName(location.pathname);
startBrowserTracingNavigationSpan(client, {
name,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: `auto.navigation.react.${instrumentationName}`,
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
},
});
}
});
}
}

/**
Expand Down
126 changes: 40 additions & 86 deletions packages/react/src/reactrouterv3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@ import {
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
} from '@sentry/core';
import type {
Integration,
SpanAttributes,
StartSpanOptions,
Transaction,
TransactionContext,
TransactionSource,
} from '@sentry/types';
import type { Integration, TransactionSource } from '@sentry/types';

import type { Location, ReactRouterInstrumentation } from './types';
import type { Location } from './types';

// Many of the types below had to be mocked out to prevent typescript issues
// these types are required for correct functionality.
Expand Down Expand Up @@ -64,85 +57,46 @@ export function reactRouterV3BrowserTracingIntegration(
afterAllSetup(client) {
integration.afterAllSetup(client);

const startPageloadCallback = (startSpanOptions: StartSpanOptions): undefined => {
startBrowserTracingPageLoadSpan(client, startSpanOptions);
return undefined;
};

const startNavigationCallback = (startSpanOptions: StartSpanOptions): undefined => {
startBrowserTracingNavigationSpan(client, startSpanOptions);
return undefined;
};

const instrumentation = reactRouterV3Instrumentation(history, routes, match);

// Now instrument page load & navigation with correct settings
instrumentation(startPageloadCallback, instrumentPageLoad, false);
instrumentation(startNavigationCallback, false, instrumentNavigation);
},
};
}

/**
* Creates routing instrumentation for React Router v3
* Works for React Router >= 3.2.0 and < 4.0.0
*
* @param history object from the `history` library
* @param routes a list of all routes, should be
* @param match `Router.match` utility
*/
function reactRouterV3Instrumentation(history: HistoryV3, routes: Route[], match: Match): ReactRouterInstrumentation {
return (
startTransaction: (context: TransactionContext) => Transaction | undefined,
startTransactionOnPageLoad: boolean = true,
startTransactionOnLocationChange: boolean = true,
) => {
let activeTransaction: Transaction | undefined;
let prevName: string | undefined;

// Have to use window.location because history.location might not be defined.
if (startTransactionOnPageLoad && WINDOW && WINDOW.location) {
normalizeTransactionName(
routes,
WINDOW.location as unknown as Location,
match,
(localName: string, source: ReactRouterV3TransactionSource = 'url') => {
prevName = localName;
activeTransaction = startTransaction({
name: prevName,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.react.reactrouter_v3',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
},
});
},
);
}
if (instrumentPageLoad && WINDOW && WINDOW.location) {
normalizeTransactionName(
routes,
WINDOW.location as unknown as Location,
match,
(localName: string, source: ReactRouterV3TransactionSource = 'url') => {
startBrowserTracingPageLoadSpan(client, {
name: localName,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.react.reactrouter_v3',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
},
});
},
);
}

if (startTransactionOnLocationChange && history.listen) {
history.listen(location => {
if (location.action === 'PUSH' || location.action === 'POP') {
if (activeTransaction) {
activeTransaction.end();
if (instrumentNavigation && history.listen) {
history.listen(location => {
if (location.action === 'PUSH' || location.action === 'POP') {
normalizeTransactionName(
routes,
location,
match,
(localName: string, source: TransactionSource = 'url') => {
startBrowserTracingNavigationSpan(client, {
name: localName,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.react.reactrouter_v3',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
},
});
},
);
}
normalizeTransactionName(routes, location, match, (localName: string, source: TransactionSource = 'url') => {
prevName = localName;

const attributes: SpanAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.react.reactrouter_v3',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
};

activeTransaction = startTransaction({
name: prevName,
attributes,
});
});
}
});
}
});
}
},
};
}

Expand Down
Loading