-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(node): Add Sentry tRPC middleware #7511
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
Changes from 5 commits
043013f
4d16e63
7cc473d
003095f
b5022f8
f74e161
8ab4c72
6ff778e
3586c36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { getCurrentHub } from '@sentry/core'; | ||
import { normalize } from '@sentry/utils'; | ||
|
||
interface SentryTrpcMiddlewareOptions { | ||
attachRpcInput?: boolean; | ||
} | ||
|
||
interface TrpcMiddlewareArguments<T> { | ||
path: string; | ||
type: 'query' | 'mutation' | 'subscription'; | ||
next: () => T; | ||
rawInput: unknown; | ||
} | ||
|
||
/** | ||
* Sentry tRPC middleware that names the handling transaction after the called procedure. | ||
* | ||
* Use the Sentry tRPC middleware in combination with the Sentry server integration, | ||
* e.g. Express Request Handlers or Next.js SDK. | ||
*/ | ||
export async function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) { | ||
return function <T>({ path, type, next, rawInput }: TrpcMiddlewareArguments<T>): T { | ||
const hub = getCurrentHub(); | ||
const clientOptions = hub.getClient()?.getOptions(); | ||
const sentryTransaction = hub.getScope()?.getTransaction(); | ||
|
||
if (sentryTransaction) { | ||
sentryTransaction.setName(`trcp/${path}`, 'route'); | ||
sentryTransaction.op = 'rpc.server'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/rpc.md I don't see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, I think it is fine to append stuff to the span op. I think there is a hierarchy to it. The docs are just a guide to set the baseline ops we should use. Here I decided to append There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this is fine, we can update our span operations spec to account for this https://develop.sentry.dev/sdk/performance/span-operations/ |
||
|
||
const trpcData: Record<string, unknown> = { | ||
procedureType: type, | ||
}; | ||
|
||
if (options.attachRpcInput !== undefined ? options.attachRpcInput : clientOptions?.sendDefaultPii) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be somewhat confusing to users. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
trpcData.procedureInput = normalize(rawInput); | ||
} | ||
|
||
sentryTransaction.setData('trpc', trpcData); | ||
} | ||
|
||
return next(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to log something if the procedure actually fails? https://trpc.io/docs/server/middlewares#logging There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, but for now I don't think we need to add this. People will still need to use this integration with a proper server framework integration like the Express integration for it to work, and those integrations will handle the errors. We could at some point add an option that allows capturing of non-200 responses. |
||
}; | ||
} |
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.
h: let's put this under
Handlers
for now. I don't think we need to worry about tree-shaking and this matches the pattern we have with the rest of the SDK. We can re-evaluate this at a later time.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.
Done! f74e161