Skip to content

event.transaction reports wrong URL when Express is used with a Router #3004

Closed
@Rush

Description

@Rush

Important Details

How are you running Sentry? @sentry/node@5.15.4
Environment: N/A, can reproduce both locally, in docker and anywhere else

Description

When doing:

const router = new Router();
router.get('/bar', (req, res, next) => {  next(new Errror('Foo'));  });
app.use('/foo', someOtherRouter);

sentry would set the event.transaction to /bar as this is what req.url will point to.

See this doc.
https://expressjs.com/en/api.html#req.originalUrl

Steps to Reproduce

  1. Setup routing using express router https://expressjs.com/en/api.html#router
  2. Handle an exception thrown from the router.
  3. event.transaction (and issue title in Sentry) will include only the router's part of the URL

What you expected to happen

Given my example in the description, I would expect to see full URL:

GET|/foo/bar

Possible Solution

Sentry should try to get the URL from req.originalUrl. I am currently using this workaround:

const sentryRequestHandler = Sentry.Handlers.requestHandler();
  app.use((req, res, next) => {
    sentryRequestHandler(req, res, () => {
      Sentry.getCurrentHub().configureScope(scope => {
        scope.addEventProcessor((event) => {
          event.transaction = `${req.method}|${req.originalUrl}`;
          return event;
        });
      });
      next();
    });
  });

This is patching the transaction to use .originalUrl

Metadata

Metadata

Assignees

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