Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add trpc path to context in trpcMiddleware #14218

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;
});
};
}
Loading