Skip to content

Commit b5ddf58

Browse files
authored
ref(react): Consolidate to use single setName call (#5681)
Remove usage `setMetadata` in favour of adding the source to `setName` helps us get prepped for #5679
1 parent 35f559c commit b5ddf58

File tree

5 files changed

+21
-39
lines changed

5 files changed

+21
-39
lines changed

packages/react/src/reactrouter.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ export function withSentryRouting<P extends Record<string, any>, R extends React
167167

168168
const WrappedRoute: React.FC<P> = (props: P) => {
169169
if (activeTransaction && props && props.computedMatch && props.computedMatch.isExact) {
170-
activeTransaction.setName(props.computedMatch.path);
171-
activeTransaction.setMetadata({ source: 'route' });
170+
activeTransaction.setName(props.computedMatch.path, 'route');
172171
}
173172

174173
// @ts-ignore Setting more specific React Component typing for `R` generic above

packages/react/src/reactrouterv6.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ function getNormalizedName(
145145

146146
function updatePageloadTransaction(location: Location, routes: RouteObject[]): void {
147147
if (activeTransaction) {
148-
const [name, source] = getNormalizedName(routes, location, _matchRoutes);
149-
activeTransaction.setName(name);
150-
activeTransaction.setMetadata({ source });
148+
activeTransaction.setName(...getNormalizedName(routes, location, _matchRoutes));
151149
}
152150
}
153151

packages/react/test/reactrouterv4.test.tsx

+8-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('React Router v4', () => {
1111
startTransactionOnPageLoad?: boolean;
1212
startTransactionOnLocationChange?: boolean;
1313
routes?: RouteConfig[];
14-
}): [jest.Mock, any, { mockSetName: jest.Mock; mockFinish: jest.Mock; mockSetMetadata: jest.Mock }] {
14+
}): [jest.Mock, any, { mockSetName: jest.Mock; mockFinish: jest.Mock }] {
1515
const options = {
1616
matchPath: _opts && _opts.routes !== undefined ? matchPath : undefined,
1717
routes: undefined,
@@ -22,16 +22,13 @@ describe('React Router v4', () => {
2222
const history = createMemoryHistory();
2323
const mockFinish = jest.fn();
2424
const mockSetName = jest.fn();
25-
const mockSetMetadata = jest.fn();
26-
const mockStartTransaction = jest
27-
.fn()
28-
.mockReturnValue({ setName: mockSetName, finish: mockFinish, setMetadata: mockSetMetadata });
25+
const mockStartTransaction = jest.fn().mockReturnValue({ setName: mockSetName, finish: mockFinish });
2926
reactRouterV4Instrumentation(history, options.routes, options.matchPath)(
3027
mockStartTransaction,
3128
options.startTransactionOnPageLoad,
3229
options.startTransactionOnLocationChange,
3330
);
34-
return [mockStartTransaction, history, { mockSetName, mockFinish, mockSetMetadata }];
31+
return [mockStartTransaction, history, { mockSetName, mockFinish }];
3532
}
3633

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

166163
it('normalizes transaction name with custom Route', () => {
167-
const [mockStartTransaction, history, { mockSetName, mockSetMetadata }] = createInstrumentation();
164+
const [mockStartTransaction, history, { mockSetName }] = createInstrumentation();
168165
const SentryRoute = withSentryRouting(Route);
169166
const { getByText } = render(
170167
<Router history={history}>
@@ -189,12 +186,11 @@ describe('React Router v4', () => {
189186
metadata: { source: 'url' },
190187
});
191188
expect(mockSetName).toHaveBeenCalledTimes(2);
192-
expect(mockSetName).toHaveBeenLastCalledWith('/users/:userid');
193-
expect(mockSetMetadata).toHaveBeenLastCalledWith({ source: 'route' });
189+
expect(mockSetName).toHaveBeenLastCalledWith('/users/:userid', 'route');
194190
});
195191

196192
it('normalizes nested transaction names with custom Route', () => {
197-
const [mockStartTransaction, history, { mockSetName, mockSetMetadata }] = createInstrumentation();
193+
const [mockStartTransaction, history, { mockSetName }] = createInstrumentation();
198194
const SentryRoute = withSentryRouting(Route);
199195
const { getByText } = render(
200196
<Router history={history}>
@@ -219,8 +215,7 @@ describe('React Router v4', () => {
219215
metadata: { source: 'url' },
220216
});
221217
expect(mockSetName).toHaveBeenCalledTimes(2);
222-
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid/v1/:teamid');
223-
expect(mockSetMetadata).toHaveBeenLastCalledWith({ source: 'route' });
218+
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid/v1/:teamid', 'route');
224219

225220
act(() => {
226221
history.push('/organizations/543');
@@ -235,8 +230,7 @@ describe('React Router v4', () => {
235230
metadata: { source: 'url' },
236231
});
237232
expect(mockSetName).toHaveBeenCalledTimes(3);
238-
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid');
239-
expect(mockSetMetadata).toHaveBeenLastCalledWith({ source: 'route' });
233+
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid', 'route');
240234
});
241235

242236
it('matches with route object', () => {

packages/react/test/reactrouterv5.test.tsx

+8-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('React Router v5', () => {
1111
startTransactionOnPageLoad?: boolean;
1212
startTransactionOnLocationChange?: boolean;
1313
routes?: RouteConfig[];
14-
}): [jest.Mock, any, { mockSetName: jest.Mock; mockFinish: jest.Mock; mockSetMetadata: jest.Mock }] {
14+
}): [jest.Mock, any, { mockSetName: jest.Mock; mockFinish: jest.Mock }] {
1515
const options = {
1616
matchPath: _opts && _opts.routes !== undefined ? matchPath : undefined,
1717
routes: undefined,
@@ -22,16 +22,13 @@ describe('React Router v5', () => {
2222
const history = createMemoryHistory();
2323
const mockFinish = jest.fn();
2424
const mockSetName = jest.fn();
25-
const mockSetMetadata = jest.fn();
26-
const mockStartTransaction = jest
27-
.fn()
28-
.mockReturnValue({ setName: mockSetName, finish: mockFinish, setMetadata: mockSetMetadata });
25+
const mockStartTransaction = jest.fn().mockReturnValue({ setName: mockSetName, finish: mockFinish });
2926
reactRouterV5Instrumentation(history, options.routes, options.matchPath)(
3027
mockStartTransaction,
3128
options.startTransactionOnPageLoad,
3229
options.startTransactionOnLocationChange,
3330
);
34-
return [mockStartTransaction, history, { mockSetName, mockFinish, mockSetMetadata }];
31+
return [mockStartTransaction, history, { mockSetName, mockFinish }];
3532
}
3633

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

166163
it('normalizes transaction name with custom Route', () => {
167-
const [mockStartTransaction, history, { mockSetName, mockSetMetadata }] = createInstrumentation();
164+
const [mockStartTransaction, history, { mockSetName }] = createInstrumentation();
168165
const SentryRoute = withSentryRouting(Route);
169166

170167
const { getByText } = render(
@@ -189,12 +186,11 @@ describe('React Router v5', () => {
189186
metadata: { source: 'url' },
190187
});
191188
expect(mockSetName).toHaveBeenCalledTimes(2);
192-
expect(mockSetName).toHaveBeenLastCalledWith('/users/:userid');
193-
expect(mockSetMetadata).toHaveBeenLastCalledWith({ source: 'route' });
189+
expect(mockSetName).toHaveBeenLastCalledWith('/users/:userid', 'route');
194190
});
195191

196192
it('normalizes nested transaction names with custom Route', () => {
197-
const [mockStartTransaction, history, { mockSetName, mockSetMetadata }] = createInstrumentation();
193+
const [mockStartTransaction, history, { mockSetName }] = createInstrumentation();
198194
const SentryRoute = withSentryRouting(Route);
199195

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

226221
act(() => {
227222
history.push('/organizations/543');
@@ -236,8 +231,7 @@ describe('React Router v5', () => {
236231
metadata: { source: 'url' },
237232
});
238233
expect(mockSetName).toHaveBeenCalledTimes(3);
239-
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid');
240-
expect(mockSetMetadata).toHaveBeenLastCalledWith({ source: 'route' });
234+
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid', 'route');
241235
});
242236

243237
it('matches with route object', () => {

packages/react/test/reactrouterv6.test.tsx

+3-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('React Router v6', () => {
2020
function createInstrumentation(_opts?: {
2121
startTransactionOnPageLoad?: boolean;
2222
startTransactionOnLocationChange?: boolean;
23-
}): [jest.Mock, { mockSetName: jest.Mock; mockFinish: jest.Mock; mockSetMetadata: jest.Mock }] {
23+
}): [jest.Mock, { mockSetName: jest.Mock; mockFinish: jest.Mock }] {
2424
const options = {
2525
matchPath: _opts ? matchPath : undefined,
2626
startTransactionOnLocationChange: true,
@@ -29,10 +29,7 @@ describe('React Router v6', () => {
2929
};
3030
const mockFinish = jest.fn();
3131
const mockSetName = jest.fn();
32-
const mockSetMetadata = jest.fn();
33-
const mockStartTransaction = jest
34-
.fn()
35-
.mockReturnValue({ setName: mockSetName, finish: mockFinish, setMetadata: mockSetMetadata });
32+
const mockStartTransaction = jest.fn().mockReturnValue({ setName: mockSetName, finish: mockFinish });
3633

3734
reactRouterV6Instrumentation(
3835
React.useEffect,
@@ -41,7 +38,7 @@ describe('React Router v6', () => {
4138
createRoutesFromChildren,
4239
matchRoutes,
4340
)(mockStartTransaction, options.startTransactionOnPageLoad, options.startTransactionOnLocationChange);
44-
return [mockStartTransaction, { mockSetName, mockFinish, mockSetMetadata }];
41+
return [mockStartTransaction, { mockSetName, mockFinish }];
4542
}
4643

4744
describe('withSentryReactRouterV6Routing', () => {

0 commit comments

Comments
 (0)