From 19d72d5d4d74478d447c94305cb233c53909abd3 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Wed, 6 Jul 2022 23:15:57 -0700 Subject: [PATCH] add transaction name source values --- packages/nextjs/src/performance/client.ts | 8 +++++++- packages/nextjs/src/utils/instrumentServer.ts | 10 +++++++++- packages/nextjs/src/utils/withSentry.ts | 4 ++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/nextjs/src/performance/client.ts b/packages/nextjs/src/performance/client.ts index e3616b3a279e..1b89d4255dd6 100644 --- a/packages/nextjs/src/performance/client.ts +++ b/packages/nextjs/src/performance/client.ts @@ -35,11 +35,16 @@ export function nextRouterInstrumentation( // route name. Setting the transaction name after the transaction is started could lead // to possible race conditions with the router, so this approach was taken. if (startTransactionOnPageLoad) { - prevTransactionName = Router.route !== null ? stripUrlQueryAndFragment(Router.route) : global.location.pathname; + const pathIsRoute = Router.route !== null; + + prevTransactionName = pathIsRoute ? stripUrlQueryAndFragment(Router.route) : global.location.pathname; activeTransaction = startTransactionCb({ name: prevTransactionName, op: 'pageload', tags: DEFAULT_TAGS, + metadata: { + source: pathIsRoute ? 'route' : 'url', + }, }); } @@ -105,6 +110,7 @@ function changeStateWrapper(originalChangeStateWrapper: RouterChangeState): Wrap name: prevTransactionName, op: 'navigation', tags, + metadata: { source: 'route' }, }); } return originalChangeStateWrapper.call(this, method, url, as, options, ...args); diff --git a/packages/nextjs/src/utils/instrumentServer.ts b/packages/nextjs/src/utils/instrumentServer.ts index 0106acfb8123..d506778a1dda 100644 --- a/packages/nextjs/src/utils/instrumentServer.ts +++ b/packages/nextjs/src/utils/instrumentServer.ts @@ -269,7 +269,14 @@ function makeWrappedReqHandler(origReqHandler: ReqHandler): WrappedReqHandler { { name: `${namePrefix}${reqPath}`, op: 'http.server', - metadata: { requestPath: reqPath, baggage }, + metadata: { + baggage, + requestPath: reqPath, + // TODO: Investigate if there's a way to tell if this is a dynamic route, so that we can make this more + // like `source: isDynamicRoute? 'url' : 'route'` + // TODO: What happens when `withSentry` is used also? Which values of `name` and `source` win? + source: 'url', + }, ...traceparentData, }, // Extra context passed to the `tracesSampler` (Note: We're combining `nextReq` and `req` this way in order @@ -326,6 +333,7 @@ function makeWrappedMethodForGettingParameterizedPath( if (transaction && transaction.metadata.requestPath) { const origPath = transaction.metadata.requestPath; transaction.name = transaction.name.replace(origPath, parameterizedPath); + transaction.setMetadata({ source: 'route' }); } return origMethod.call(this, parameterizedPath, ...args); diff --git a/packages/nextjs/src/utils/withSentry.ts b/packages/nextjs/src/utils/withSentry.ts index 714a2271ceae..d1b249bf87a3 100644 --- a/packages/nextjs/src/utils/withSentry.ts +++ b/packages/nextjs/src/utils/withSentry.ts @@ -60,7 +60,7 @@ export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler = // Replace with placeholder if (req.query) { // TODO get this from next if possible, to avoid accidentally replacing non-dynamic parts of the path if - // they match dynamic parts + // they happen to match the values of any of the dynamic parts for (const [key, value] of Object.entries(req.query)) { reqPath = reqPath.replace(`${value}`, `[${key}]`); } @@ -72,7 +72,7 @@ export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler = name: `${reqMethod}${reqPath}`, op: 'http.server', ...traceparentData, - metadata: { baggage }, + metadata: { baggage, source: 'route' }, }, // extra context passed to the `tracesSampler` { request: req },