Skip to content

Commit e1352a3

Browse files
committed
clean up copy pasta in handlers.ts
1 parent de2ac55 commit e1352a3

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

packages/nextjs/src/utils/handlers.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export const withSentry = (handler: NextApiHandler): WrappedNextApiHandler => {
2020
if (currentScope) {
2121
currentScope.addEventProcessor(event => addRequestDataToEvent(event, req as NextRequest));
2222

23-
// We only want to record page and API requests
2423
if (hasTracingEnabled()) {
2524
// If there is a trace header set, extract the data from it (parentSpanId, traceId, and sampling decision)
2625
let traceparentData;
@@ -34,20 +33,18 @@ export const withSentry = (handler: NextApiHandler): WrappedNextApiHandler => {
3433
let reqPath = stripUrlQueryAndFragment(url);
3534
// Replace with placeholder
3635
if (req.query) {
36+
// TODO get this from next if possible, to avoid accidentally replacing non-dynamic parts of the path if
37+
// they match dynamic parts
3738
for (const [key, value] of Object.entries(req.query)) {
3839
reqPath = reqPath.replace(`${value}`, `[${key}]`);
3940
}
4041
}
41-
42-
// requests for pages will only ever be GET requests, so don't bother to include the method in the transaction
43-
// name; requests to API routes could be GET, POST, PUT, etc, so do include it there
44-
const namePrefix = `${(req.method || 'GET').toUpperCase()} `;
42+
const reqMethod = `${(req.method || 'GET').toUpperCase()} `;
4543

4644
const transaction = startTransaction(
4745
{
48-
name: `${namePrefix}${reqPath}`,
46+
name: `${reqMethod}${reqPath}`,
4947
op: 'http.server',
50-
metadata: { requestPath: reqPath },
5148
...traceparentData,
5249
},
5350
// extra context passed to the `tracesSampler`
@@ -57,7 +54,7 @@ export const withSentry = (handler: NextApiHandler): WrappedNextApiHandler => {
5754
}
5855
}
5956

60-
return await handler(req, res); // Call Handler
57+
return await handler(req, res); // Call original handler
6158
} catch (e) {
6259
withScope(scope => {
6360
scope.addEventProcessor(event => {
@@ -74,10 +71,6 @@ export const withSentry = (handler: NextApiHandler): WrappedNextApiHandler => {
7471
if (transaction) {
7572
transaction.setHttpStatus(res.statusCode);
7673

77-
// we'll collect this data in a more targeted way in the event processor we added above,
78-
// `addRequestDataToEvent`
79-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
80-
delete transaction.metadata.requestPath;
8174

8275
transaction.finish();
8376
}

0 commit comments

Comments
 (0)