From 1d96f8bb5c85d3c3247b518c97910cf32a8a268b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Wed, 1 Mar 2023 15:48:10 +0100 Subject: [PATCH] fix(tracing): automatically instrument server-side requests (#514) --- lib/core/hooks.js | 3 +++ lib/core/options.js | 2 ++ lib/module.js | 5 +++++ types/sentry.d.ts | 1 + 4 files changed, 11 insertions(+) diff --git a/lib/core/hooks.js b/lib/core/hooks.js index f2304e08..921d7a72 100644 --- a/lib/core/hooks.js +++ b/lib/core/hooks.js @@ -168,6 +168,9 @@ export async function initializeServerSentry (moduleContainer, moduleOptions, se Sentry.init(config) sentryHandlerProxy.errorHandler = Sentry.Handlers.errorHandler() sentryHandlerProxy.requestHandler = Sentry.Handlers.requestHandler(moduleOptions.requestHandlerConfig) + if (serverOptions.tracing) { + sentryHandlerProxy.tracingHandler = Sentry.Handlers.tracingHandler() + } } process.sentry = Sentry diff --git a/lib/core/options.js b/lib/core/options.js index 8108351b..a60a1d07 100644 --- a/lib/core/options.js +++ b/lib/core/options.js @@ -116,6 +116,8 @@ function resolveTracingOptions (options, config) { config.tracesSampleRate = tracingOptions.tracesSampleRate } options.tracing = tracingOptions + // Enable tracing for `Http` integration. + options.serverIntegrations = merge({ Http: { tracing: true } }, options.serverIntegrations || {}) } /** diff --git a/lib/module.js b/lib/module.js index 07d2bd77..67724270 100644 --- a/lib/module.js +++ b/lib/module.js @@ -109,9 +109,14 @@ export default async function SentryModule (moduleOptions) { const sentryHandlerProxy = { errorHandler: (error, req, res, next) => { next(error) }, requestHandler: (req, res, next) => { next() }, + tracingHandler: (req, res, next) => { next() }, } // @ts-ignore this.nuxt.hook('render:setupMiddleware', app => app.use((req, res, next) => { sentryHandlerProxy.requestHandler(req, res, next) })) + if (options.tracing) { + // @ts-ignore + this.nuxt.hook('render:setupMiddleware', app => app.use((req, res, next) => { sentryHandlerProxy.tracingHandler(req, res, next) })) + } // @ts-ignore this.nuxt.hook('render:errorMiddleware', app => app.use((error, req, res, next) => { sentryHandlerProxy.errorHandler(error, req, res, next) })) // @ts-ignore diff --git a/types/sentry.d.ts b/types/sentry.d.ts index 6ced7e37..457769b6 100644 --- a/types/sentry.d.ts +++ b/types/sentry.d.ts @@ -10,6 +10,7 @@ import { NodeOptions, Handlers } from '@sentry/node' export interface SentryHandlerProxy { errorHandler: (error: any, req: IncomingMessage, res: ServerResponse, next: (error: any) => void) => void requestHandler: (req: IncomingMessage, res: ServerResponse, next: (error?: any) => void) => void + tracingHandler: (req: IncomingMessage, res: ServerResponse, next: (error?: any) => void) => void } export type IntegrationsConfiguration = Record