Skip to content

Request-specific context does not work with express #1939

Closed
@asabhaney

Description

@asabhaney

Package + Version

  • @sentry/browser
  • @sentry/node
  • raven-js
  • raven-node (raven for node)
  • other:

Version:

4.6.4

Description

I'm having a lot of issues setting request-specific context when using Sentry with Express. As a reference, I took a look at the official Express integration docs, as well as the following issue and PR:

const express = require('express')
const Sentry = require('@sentry/node')
const constants = require('./config/constants')

const api = express()

Sentry.init({
   dsn: constants.SENTRY_DSN_KEY,
   environment: constants.ENV_CURRENT
})

api.use(Sentry.Handlers.requestHandler())
api.use((req, res, next) => {
   console.log('Point A', Sentry.getHubFromCarrier(req))
   Sentry.getHubFromCarrier(req).configureScope(scope => {
      console.log('Point B')
      scope.setTag('test', 'Tag')
      scope.setExtra('test', req.path)
   })

   throw new Error('Request-specific context does not work')
})

api.use(Sentry.Handlers.errorHandler())
api.listen(constants.API_PORT)

Console Output:

Point A 
Hub {
  version: 3,
  stack: [ { client: undefined, scope: [Object] } ] }

Error: Request-specific context does not work
    at api.use (api/index.js:21:10)
    at Layer.handle [as handle_request] (api/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (api/node_modules/express/lib/router/index.js:317:13)
    at api/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (api/node_modules/express/lib/router/index.js:335:12)
    at next (api/node_modules/express/lib/router/index.js:275:10)
    at Domain.<anonymous> (api/node_modules/@sentry/node/dist/handlers.js:161:13)
    at Domain.run (domain.js:242:14)
    at sentryRequestMiddleware (api/node_modules/@sentry/node/dist/handlers.js:155:15)
    at Layer.handle [as handle_request] (api/node_modules/express/lib/router/layer.js:95:5)

Notice from the console output above:

  • that Point B is never output in the console
  • client in the stack of the Hub instance is undefined - not sure if this is related

Using node 8.14.0 and express 4.16.4.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions