Skip to content

Commit 1d96f8b

Browse files
authored
fix(tracing): automatically instrument server-side requests (#514)
1 parent 7933761 commit 1d96f8b

File tree

4 files changed

+11
-0
lines changed

4 files changed

+11
-0
lines changed

lib/core/hooks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ export async function initializeServerSentry (moduleContainer, moduleOptions, se
168168
Sentry.init(config)
169169
sentryHandlerProxy.errorHandler = Sentry.Handlers.errorHandler()
170170
sentryHandlerProxy.requestHandler = Sentry.Handlers.requestHandler(moduleOptions.requestHandlerConfig)
171+
if (serverOptions.tracing) {
172+
sentryHandlerProxy.tracingHandler = Sentry.Handlers.tracingHandler()
173+
}
171174
}
172175

173176
process.sentry = Sentry

lib/core/options.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ function resolveTracingOptions (options, config) {
116116
config.tracesSampleRate = tracingOptions.tracesSampleRate
117117
}
118118
options.tracing = tracingOptions
119+
// Enable tracing for `Http` integration.
120+
options.serverIntegrations = merge({ Http: { tracing: true } }, options.serverIntegrations || {})
119121
}
120122

121123
/**

lib/module.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,14 @@ export default async function SentryModule (moduleOptions) {
109109
const sentryHandlerProxy = {
110110
errorHandler: (error, req, res, next) => { next(error) },
111111
requestHandler: (req, res, next) => { next() },
112+
tracingHandler: (req, res, next) => { next() },
112113
}
113114
// @ts-ignore
114115
this.nuxt.hook('render:setupMiddleware', app => app.use((req, res, next) => { sentryHandlerProxy.requestHandler(req, res, next) }))
116+
if (options.tracing) {
117+
// @ts-ignore
118+
this.nuxt.hook('render:setupMiddleware', app => app.use((req, res, next) => { sentryHandlerProxy.tracingHandler(req, res, next) }))
119+
}
115120
// @ts-ignore
116121
this.nuxt.hook('render:errorMiddleware', app => app.use((error, req, res, next) => { sentryHandlerProxy.errorHandler(error, req, res, next) }))
117122
// @ts-ignore

types/sentry.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { NodeOptions, Handlers } from '@sentry/node'
1010
export interface SentryHandlerProxy {
1111
errorHandler: (error: any, req: IncomingMessage, res: ServerResponse, next: (error: any) => void) => void
1212
requestHandler: (req: IncomingMessage, res: ServerResponse, next: (error?: any) => void) => void
13+
tracingHandler: (req: IncomingMessage, res: ServerResponse, next: (error?: any) => void) => void
1314
}
1415

1516
export type IntegrationsConfiguration = Record<string, unknown>

0 commit comments

Comments
 (0)