diff --git a/dev-packages/e2e-tests/test-applications/nextjs-t3/tests/trpc-error.test.ts b/dev-packages/e2e-tests/test-applications/nextjs-t3/tests/trpc-error.test.ts index 0245b641db5c..d3e175e0558b 100644 --- a/dev-packages/e2e-tests/test-applications/nextjs-t3/tests/trpc-error.test.ts +++ b/dev-packages/e2e-tests/test-applications/nextjs-t3/tests/trpc-error.test.ts @@ -12,9 +12,10 @@ test('should capture error with trpc context', async ({ page }) => { const trpcError = await errorEventPromise; expect(trpcError).toBeDefined(); - expect(trpcError.contexts.trpc).toBeDefined(); - expect(trpcError.contexts.trpc.procedure_type).toEqual('mutation'); - expect(trpcError.contexts.trpc.input).toEqual({ name: 'I love dogs' }); + expect(trpcError.contexts?.trpc).toBeDefined(); + expect(trpcError.contexts?.trpc?.procedure_type).toEqual('mutation'); + expect(trpcError.contexts?.trpc?.procedure_path).toBe('post.throwError'); + expect(trpcError.contexts?.trpc?.input).toEqual({ name: 'I love dogs' }); }); test('should create transaction with trpc input for error', async ({ page }) => { @@ -26,9 +27,5 @@ test('should create transaction with trpc input for error', async ({ page }) => await page.click('#error-button'); const trpcTransaction = await trpcTransactionPromise; - expect(trpcTransaction).toBeDefined(); - expect(trpcTransaction.contexts.trpc).toBeDefined(); - expect(trpcTransaction.contexts.trpc.procedure_type).toEqual('mutation'); - expect(trpcTransaction.contexts.trpc.input).toEqual({ name: 'I love dogs' }); }); diff --git a/dev-packages/e2e-tests/test-applications/nextjs-t3/tests/trpc-mutation.test.ts b/dev-packages/e2e-tests/test-applications/nextjs-t3/tests/trpc-mutation.test.ts index 47d6a52f8a19..ee3ebfd099ff 100644 --- a/dev-packages/e2e-tests/test-applications/nextjs-t3/tests/trpc-mutation.test.ts +++ b/dev-packages/e2e-tests/test-applications/nextjs-t3/tests/trpc-mutation.test.ts @@ -13,7 +13,4 @@ test('should create transaction with trpc input for mutation', async ({ page }) const trpcTransaction = await trpcTransactionPromise; expect(trpcTransaction).toBeDefined(); - expect(trpcTransaction.contexts.trpc).toBeDefined(); - expect(trpcTransaction.contexts.trpc.procedure_type).toEqual('mutation'); - expect(trpcTransaction.contexts.trpc.input).toEqual({ name: 'I love dogs' }); }); diff --git a/dev-packages/e2e-tests/test-applications/node-express/tests/trpc.test.ts b/dev-packages/e2e-tests/test-applications/node-express/tests/trpc.test.ts index 4f274fdc16ae..fcdd9b39a103 100644 --- a/dev-packages/e2e-tests/test-applications/node-express/tests/trpc.test.ts +++ b/dev-packages/e2e-tests/test-applications/node-express/tests/trpc.test.ts @@ -33,11 +33,6 @@ test('Should record span for trpc query', async ({ baseURL }) => { description: `trpc/getSomething`, }), ); - - expect(transaction.contexts?.trpc).toMatchObject({ - procedure_type: 'query', - input: 'foobar', - }); }); test('Should record transaction for trpc mutation', async ({ baseURL }) => { @@ -70,10 +65,6 @@ test('Should record transaction for trpc mutation', async ({ baseURL }) => { description: `trpc/createSomething`, }), ); - - expect(transaction.contexts?.trpc).toMatchObject({ - procedure_type: 'mutation', - }); }); test('Should record transaction and error for a crashing trpc handler', async ({ baseURL }) => { @@ -100,6 +91,9 @@ test('Should record transaction and error for a crashing trpc handler', async ({ await expect(transactionEventPromise).resolves.toBeDefined(); await expect(errorEventPromise).resolves.toBeDefined(); + + expect((await errorEventPromise).contexts?.trpc?.['procedure_type']).toBe('mutation'); + expect((await errorEventPromise).contexts?.trpc?.['procedure_path']).toBe('crashSomething'); }); test('Should record transaction and error for a trpc handler that returns a status code', async ({ baseURL }) => { 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 = { + 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; + async span => { + try { + const nextResult = await next(); + captureIfError(nextResult); + span.end(); + return nextResult; + } catch (e) { + captureException(e, trpcCaptureContext); + span.end(); + throw e; + } + }, + ) as SentryTrpcMiddleware; + }); }; }