sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

feat(node): Add `ignoreLayers` and `ignoreLayersType` options to express instrumentation

Open AbhiPrasad opened this issue 6 months ago • 1 comments

This PR adds two new options to the Express integration: ignoreLayers and ignoreLayersType, bringing it to feature parity with the underlying OpenTelemetry instrumentation.

resolves https://github.com/getsentry/sentry-javascript/issues/16555

Changes

  • Added ignoreLayers option to ignore specific Express layers based on their path
  • Added ignoreLayersType option to ignore specific Express layers based on their type
  • Added comprehensive tests for both options
  • Updated JSDoc documentation with usage examples

Usage

Ignore layers by path

The ignoreLayers option accepts an array of elements that can be:

  • string for full match of the path
  • RegExp for partial match of the path
  • function in the form of (path) => boolean for custom logic
const Sentry = require('@sentry/node');

Sentry.init({
  integrations: [
    Sentry.expressIntegration({
      ignoreLayers: [
        '/health',              // Ignore exact path
        /^\/internal/,          // Ignore paths starting with /internal
        (path) => path.includes('admin') // Custom logic
      ]
    })
  ],
});

Ignore layers by type

The ignoreLayersType option accepts an array of the following strings:

  • router - for express.Router()
  • middleware - for middleware functions
  • request_handler - for request handlers (anything that's not a router or middleware)
const Sentry = require('@sentry/node');

Sentry.init({
  integrations: [
    Sentry.expressIntegration({
      ignoreLayersType: ['middleware'] // Ignore all middleware spans
    })
  ],
});

Combining both options

Both options can be used together:

const Sentry = require('@sentry/node');

Sentry.init({
  integrations: [
    Sentry.expressIntegration({
      ignoreLayers: ['/health', '/metrics', /^\/internal/],
      ignoreLayersType: ['middleware']
    })
  ],
});

AbhiPrasad avatar Jun 11 '25 17:06 AbhiPrasad

size-limit report 📦

Path Size % Change Change
@sentry/browser 23.88 kB - -
@sentry/browser - with treeshaking flags 22.35 kB - -
@sentry/browser (incl. Tracing) 39.75 kB - -
@sentry/browser (incl. Tracing, Replay) 77.88 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 67.61 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 82.58 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 94.68 kB - -
@sentry/browser (incl. Feedback) 40.58 kB - -
@sentry/browser (incl. sendFeedback) 28.56 kB - -
@sentry/browser (incl. FeedbackAsync) 33.46 kB - -
@sentry/react 25.61 kB - -
@sentry/react (incl. Tracing) 41.72 kB - -
@sentry/vue 28.31 kB - -
@sentry/vue (incl. Tracing) 41.53 kB - -
@sentry/svelte 23.9 kB - -
CDN Bundle 25.18 kB - -
CDN Bundle (incl. Tracing) 39.44 kB - -
CDN Bundle (incl. Tracing, Replay) 75.44 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 80.91 kB - -
CDN Bundle - uncompressed 73.54 kB - -
CDN Bundle (incl. Tracing) - uncompressed 116.99 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 231.18 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 243.99 kB - -
@sentry/nextjs (client) 43.32 kB - -
@sentry/sveltekit (client) 40.2 kB - -
@sentry/node 167.62 kB +0.02% +21 B 🔺
@sentry/node - without tracing 100.3 kB - -
@sentry/aws-serverless 128.41 kB - -

View base workflow run

github-actions[bot] avatar Jun 11 '25 18:06 github-actions[bot]

Until this is merged, is there any workaround to disable the Express middlewares from the default list while keeping all other default middlewares?

eliliam avatar Aug 12 '25 05:08 eliliam

@eliliam you can use our new ignoreSpans config introduced with 10.2.0: https://github.com/getsentry/sentry-javascript/releases/tag/10.2.0

AbhiPrasad avatar Aug 12 '25 12:08 AbhiPrasad