Skip to content

Commit a579fba

Browse files
phuctm97lforst
andauthored
feat(core): Add trpc path to context in trpcMiddleware (#14218)
Resolves #14158 Before submitting a pull request, please take a look at our [Contributing](https://github.com/getsentry/sentry-javascript/blob/master/CONTRIBUTING.md) guidelines and verify: - [x] If you've added code that should be tested, please add tests. - [x] Ensure your code lints and the test suite passes (`yarn lint`) & (`yarn test`). --------- Co-authored-by: Luca Forstner <luca.forstner@sentry.io>
1 parent e9d8ec8 commit a579fba

File tree

4 files changed

+34
-43
lines changed

4 files changed

+34
-43
lines changed

dev-packages/e2e-tests/test-applications/nextjs-t3/tests/trpc-error.test.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ test('should capture error with trpc context', async ({ page }) => {
1212
const trpcError = await errorEventPromise;
1313

1414
expect(trpcError).toBeDefined();
15-
expect(trpcError.contexts.trpc).toBeDefined();
16-
expect(trpcError.contexts.trpc.procedure_type).toEqual('mutation');
17-
expect(trpcError.contexts.trpc.input).toEqual({ name: 'I love dogs' });
15+
expect(trpcError.contexts?.trpc).toBeDefined();
16+
expect(trpcError.contexts?.trpc?.procedure_type).toEqual('mutation');
17+
expect(trpcError.contexts?.trpc?.procedure_path).toBe('post.throwError');
18+
expect(trpcError.contexts?.trpc?.input).toEqual({ name: 'I love dogs' });
1819
});
1920

2021
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 }) =>
2627
await page.click('#error-button');
2728

2829
const trpcTransaction = await trpcTransactionPromise;
29-
3030
expect(trpcTransaction).toBeDefined();
31-
expect(trpcTransaction.contexts.trpc).toBeDefined();
32-
expect(trpcTransaction.contexts.trpc.procedure_type).toEqual('mutation');
33-
expect(trpcTransaction.contexts.trpc.input).toEqual({ name: 'I love dogs' });
3431
});

dev-packages/e2e-tests/test-applications/nextjs-t3/tests/trpc-mutation.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,4 @@ test('should create transaction with trpc input for mutation', async ({ page })
1313
const trpcTransaction = await trpcTransactionPromise;
1414

1515
expect(trpcTransaction).toBeDefined();
16-
expect(trpcTransaction.contexts.trpc).toBeDefined();
17-
expect(trpcTransaction.contexts.trpc.procedure_type).toEqual('mutation');
18-
expect(trpcTransaction.contexts.trpc.input).toEqual({ name: 'I love dogs' });
1916
});

dev-packages/e2e-tests/test-applications/node-express/tests/trpc.test.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ test('Should record span for trpc query', async ({ baseURL }) => {
3333
description: `trpc/getSomething`,
3434
}),
3535
);
36-
37-
expect(transaction.contexts?.trpc).toMatchObject({
38-
procedure_type: 'query',
39-
input: 'foobar',
40-
});
4136
});
4237

4338
test('Should record transaction for trpc mutation', async ({ baseURL }) => {
@@ -70,10 +65,6 @@ test('Should record transaction for trpc mutation', async ({ baseURL }) => {
7065
description: `trpc/createSomething`,
7166
}),
7267
);
73-
74-
expect(transaction.contexts?.trpc).toMatchObject({
75-
procedure_type: 'mutation',
76-
});
7768
});
7869

7970
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 ({
10091

10192
await expect(transactionEventPromise).resolves.toBeDefined();
10293
await expect(errorEventPromise).resolves.toBeDefined();
94+
95+
expect((await errorEventPromise).contexts?.trpc?.['procedure_type']).toBe('mutation');
96+
expect((await errorEventPromise).contexts?.trpc?.['procedure_path']).toBe('crashSomething');
10397
});
10498

10599
test('Should record transaction and error for a trpc handler that returns a status code', async ({ baseURL }) => {

packages/core/src/trpc.ts

+27-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { normalize } from '@sentry/utils';
22

3-
import { getClient } from './currentScopes';
4-
import { captureException, setContext } from './exports';
3+
import { getClient, withScope } from './currentScopes';
4+
import { captureException } from './exports';
55
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from './semanticAttributes';
66
import { startSpanManual } from './tracing';
77

@@ -48,6 +48,7 @@ export function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) {
4848
const clientOptions = client && client.getOptions();
4949

5050
const trpcContext: Record<string, unknown> = {
51+
procedure_path: path,
5152
procedure_type: type,
5253
};
5354

@@ -66,29 +67,31 @@ export function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) {
6667
}
6768
}
6869
}
69-
setContext('trpc', trpcContext);
7070

71-
return startSpanManual(
72-
{
73-
name: `trpc/${path}`,
74-
op: 'rpc.server',
75-
attributes: {
76-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
77-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.rpc.trpc',
71+
return withScope(scope => {
72+
scope.setContext('trpc', trpcContext);
73+
return startSpanManual(
74+
{
75+
name: `trpc/${path}`,
76+
op: 'rpc.server',
77+
attributes: {
78+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
79+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.rpc.trpc',
80+
},
7881
},
79-
},
80-
async span => {
81-
try {
82-
const nextResult = await next();
83-
captureIfError(nextResult);
84-
span.end();
85-
return nextResult;
86-
} catch (e) {
87-
captureException(e, trpcCaptureContext);
88-
span.end();
89-
throw e;
90-
}
91-
},
92-
) as SentryTrpcMiddleware<T>;
82+
async span => {
83+
try {
84+
const nextResult = await next();
85+
captureIfError(nextResult);
86+
span.end();
87+
return nextResult;
88+
} catch (e) {
89+
captureException(e, trpcCaptureContext);
90+
span.end();
91+
throw e;
92+
}
93+
},
94+
) as SentryTrpcMiddleware<T>;
95+
});
9396
};
9497
}

0 commit comments

Comments
 (0)