Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Tracing integration with express erases router metadata #5050

Closed
3 tasks done
jondot opened this issue May 7, 2022 · 3 comments
Closed
3 tasks done

Tracing integration with express erases router metadata #5050

jondot opened this issue May 7, 2022 · 3 comments
Labels
Package: node Issues related to the Sentry Node SDK Type: Bug

Comments

@jondot
Copy link

jondot commented May 7, 2022

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.

@lforst lforst added Package: node Issues related to the Sentry Node SDK Status: Backlog and removed Status: Untriaged labels May 9, 2022
@lforst
Copy link
Member

lforst commented May 9, 2022

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

Are you using the Express integration?

@kamilogorek
Copy link
Contributor

kamilogorek commented Nov 27, 2022

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

@github-actions
Copy link
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 🥀

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Package: node Issues related to the Sentry Node SDK Type: Bug
Projects
None yet
Development

No branches or pull requests

3 participants