Skip to content

Commit

Permalink
fix(tracing): automatically instrument server-side requests (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl authored Mar 1, 2023
1 parent 7933761 commit 1d96f8b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/core/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/core/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {})
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions types/sentry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>
Expand Down

0 comments on commit 1d96f8b

Please sign in to comment.