Skip to content

[7.108.0] Sentry.Handlers.tracingHandler uses incorret route name in Express server  #11337

Closed

Description

@lhermann

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

7.108.0

Framework Version

Node 18, Express 4.19.2

Link to Sentry event

SDK Setup

import * as Sentry from '@sentry/node'
import { nodeProfilingIntegration } from '@sentry/profiling-node'
import express from 'express'

const app = express()

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  enabled: Boolean(process.env.SENTRY_DSN),
  release: process.env.npm_package_version,
  environment: process.env.SENTRY_ENV,
  serverName: process.env.SERVER_ID,
  integrations: [
    new Sentry.Integrations.Express({ app }),
    nodeProfilingIntegration(),
  ],
  sampleRate: 1,
  tracesSampler: (samplingContext) => {
    console.log('[Sentry]', { samplingContext: samplingContext.transactionContext.name })
    return 0.05
  },
  profilesSampleRate: 0.2,
})

const router = express.Router()

router.use(Sentry.Handlers.requestHandler())
router.use(Sentry.Handlers.tracingHandler())

router.get('/test/:first/:second', (req, res) => {
  console.log('[Test Route]', { 'req.route.path': req.route.path })
  console.log('[Test Route]', { 'req.originalUrl': req.originalUrl })
  res.sendStatus(200)
})

router.use(Sentry.Handlers.errorHandler())

app.use(router)

app.listen(process.env.PORT, () => {
  console.log(`Example app listening on port ${process.env.PORT}`)
})

Steps to Reproduce

  1. Start the express test server from the code above node -r dotenv/config index.js.
  2. Trigger a GET request to the example route GET http://127.0.0.1:3000/test/hello/world.
  3. Observe the console output.

Expected Result

[Sentry] { samplingContext: 'GET /test/:first/:second' }
[Test Route] { 'req.route.path': '/test/:first/:second' }
[Test Route] { 'req.originalUrl': '/test/hello/world' }

I expect the Sentry Trace to use the route name /test/:first/:second.

Actual Result

[Sentry] { samplingContext: 'GET /test/hello/world' }
[Test Route] { 'req.route.path': '/test/:first/:second' }
[Test Route] { 'req.originalUrl': '/test/hello/world' }

The Sentry Trace uses the originalUrl /test/hello/world instead of the router path name.
As an effect, the performance dashboard is nearly useless, because all requests are filed under different names.

Observations:

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Mar 28, 2024
  2. mydea commented on Mar 28, 2024

    @mydea
    Member

    Hello, in the upcoming v8 version we use a new instrumentation base, where this may (or may not, hard to say) be fixed already. Could you give it a try with 8.0.0-alpha.7, just to check if this is also the case there? If you don't want to/cannot try the alpha, we can wait until a more stable release to test this then!

  3. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Mar 28, 2024
  4. lhermann commented on Mar 28, 2024

    @lhermann
    Author

    I'm using "type": "module" with import * as Sentry from '@sentry/node' which seems not supported in v8.

  5. moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Mar 28, 2024
  6. souterjk commented on Mar 29, 2024

    @souterjk

    Hey @mydea we've got the same problem on basically the same setup (express, node, version 7 of Sentry, etc.). I tried to move over to version 8 alpha so I could tell you if its working or not, but I can't even figure out how to set v8 up for node. Can you help me with the v8 equivalent of this setup block? Almost all of the integration paths are no longer importable, and I was able to figure most of that part out by moving the paths around, but the middlewares for app.use seem completely gone in v8?

    Also does the express integration no longer require the app (the express router) parameter in the new version?

    Sentry.init({
        //a bunch of other irrelevant stuff...
        integrations: app
          ? [
              new Sentry.Integrations.Http({ tracing: true }),
              new Sentry.Integrations.Express({ app }),
              new Sentry.Integrations.Postgres(),
              nodeProfilingIntegration(),
              ...(anrThreshold
                ? [new Sentry.Integrations.Anr({ captureStackTrace: true, anrThreshold })]
                : []),
            ]
          : [
              new Sentry.Integrations.Http({ tracing: true }),
              new Sentry.Integrations.Postgres(),
              nodeProfilingIntegration(),
            ],
      });
      if (app) {
        // The request handler must be the first middleware on the app
        app.use(Sentry.Handlers.requestHandler());
        app.use(Sentry.Handlers.tracingHandler());
      }
    

    Also the link here for how to set this up is completely dead 😢

  7. souterjk commented on Mar 29, 2024

    @souterjk

    Circling back a few hours later, the doc I needed was here . This is indeed fixed in the v8 alpha. Is there any chance that whatever fixed the transaction name grouping can get ported back into v7? Unless v8 is close to a full release, this is a pretty big breakage in the most recent v7.

  8. mydea commented on Apr 2, 2024

    @mydea
    Member

    Hey, sadly the code for this is completely different in v8 than it was in v7. So it is hard to backport this, I fear. However, a full release of v8 shouldn't be too far off - we are planning to release a beta in the next week or so!

  9. 4 remaining items

  10. moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Apr 15, 2024
  11. mydea commented on Apr 16, 2024

    @mydea
    Member

    We have just released v8.0.0-beta.1 - could you give that a try and let us now if it works for you as expected on that version?

  12. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Apr 16, 2024
  13. lhermann commented on May 24, 2024

    @lhermann
    Author

    @mydea I can confirm that route names are correctly identified in @sentry/node@8.

    I'm using "type": "module" and am starting the test application with node -r dotenv/config --import ./instrument.js index.js.

    instrument.js

    import * as Sentry from '@sentry/node'
    
    Sentry.init({
      dsn: process.env.SENTRY_DSN,
      enabled: Boolean(process.env.SENTRY_DSN),
      release: process.env.npm_package_version,
      environment: process.env.SENTRY_ENV,
      serverName: process.env.SERVER_ID,
      sampleRate: 1,
      tracesSampler: (samplingContext) => {
        console.log('[Sentry] transactionContext', samplingContext.transactionContext)
        return 1
      },
    })

    The log messages, while showing much more activity than v7, seems to identify the route name correctly:

    Example app listening on port 3000
    [Sentry] transactionContext { name: 'GET', parentSampled: undefined }
    [Sentry] transactionContext { name: 'middleware - query', parentSampled: true }
    [Sentry] transactionContext { name: 'middleware - expressInit', parentSampled: true }
    [Sentry] transactionContext { name: 'router - /', parentSampled: true }
    [Sentry] transactionContext { name: 'request handler - /test/:first/:second', parentSampled: true }
    [Test Route] { 'req.route.path': '/test/:first/:second' }
    [Test Route] { 'req.originalUrl': '/test/foo/bar/' }

    However, on my production app I run into the "SDK fails in ESM mode in combination with date-fns" error and had to disable the instrumentation 😢: #12154

  14. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on May 24, 2024
  15. mydea commented on May 27, 2024

    @mydea
    Member

    Thank you for the feedback! It's good to hear that this is generally resolved.
    We are working on resolving the date-fns issue as soon as possible 😬

  16. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on May 27, 2024
  17. AbhiPrasad commented on Jun 10, 2024

    @AbhiPrasad
    Contributor

    We've fixed the date-fns issues with https://github.com/getsentry/sentry-javascript/releases/tag/8.8.0 - please give it a try!

  18. ghost added on Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions