-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
feat(node): Fork isolation scope in tRPC middleware #16296
Conversation
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
@@ -76,7 +76,7 @@ export function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) { | |||
} | |||
} | |||
|
|||
return withScope(scope => { | |||
return withIsolationScope(scope => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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".
There was a problem hiding this 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.
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