Skip to content

GitAuto: [Sentry] Express is not instrumented. This is likely because you required/imported express before calling Sentry.init() #21

New issue

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

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

Already on GitHub? Sign in to your account

Conversation

gitauto-for-dev[bot]
Copy link

Resolves #17

Why the bug occurs

The bug occurs because Express is imported before calling Sentry.init(). This import order prevents Sentry from properly instrumenting Express, resulting in the warning:

[Sentry] Express is not instrumented. This is likely because you required/imported express before calling `Sentry.init()`.

Additionally, when using bundlers like esbuild with frameworks such as NX, the bundling process can disrupt the require hooks that Sentry relies on for instrumentation. This leads to inconsistent behavior where the warning may appear intermittently despite following the correct initialization procedures.

How to reproduce

  1. Setup Project with NX and Esbuild:

    • Install NX and set up an Express application using NX's Express plugin.
    • Configure the project to use esbuild as the bundler.
  2. Initialize Sentry:

    • Import and initialize Sentry in the main entry file (main.ts or equivalent) before importing Express.
    • Example:
      import * as Sentry from "@sentry/node";
      import { nodeProfilingIntegration } from "@sentry/profiling-node";
      
      Sentry.init({
        dsn: process.env.SENTRY_DSN,
        integrations: [
          Sentry.httpIntegration(),
          Sentry.expressIntegration(),
          nodeProfilingIntegration(),
        ],
        tracesSampleRate: 1.0,
        // other configurations...
      });
      
      import express from "express";
      const app = express();
  3. Run the Application:

    • Use NX to build and serve the Express application.
    • Observe the console for the warning:
      [Sentry] Express is not instrumented. This is likely because you required/imported express before calling `Sentry.init()`.
      

How to fix

To resolve the instrumentation issue, follow these steps:

  1. Ensure Correct Import Order:

    • Import and initialize Sentry before importing Express or any other frameworks.
    • Consider moving the Sentry initialization to a separate file and using the --import flag when running the application to guarantee that Sentry is initialized first.

    Example:

    // sentry.ts
    import * as Sentry from "@sentry/node";
    import { nodeProfilingIntegration } from "@sentry/profiling-node";
    
    Sentry.init({
      dsn: process.env.SENTRY_DSN,
      integrations: [
        nodeProfilingIntegration(),
        // Remove expressIntegration if using automatic integrations
      ],
      tracesSampleRate: 1.0,
      enableTracing: true,
      profilesSampleRate: 1.0,
      debug: process.env.NODE_ENV !== "production",
      // Other configurations...
    });
    // app.ts
    import "./sentry"; // Ensure this is imported first
    import express from "express";
    import * as Sentry from "@sentry/node";
    
    const app = express();
    
    // Sentry error handling
    Sentry.setupExpressErrorHandler(app);
    
    // Rest of the app setup...
  2. Adjust Bundler Configuration:

    • When using esbuild or similar bundlers, define Sentry and related integrations (e.g., Express) as external packages to prevent them from being bundled. This ensures that Sentry's require hooks function correctly.
    • Example esbuild configuration snippet:
      {
        "external": ["express", "@sentry/node", "@sentry/profiling-node"]
      }
  3. Update Sentry SDK:

    • Ensure you're using the latest version of the Sentry SDK, as newer versions may include fixes and improvements related to instrumentation and bundling.
    • Example:
      yarn add @sentry/node@latest @sentry/profiling-node@latest
      
  4. Modify Initialization for ESM:

    • If your project uses ESM modules, adjust the initialization as per Sentry's ESM setup guide.
    • Use the --import flag to initialize Sentry before other imports.
  5. Handle Integration Removal if Necessary:

    • If certain integrations like Mongoose are causing build issues, temporarily remove them until a fix is available.
    • Example:
      Sentry.init({
        integrations: (integrations) => 
          integrations.filter(integration => integration.name !== 'Mongoose'),
        // Other configurations...
      });

About backward compatibility

These changes maintain backward compatibility as they primarily involve reordering imports and adjusting bundler configurations. No existing functionality is removed, and Sentry's instrumentation remains intact. Users following the updated initialization steps will benefit from proper instrumentation without any breaking changes to their application logic.

Test these changes locally

git checkout -b gitauto-wes/issue-#17-7db8440d-effd-48fa-b3c3-5ad8904518db
git pull origin gitauto-wes/issue-#17-7db8440d-effd-48fa-b3c3-5ad8904518db

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant