Skip to content

Commit

Permalink
feat(core): Add trpc path to context in trpcMiddleware (#14218)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
phuctm97 and lforst authored Nov 11, 2024
1 parent e9d8ec8 commit a579fba
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand All @@ -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' });
});
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -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 }) => {
Expand All @@ -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 }) => {
Expand Down
51 changes: 27 additions & 24 deletions packages/core/src/trpc.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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,
};

Expand All @@ -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>;
});
};
}

0 comments on commit a579fba

Please sign in to comment.