Repository: getsentry/sentry-javascript (TypeScript)

Issue:
Add trpc path to context in trpcMiddleware
### Problem Statement

It would make it easier to debug if I can see which trpc route is causing an issue. The current trpc route only add procedure type and input to the context.

### Solution Brainstorm

Simply add path to trpc context in this function would likely work? https://github.com/getsentry/sentry-javascript/blob/984a6bdceab56bf64a67d42fa4d457e3256020fc/packages/core/src/trpc.ts#L41-L94


Discussion: So we do attach the path to spans if you have tracing enabled, but I think it also makes sense to add it as context.

would you be open to create a PR to add this?
> So we do attach the path to spans if you have tracing enabled, but I think it also makes sense to add it as context.
> 
> would you be open to create a PR to add this?

Here you go: https://github.com/getsentry/sentry-javascript/pull/14218



Fix (patch):
diff --git a/packages/core/src/trpc.ts b/packages/core/src/trpc.ts
index 366a0ba9aa62..fbcdf5832b46 100644
--- a/packages/core/src/trpc.ts
+++ b/packages/core/src/trpc.ts
@@ -1,7 +1,7 @@
 import { normalize } from '@sentry/utils';
 
-import { getClient } from './currentScopes';
-import { captureException, setContext } from './exports';
+import { getClient, withScope } from './currentScopes';
+import { captureException } from './exports';
 import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from './semanticAttributes';
 import { startSpanManual } from './tracing';
 
@@ -48,6 +48,7 @@ export function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) {
     const clientOptions = client && client.getOptions();
 
     const trpcContext: Record<string, unknown> = {
+      procedure_path: path,
       procedure_type: type,
     };
 
@@ -66,29 +67,31 @@ export function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) {
         }
       }
     }
-    setContext('trpc', trpcContext);
 
-    return startSpanManual(
-      {
-        name: `trpc/${path}`,
-        op: 'rpc.server',
-        attributes: {
-          [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
-          [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.rpc.trpc',
+    return withScope(scope => {
+      scope.setContext('trpc', trpcContext);
+      return startSpanManual(
+        {
+          name: `trpc/${path}`,
+          op: 'rpc.server',
+          attributes: {
+            [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
+            [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.rpc.trpc',
+          },
         },
-      },
-      async span => {
-        try {
-          const nextResult = await next();
-          captureIfError(nextResult);
-          span.end();
-          return nextResult;
-        } catch (e) {
-          captureException(e, trpcCaptureContext);
-          span.end();
-          throw e;
-        }
-      },
-    ) as SentryTrpcMiddleware<T>;
+        async span => {
+          try {
+            const nextResult = await next();
+            captureIfError(nextResult);
+            span.end();
+            return nextResult;
+          } catch (e) {
+            captureException(e, trpcCaptureContext);
+            span.end();
+            throw e;
+          }
+        },
+      ) as SentryTrpcMiddleware<T>;
+    });
   };
 }