Skip to content

feat(node): Fork isolation scope in tRPC middleware #16296

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

Merged
merged 1 commit into from
May 15, 2025

Conversation

andreiborza
Copy link
Member

Writing tRPC handlers today and using top-level methods like Sentry.setTag isn't very intuitive as the isolations scope is not forked per procedure in our middleware.

This PR changes the middleware to fork the isolation scope, while this is not 100% correct, as it breaks the one isolation scope per process/request model, it should be more intuitive and work better for most users.

Resolves: #16262

Writing tRPC handlers today and using top-level methods like `Sentry.setTag`
isn't very intuitive as the isolations scope is not forked per procedure in our
middleware.

This PR changes the middleware to fork the isolation scope, while this is not
100% correct, as it breaks the one isolation scope per process/request model,
it should be more intuitive and work better for most users.

Resolves: #16262
@andreiborza andreiborza requested review from mydea, lforst and Lms24 May 14, 2025 15:02
@mydea mydea changed the title feat(node): Adds isolation scope forking to tRPC middleware feat(node): Fork isolation scope in tRPC middleware May 14, 2025
@@ -76,7 +76,7 @@ export function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) {
}
}

return withScope(scope => {
return withIsolationScope(scope => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a check for something like so?

getIsolationScope() !== getDefaultIsolationScope()

so that we only use an isolation scope if it hasn't been explicitly set? This way we don't break server-use cases, like using trpcMiddleware in your express app.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be fine even on express. Middleware only runs on the server. What are your concerns?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just worried we rebind a new isolation scope which interferes with a framework integration.

Like if you use the trpc middleware in a cloudflare worker: https://trpc.io/docs/server/adapters/fetch#create-cloudflare-worker

import * as Sentry from "@sentry/cloudflare";
import { initTRPC } from '@trpc/server';
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
import { createContext } from './context';

const t = initTRPC.context().create();
const sentryMiddleware = t.middleware(
  Sentry.trpcMiddleware({
    attachRpcInput: true,
  }),
);
const sentrifiedProcedure = t.procedure.use(sentryMiddleware);
export const appRouter = t.router({
  getUserById: sentrifiedProcedure.input(...),
});

export default Sentry.withSentry(() => {}, {
  async fetch(request: Request): Promise<Response> {
    return fetchRequestHandler({
      endpoint: '/trpc',
      req: request,
      router: appRouter,
      createContext,
    });
  },
});

withSentry creates an isolation scope around fetch, but so would the getUserById trpc procedure. In this use case, we can just rely on the isolation scope from the fetch handler instrumentation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this is that the isolation scope is already different from the default isolation scope, but the scope remains the same between the two middleware invocations which is why we end up with issue like tags "leaking".

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's go for it - we can always re-evaluate this later.

@andreiborza andreiborza merged commit 6d63705 into develop May 15, 2025
312 of 314 checks passed
@andreiborza andreiborza deleted the ab/trpc-middleware-isolation-scope-forking branch May 15, 2025 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TRPC Middleware Context not Working as Expected with Batch Requests
4 participants