GitAuto: [Sentry] Express is not instrumented. This is likely because you required/imported express before calling Sentry.init()
#24
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #17
Why the bug occurs
When building Node.js applications with bundling tools like esbuild (used by frameworks such as NX), Sentry's auto-instrumentation for frameworks like Express does not work as expected. This is because Sentry relies on intercepting module imports using
require
hooks to instrument frameworks before they are imported and used by the application. Bundling tools often inline modules and change the load order, preventing Sentry from intercepting the imports as intended. As a result, even ifSentry.init()
is called before importing Express in the source code, the bundled output may import Express before Sentry has a chance to instrument it.This leads to the warning message:
And it prevents Sentry from fully instrumenting Express, missing valuable performance data such as route parameters and middleware spans.
How to reproduce
Set up an NX Express application:
@sentry/node
version 8.0.0 or later.Initialize Sentry in your
main.ts
:Sentry.init()
before importing Express, following the Sentry documentation.main.ts
:Build the application using NX with esbuild:
nx build your-app
to bundle the application using esbuild.Run the bundled application:
node ./dist/main.js
.Observe the warning message:
How to fix
To fix this issue, we need to ensure that Sentry can instrument Express even when the application is bundled with tools like esbuild. There are two main approaches:
Option 1: Externalize Express in the Bundler Configuration
Modify the esbuild configuration to treat
express
(and any other modules Sentry needs to instrument) as external dependencies. This ensures thatexpress
is not bundled and remains a separate module that can be required at runtime, allowing Sentry to instrument it properly.Steps:
Update
project.json
or build configuration:Add the
external
option to theesbuildOptions
:Rebuild the application:
Run
nx build your-app
to rebuild the application with the updated configuration.Re-run the application:
Start the application again and verify that the warning message is no longer displayed, and Sentry properly instruments Express.
Explanation:
By externalizing
express
, esbuild will not bundle it into the application's output. Instead, it will be required at runtime, preserving therequire
hooks that Sentry relies on for instrumentation.Option 2: Manually Instrument Express
If externalizing modules is not feasible or desirable, we can enhance the Sentry SDK to allow manual instrumentation of Express. This involves adding a method to the SDK that instruments Express explicitly, regardless of import order or bundling.
Proposed Changes to the Sentry SDK:
Add a manual instrumentation method:
Instruct users to call
Sentry.instrumentExpress()
:Update the documentation to guide users to call this method after
Sentry.init()
.Updated
main.ts
:Explanation:
This method attempts to require
express
and instrument it directly. By callingSentry.instrumentExpress()
afterSentry.init()
, we ensure that Express is instrumented regardless of bundling or import order.Benefits:
Considerations:
express
is not installed or available.By implementing one of these fixes, we can resolve the issue where Sentry fails to instrument Express in bundled applications, providing better performance monitoring and user experience.
Test these changes locally