feat(node): Add `ignoreLayers` and `ignoreLayersType` options to express instrumentation
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
ignoreLayersoption to ignore specific Express layers based on their path - Added
ignoreLayersTypeoption 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:
stringfor full match of the pathRegExpfor partial match of the pathfunctionin the form of(path) => booleanfor 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- forexpress.Router()middleware- for middleware functionsrequest_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']
})
],
});
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 | - | - |
Until this is merged, is there any workaround to disable the Express middlewares from the default list while keeping all other default middlewares?
@eliliam you can use our new ignoreSpans config introduced with 10.2.0: https://github.com/getsentry/sentry-javascript/releases/tag/10.2.0