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

Add trpc path to context in trpcMiddleware #14158

Closed
phuctm97 opened this issue Nov 1, 2024 · 3 comments · Fixed by #14218
Closed

Add trpc path to context in trpcMiddleware #14158

phuctm97 opened this issue Nov 1, 2024 · 3 comments · Fixed by #14218

Comments

@phuctm97
Copy link
Contributor

phuctm97 commented Nov 1, 2024

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?

export function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return async function <T>(opts: SentryTrpcMiddlewareArguments<T>): SentryTrpcMiddleware<T> {
const { path, type, next, rawInput, getRawInput } = opts;
const client = getClient();
const clientOptions = client && client.getOptions();
const trpcContext: Record<string, unknown> = {
procedure_type: type,
};
if (options.attachRpcInput !== undefined ? options.attachRpcInput : clientOptions && clientOptions.sendDefaultPii) {
if (rawInput !== undefined) {
trpcContext.input = normalize(rawInput);
}
if (getRawInput !== undefined && typeof getRawInput === 'function') {
try {
const rawRes = await getRawInput();
trpcContext.input = normalize(rawRes);
} catch (err) {
// noop
}
}
}
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>;
};
}

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Nov 1, 2024
@AbhiPrasad
Copy link
Member

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?

@phuctm97
Copy link
Contributor Author

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: #14218

Copy link
Contributor

A PR closing this issue has just been released 🚀

This issue was referenced by PR #14218, which was included in the 8.38.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants