-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request-specific context does not work with express #1939
Comments
@asabhaney when using Your code will work just fine if you replace (remember about However, sentry-javascript/packages/hub/src/hub.ts Lines 335 to 359 in f50e025
|
Some additional docs on the https://docs.sentry.io/platforms/node/express/ page would be great. It would stop other people going down the same rabbit hole. |
I apologize for the delayed response on this. I have upgraded to Here is my updated code: 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,
beforeSend(event) {
console.log('beforeSend', event.user.id)
return event
}
})
api.use(Sentry.Handlers.requestHandler())
api.use(session({ ... })) // Sets req.user
// Set user for Sentry context
api.use((req, res, next) => {
if (req.user) {
console.log('Point A', req.user.id)
Sentry.getCurrentHub().configureScope((scope) => {
console.log('Point B', req.user.id)
scope.setUser({ id: req.user.id })
})
}
next()
})
// Throw an error for all requests
api.use((req, res, next) => {
throw new Error('Uh-oh Test')
})
api.use(Sentry.Handlers.errorHandler())
api.use((err, req, res, next) => {
const status = err.status || 500
const msg = err.message || 'Internal Server Error'
if (err instanceof errors.ApiError && typeof err.serialize === 'function') {
res.status(status).json(err.serialize())
} else {
res.status(status).json({ name: 'API Error', message: msg })
}
})
api.listen(constants.API_PORT) So I fire two requests to the node server from Safari, which produces the following log output:
(For simplicity, I've replaced the actual This looks correct, including in the Sentry dashboard. Now I clear all the issues in the Sentry dashboard, then switch to Chrome, where I am logged in as a different user (represented below as
In addition to the incorrect user being logged, when I look at the new issues that appear in the Sentry dashboard, they still show Safari as being the browser which triggered the requests (should be Chrome in this case). Not totally sure, but it could be related to these issues: |
@asabhaney how is My code: https://gist.github.com/kamilogorek/654d471ebbcab3dc20a81d7a1bfe25a7 (copy/pasted yours - constants and error sending) Results:
|
Thanks for the reply @kamilogorek. Fascinating, I didn't think to question whether the session middleware might be causing the issue, but I can confirm that is in fact the culprit. Specifically, I am using express-session with connect-mongodb-session, but it seems to be the mongodb session store that's causing the issues, as the Sentry stuff all works as expected when using Interestingly, I tried a different session store library, connect-mongo, and that seems to work without any issues as well. I wanted to use I'll see if I can take a look through the |
We had a similar issue in our code caused by the combination of
changed to
This seems to be an issue that can be caused by several middlewares. It would be nice if we could figure out what's causing the issue, and how to either prevent it or fix the other misbehaving middleware. |
I tried to use passport today to break it with no luck... I'll close the issue for now, but please feel free to link me any working (I mean broken :D) repro-case and I'll reopen it and take a look! :) |
I dug into this a little more, and the bug is triggered specifically by any middleware that interacts with mongodb. The mongodb connection pool loses the current process domain. That's why vanilla passport didn't repro it, but e.g. connect-mongodb-session did. Automattic/mongoose#4803 is an example of this behavior being reported as a bug in Mongoose. The workaround described there is to pass I unfortunately haven't done the work of extracting a minimal example, but I verified these two behaviors in my own app:
|
Package + Version
@sentry/browser
@sentry/node
raven-js
raven-node
(raven for node)Version:
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:
Console Output:
Notice from the console output above:
Point B
is never output in the consoleclient
in thestack
of theHub
instance isundefined
- not sure if this is relatedUsing node
8.14.0
and express4.16.4
.The text was updated successfully, but these errors were encountered: