Skip to content

Sentry Express instrumenting whole app instead of only router #5433

Closed as not planned
@woss

Description

@woss

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

7.6.0

Framework Version

7.6.0

Link to Sentry event

No response

Steps to Reproduce

I followed the Express integration and i have this code:

/* eslint-disable @rushstack/typedef-var */

import * as Sentry from '@sentry/node';
import * as Tracing from '@sentry/tracing';
import express, { Request, Response } from 'express'; //  "express": "4.18.1",
import { type Express } from 'express';

import { port } from './config';

/**
 * Express app
 * @internal
 */
export const app: Express = express();

const router = express.Router({
  caseSensitive: true
});
/// init the Sentry
Sentry.init({
  dsn: 'obv-dsn',
  enabled: true,
  environment: 'envvvvv',
  integrations: [
    new Sentry.Integrations.Http({ tracing: true }),
    // enable Express.js middleware tracing
    new Tracing.Integrations.Express({ app, methods: ['get'], router })
  ],
  tracesSampleRate: 1.0
});
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());

router.get('/my-router-route/*', async (req: Request, res: Response) => {
  res.send('ok');
});

app.use(router);

/**
 * Home route
 */
app.get('/', (req: Request, res: Response) => {
  res.send('home');
});

/**
 * Health-check route
 */
app.get('/healthcheck', (req: Request, res: Response) => {
  res.send('OK');
});

app.post('/po', (req: Request, res: Response) => {
  res.send('OK');
});


// eslint-disable-next-line @typescript-eslint/naming-convention
app.get('*', function (req: Request, res: Response) {
  res.status(404).end();
});

// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler());

app.listen(port);

Expected Result

only router is instrumented, and get requests in the router only. Not the whole app

Actual Result

You can see here that all routes are instrumented. I have tried to set the

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

from the app but it doesn't work.

image

Metadata

Metadata

Assignees

No one assigned

    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