Skip to content

Commit f74e161

Browse files
committed
Export as part of handlers
1 parent b5022f8 commit f74e161

File tree

3 files changed

+43
-46
lines changed

3 files changed

+43
-46
lines changed

packages/node/src/handlers.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
extractTraceparentData,
1111
isString,
1212
logger,
13+
normalize,
1314
} from '@sentry/utils';
1415
import * as domain from 'domain';
1516
import type * as http from 'http';
@@ -315,6 +316,48 @@ export function errorHandler(options?: {
315316
};
316317
}
317318

319+
interface SentryTrpcMiddlewareOptions {
320+
attachRpcInput?: boolean;
321+
}
322+
323+
interface TrpcMiddlewareArguments<T> {
324+
path: string;
325+
type: 'query' | 'mutation' | 'subscription';
326+
next: () => T;
327+
rawInput: unknown;
328+
}
329+
330+
/**
331+
* Sentry tRPC middleware that names the handling transaction after the called procedure.
332+
*
333+
* Use the Sentry tRPC middleware in combination with the Sentry server integration,
334+
* e.g. Express Request Handlers or Next.js SDK.
335+
*/
336+
export async function trpcMiddleware(options: SentryTrpcMiddlewareOptions = {}) {
337+
return function <T>({ path, type, next, rawInput }: TrpcMiddlewareArguments<T>): T {
338+
const hub = getCurrentHub();
339+
const clientOptions = hub.getClient()?.getOptions();
340+
const sentryTransaction = hub.getScope()?.getTransaction();
341+
342+
if (sentryTransaction) {
343+
sentryTransaction.setName(`trcp/${path}`, 'route');
344+
sentryTransaction.op = 'rpc.server';
345+
346+
const trpcData: Record<string, unknown> = {
347+
procedureType: type,
348+
};
349+
350+
if (options.attachRpcInput !== undefined ? options.attachRpcInput : clientOptions?.sendDefaultPii) {
351+
trpcData.procedureInput = normalize(rawInput);
352+
}
353+
354+
sentryTransaction.setData('trpc', trpcData);
355+
}
356+
357+
return next();
358+
};
359+
}
360+
318361
// TODO (v8 / #5257): Remove this
319362
// eslint-disable-next-line deprecation/deprecation
320363
export type { ParseRequestOptions, ExpressRequest } from './requestDataDeprecated';

packages/node/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ const INTEGRATIONS = {
6565

6666
export { INTEGRATIONS as Integrations, Handlers };
6767

68-
export { trpcMiddleware } from './trpc';
69-
7068
// We need to patch domain on the global __SENTRY__ object to make it work for node in cross-platform packages like
7169
// @sentry/core. If we don't do this, browser bundlers will have troubles resolving `require('domain')`.
7270
const carrier = getMainCarrier();

packages/node/src/trpc.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)