Skip to content

Add trpc path to context in trpcMiddleware #14158

Closed
@phuctm97

Description

@phuctm97

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>;
};
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions