Skip to content

ref(react): Consolidate to use single setName call #5681

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 2 commits into from
Sep 2, 2022
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
3 changes: 1 addition & 2 deletions packages/react/src/reactrouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ export function withSentryRouting<P extends Record<string, any>, R extends React

const WrappedRoute: React.FC<P> = (props: P) => {
if (activeTransaction && props && props.computedMatch && props.computedMatch.isExact) {
activeTransaction.setName(props.computedMatch.path);
activeTransaction.setMetadata({ source: 'route' });
activeTransaction.setName(props.computedMatch.path, 'route');
}

// @ts-ignore Setting more specific React Component typing for `R` generic above
Expand Down
4 changes: 1 addition & 3 deletions packages/react/src/reactrouterv6.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ function getNormalizedName(

function updatePageloadTransaction(location: Location, routes: RouteObject[]): void {
if (activeTransaction) {
const [name, source] = getNormalizedName(routes, location, _matchRoutes);
activeTransaction.setName(name);
activeTransaction.setMetadata({ source });
activeTransaction.setName(...getNormalizedName(routes, location, _matchRoutes));
}
}

Expand Down
22 changes: 8 additions & 14 deletions packages/react/test/reactrouterv4.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('React Router v4', () => {
startTransactionOnPageLoad?: boolean;
startTransactionOnLocationChange?: boolean;
routes?: RouteConfig[];
}): [jest.Mock, any, { mockSetName: jest.Mock; mockFinish: jest.Mock; mockSetMetadata: jest.Mock }] {
}): [jest.Mock, any, { mockSetName: jest.Mock; mockFinish: jest.Mock }] {
const options = {
matchPath: _opts && _opts.routes !== undefined ? matchPath : undefined,
routes: undefined,
Expand All @@ -22,16 +22,13 @@ describe('React Router v4', () => {
const history = createMemoryHistory();
const mockFinish = jest.fn();
const mockSetName = jest.fn();
const mockSetMetadata = jest.fn();
const mockStartTransaction = jest
.fn()
.mockReturnValue({ setName: mockSetName, finish: mockFinish, setMetadata: mockSetMetadata });
const mockStartTransaction = jest.fn().mockReturnValue({ setName: mockSetName, finish: mockFinish });
reactRouterV4Instrumentation(history, options.routes, options.matchPath)(
mockStartTransaction,
options.startTransactionOnPageLoad,
options.startTransactionOnLocationChange,
);
return [mockStartTransaction, history, { mockSetName, mockFinish, mockSetMetadata }];
return [mockStartTransaction, history, { mockSetName, mockFinish }];
}

it('starts a pageload transaction when instrumentation is started', () => {
Expand Down Expand Up @@ -164,7 +161,7 @@ describe('React Router v4', () => {
});

it('normalizes transaction name with custom Route', () => {
const [mockStartTransaction, history, { mockSetName, mockSetMetadata }] = createInstrumentation();
const [mockStartTransaction, history, { mockSetName }] = createInstrumentation();
const SentryRoute = withSentryRouting(Route);
const { getByText } = render(
<Router history={history}>
Expand All @@ -189,12 +186,11 @@ describe('React Router v4', () => {
metadata: { source: 'url' },
});
expect(mockSetName).toHaveBeenCalledTimes(2);
expect(mockSetName).toHaveBeenLastCalledWith('/users/:userid');
expect(mockSetMetadata).toHaveBeenLastCalledWith({ source: 'route' });
expect(mockSetName).toHaveBeenLastCalledWith('/users/:userid', 'route');
});

it('normalizes nested transaction names with custom Route', () => {
const [mockStartTransaction, history, { mockSetName, mockSetMetadata }] = createInstrumentation();
const [mockStartTransaction, history, { mockSetName }] = createInstrumentation();
const SentryRoute = withSentryRouting(Route);
const { getByText } = render(
<Router history={history}>
Expand All @@ -219,8 +215,7 @@ describe('React Router v4', () => {
metadata: { source: 'url' },
});
expect(mockSetName).toHaveBeenCalledTimes(2);
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid/v1/:teamid');
expect(mockSetMetadata).toHaveBeenLastCalledWith({ source: 'route' });
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid/v1/:teamid', 'route');

act(() => {
history.push('/organizations/543');
Expand All @@ -235,8 +230,7 @@ describe('React Router v4', () => {
metadata: { source: 'url' },
});
expect(mockSetName).toHaveBeenCalledTimes(3);
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid');
expect(mockSetMetadata).toHaveBeenLastCalledWith({ source: 'route' });
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid', 'route');
});

it('matches with route object', () => {
Expand Down
22 changes: 8 additions & 14 deletions packages/react/test/reactrouterv5.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('React Router v5', () => {
startTransactionOnPageLoad?: boolean;
startTransactionOnLocationChange?: boolean;
routes?: RouteConfig[];
}): [jest.Mock, any, { mockSetName: jest.Mock; mockFinish: jest.Mock; mockSetMetadata: jest.Mock }] {
}): [jest.Mock, any, { mockSetName: jest.Mock; mockFinish: jest.Mock }] {
const options = {
matchPath: _opts && _opts.routes !== undefined ? matchPath : undefined,
routes: undefined,
Expand All @@ -22,16 +22,13 @@ describe('React Router v5', () => {
const history = createMemoryHistory();
const mockFinish = jest.fn();
const mockSetName = jest.fn();
const mockSetMetadata = jest.fn();
const mockStartTransaction = jest
.fn()
.mockReturnValue({ setName: mockSetName, finish: mockFinish, setMetadata: mockSetMetadata });
const mockStartTransaction = jest.fn().mockReturnValue({ setName: mockSetName, finish: mockFinish });
reactRouterV5Instrumentation(history, options.routes, options.matchPath)(
mockStartTransaction,
options.startTransactionOnPageLoad,
options.startTransactionOnLocationChange,
);
return [mockStartTransaction, history, { mockSetName, mockFinish, mockSetMetadata }];
return [mockStartTransaction, history, { mockSetName, mockFinish }];
}

it('starts a pageload transaction when instrumentation is started', () => {
Expand Down Expand Up @@ -164,7 +161,7 @@ describe('React Router v5', () => {
});

it('normalizes transaction name with custom Route', () => {
const [mockStartTransaction, history, { mockSetName, mockSetMetadata }] = createInstrumentation();
const [mockStartTransaction, history, { mockSetName }] = createInstrumentation();
const SentryRoute = withSentryRouting(Route);

const { getByText } = render(
Expand All @@ -189,12 +186,11 @@ describe('React Router v5', () => {
metadata: { source: 'url' },
});
expect(mockSetName).toHaveBeenCalledTimes(2);
expect(mockSetName).toHaveBeenLastCalledWith('/users/:userid');
expect(mockSetMetadata).toHaveBeenLastCalledWith({ source: 'route' });
expect(mockSetName).toHaveBeenLastCalledWith('/users/:userid', 'route');
});

it('normalizes nested transaction names with custom Route', () => {
const [mockStartTransaction, history, { mockSetName, mockSetMetadata }] = createInstrumentation();
const [mockStartTransaction, history, { mockSetName }] = createInstrumentation();
const SentryRoute = withSentryRouting(Route);

const { getByText } = render(
Expand All @@ -220,8 +216,7 @@ describe('React Router v5', () => {
metadata: { source: 'url' },
});
expect(mockSetName).toHaveBeenCalledTimes(2);
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid/v1/:teamid');
expect(mockSetMetadata).toHaveBeenLastCalledWith({ source: 'route' });
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid/v1/:teamid', 'route');

act(() => {
history.push('/organizations/543');
Expand All @@ -236,8 +231,7 @@ describe('React Router v5', () => {
metadata: { source: 'url' },
});
expect(mockSetName).toHaveBeenCalledTimes(3);
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid');
expect(mockSetMetadata).toHaveBeenLastCalledWith({ source: 'route' });
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid', 'route');
});

it('matches with route object', () => {
Expand Down
9 changes: 3 additions & 6 deletions packages/react/test/reactrouterv6.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('React Router v6', () => {
function createInstrumentation(_opts?: {
startTransactionOnPageLoad?: boolean;
startTransactionOnLocationChange?: boolean;
}): [jest.Mock, { mockSetName: jest.Mock; mockFinish: jest.Mock; mockSetMetadata: jest.Mock }] {
}): [jest.Mock, { mockSetName: jest.Mock; mockFinish: jest.Mock }] {
const options = {
matchPath: _opts ? matchPath : undefined,
startTransactionOnLocationChange: true,
Expand All @@ -29,10 +29,7 @@ describe('React Router v6', () => {
};
const mockFinish = jest.fn();
const mockSetName = jest.fn();
const mockSetMetadata = jest.fn();
const mockStartTransaction = jest
.fn()
.mockReturnValue({ setName: mockSetName, finish: mockFinish, setMetadata: mockSetMetadata });
const mockStartTransaction = jest.fn().mockReturnValue({ setName: mockSetName, finish: mockFinish });

reactRouterV6Instrumentation(
React.useEffect,
Expand All @@ -41,7 +38,7 @@ describe('React Router v6', () => {
createRoutesFromChildren,
matchRoutes,
)(mockStartTransaction, options.startTransactionOnPageLoad, options.startTransactionOnLocationChange);
return [mockStartTransaction, { mockSetName, mockFinish, mockSetMetadata }];
return [mockStartTransaction, { mockSetName, mockFinish }];
}

describe('withSentryReactRouterV6Routing', () => {
Expand Down