Skip to content

Tracing integration with express erases router metadata #5050

Description

@jondot

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/node

SDK Version

6.19.7

Framework Version

6.19.7

Link to Sentry event

No response

Steps to Reproduce

Integrate sentry with an existing express app.
Use this to list routes:

import path from 'path'

function getPathFromRegex(regexp) {
  return regexp
    .toString()
    .replace('/^', '')
    .replace('?(?=\\/|$)/i', '')
    .replace(/\\\//g, '/')
}

function combineStacks(acc, stack) {
  if (stack.handle.stack) {
    const routerPath = getPathFromRegex(stack.regexp)
    return [
      ...acc,
      ...stack.handle.stack.map((stack) => ({ routerPath, ...stack })),
    ]
  }
  return [...acc, stack]
}

function getStacks(app) {
  // Express 3
  if (app.routes) {
    // convert to express 4
    return Object.keys(app.routes)
      .reduce((acc, method) => [...acc, ...app.routes[method]], [])
      .map((route) => ({ route: { stack: [route] } }))
  }

  // Express 4
  if (app._router && app._router.stack) {
    return app._router.stack.reduce(combineStacks, [])
  }

  // Express 4 Router
  if (app.stack) {
    return app.stack.reduce(combineStacks, [])
  }

  // Express 5
  if (app.router && app.router.stack) {
    return app.router.stack.reduce(combineStacks, [])
  }

  return []
}

function listRoutes(app) {
  const stacks = getStacks(app)
  const routes = []

  if (stacks) {
    for (const stack of stacks) {
      if (stack.route) {
        const routeLogged = {}
        for (const route of stack.route.stack) {
          const method = route.method ? route.method.toUpperCase() : null
          if (!routeLogged[method] && method) {
            const stackPath =
              stack.routerPath || route.path
                ? path.resolve(
                    [stack.routerPath, stack.route.path, route.path]
                      .filter((s) => !!s)
                      .join('')
                  )
                : stack.route.path
            routes.push({ verb: method, path: stackPath })
            routeLogged[method] = true
          }
        }
      }
    }
  }
  return routes
}

export default listRoutes

Expected Result

routes listed and returned

Actual Result

after integration with Sentry, all route information disappears.

Activity

  1. lforst commented on May 9, 2022

    @lforst
    Contributor

    Hi @jondot, thanks for raising this issue. I've backlogged this to look into it.

    Are you using the Express integration?

  2. kamilogorek commented on Nov 27, 2022

    @kamilogorek
    Contributor

    Can't repro, works just fine with your code:

    Sentry Logger [Log]: Integration installed: InboundFilters
    Sentry Logger [Log]: Integration installed: FunctionToString
    Sentry Logger [Log]: Integration installed: Console
    Sentry Logger [Log]: Integration installed: Http
    Sentry Logger [Log]: Integration installed: OnUncaughtException
    Sentry Logger [Log]: Integration installed: OnUnhandledRejection
    Sentry Logger [Log]: Integration installed: LinkedErrors
    Sentry Logger [Log]: [Tracing] starting test transaction - My First Test Transaction
    Sentry Logger [Log]: [Tracing] Finishing test transaction: My First Test Transaction.
    [
      { verb: 'GET', path: '/foo' },
      { verb: 'GET', path: '/bar' },
      { verb: 'POST', path: '/baz/:foo' },
      { verb: 'GET', path: '/baz/:foo/quz' }
    ]
    Example app listening at http://localhost:3000
    
  3. github-actions commented on Dec 19, 2022

    @github-actions
    Contributor

    This issue has gone three weeks without activity. In another week, I will close it.

    But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


    "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions