-
-
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 6 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 |
---|---|---|
|
@@ -10,6 +10,7 @@ import { | |
extractTraceparentData, | ||
isString, | ||
logger, | ||
normalize, | ||
} from '@sentry/utils'; | ||
import * as domain from 'domain'; | ||
import type * as http from 'http'; | ||
|
@@ -315,6 +316,48 @@ export function errorHandler(options?: { | |
}; | ||
} | ||
|
||
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'; | ||
|
||
const trpcData: Record<string, unknown> = { | ||
procedureType: type, | ||
}; | ||
|
||
if (options.attachRpcInput !== undefined ? options.attachRpcInput : clientOptions?.sendDefaultPii) { | ||
trpcData.procedureInput = normalize(rawInput); | ||
} | ||
|
||
sentryTransaction.setData('trpc', trpcData); | ||
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.
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. I think we had this exact discussion before ^^ Data definitely appears in the transaction view: But I agree that a context is more valuable: 6ff778e 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. Ah it does appear in trace context!! Keep forgetting 🤦 |
||
} | ||
|
||
return next(); | ||
}; | ||
} | ||
|
||
// TODO (v8 / #5257): Remove this | ||
// eslint-disable-next-line deprecation/deprecation | ||
export type { ParseRequestOptions, ExpressRequest } from './requestDataDeprecated'; | ||
|
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.
m: can we add a doc string?
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.
👍 8ab4c72