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

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

Open amiranvarov opened this issue 1 year ago • 47 comments

Is there an existing issue for this?

  • [X] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
  • [X] I have reviewed the documentation https://docs.sentry.io/
  • [X] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

8.0.0

Framework Version

express@^4.18.1, @nx/express": "18.2.2

Link to Sentry event

No response

SDK Setup

// main.ts
import * as Sentry from "@sentry/node";
import { nodeProfilingIntegration } from "@sentry/profiling-node";
Sentry.init({
	serverName: "auth-service",
	dsn: process.env.SENTRY_DSN,
	environment: process.env.NODE_ENV,
	enabled: true,
	debug: process.env.NODE_ENV !== "production",
	integrations: [Sentry.httpIntegration(), Sentry.expressIntegration(), nodeProfilingIntegration()],
	// Set tracesSampleRate to 1.0 to capture 100%
	// of transactions for performance monitoring.
	// We recommend adjusting this value in production
	tracesSampleRate: 1.0,
	beforeSend(event, hint) {
		const error = hint.originalException as Error;
		if (error?.message?.match(/database unavailable/i)) {
			event.fingerprint = ["database-unavailable"];
		}

		// Modify or drop the event here
		if (event.user) {
			// Don't send user's email address
			event.user.email = undefined;
		}

		return event;
	},

	// Called for transaction events
	beforeSendTransaction(event) {
		// Modify or drop the event here
		if (event.transaction === "/unimportant/route") {
			// Don't send the event to Sentry
			return null;
		}
		return event;
	},
	beforeBreadcrumb(breadcrumb, hint) {
		// Check if the breadcrumb has sensitive data like user email
		if (breadcrumb.data?.["user"]?.email) {
			// Remove the user email from the breadcrumb
			breadcrumb.data["user"].email = undefined;
		}
		return breadcrumb;
	},
	includeLocalVariables: true,
	attachStacktrace: true,
});

/// rest of the app

Steps to Reproduce

  1. install NX
  2. Install NX's express
  3. run nx run --target=serve

I did right as you suggested it in your docs. Imprted and initialised Sentry first thing first. But still, it's not working image

Expected Result

Do not have [Sentry] Express is not instrumented. This is likely because you required/imported express before calling Sentry.init() message, and

Actual Result

Having error message: [Sentry] Express is not instrumented. This is likely because you required/imported express before calling Sentry.init() message, and image

amiranvarov avatar May 15 '24 17:05 amiranvarov

Strange... I did some code refactoring, and didn't touch this part I shared at all.. somehow I'm not having this error anymore. I will close this issue for a bit, if I encounter it again, I'll reopen it

amiranvarov avatar May 15 '24 18:05 amiranvarov

Isse came back again, I have no idea why. I didn't change anything. tried to update to latest Sentry, didn't help. Tried to nuke node_modules. lock file and everything, still didn't work. This is some weird mystery...

amiranvarov avatar May 18 '24 19:05 amiranvarov

Hi there,

Things changed in v8 a bit, most integrations are now automatically added and you no longer have to take care of those (with the exception of nodeProfilingIntegration in your example).

We recommend going through the migration guide here. You can use npx @sentry/migr8@latest to take care of it for you, or follow the steps to manually set it up here.

In your particular example, could you try removing Sentry.httpIntegration() and Sentry.expressIntegration() and report back?

andreiborza avatar May 18 '24 19:05 andreiborza

hey, thanks for getting back @andreiborza! I did follow the migration guide, and used npx @sentry/migr8@latest right from the start. But no, it didn't help.

As you suggested, i removed Sentry.httpIntegration and Sentry.expressIntegration() it didn't help. Then i removed all of the integrations, didn't help either. still getting this message.

amiranvarov avatar May 19 '24 05:05 amiranvarov

If it helps, you may check my comment on another similar issue that was opened 2 days ago: https://github.com/getsentry/sentry-javascript/issues/12105

amiranvarov avatar May 19 '24 05:05 amiranvarov

If that help,s we can have a short screensharing call so u can get the context you need

amiranvarov avatar May 19 '24 05:05 amiranvarov

Have you updated to 8.2.1 yet? Same result?

Could you please provide

  • node version you are running
  • the updated code snippets after your migration attempts (instrument.ts and main.ts)
  • the way you run your application (i.e. the script behind auth-service:build:development)

I see a .ts ending in the comment, what are you using to bundle your app?

andreiborza avatar May 21 '24 06:05 andreiborza

yes, updated just yesterday. And yes, same result.

  • node v20.13.1
  • will provide later today
  • I'm not sure what exactly do you mean? the whole code of my microservice? if so, can't share much of it, it's private :( If you mean the way u run the script itself: just nx run auth-service:build:development. In the screen you may say that I didn't type it manually, that's because I'm using NX's VScode extension that will run this script for me with one click, but it should not matter for your debugging as it's still running nx run auth-service:build:development

Regarding bundler: I'm using @nx/esbuild, as it comes as built-in bundler in NX.

Here is part of my NX project (this microservice) config: apps/auth-service/project.json

{
	"name": "auth-service",
	"$schema": "../../node_modules/nx/schemas/project-schema.json",
	"sourceRoot": "apps/auth-service/src",
	"projectType": "application",
	"targets": {
		"build": {
			"executor": "@nx/esbuild:esbuild",
			"outputs": ["{projectRoot}/build"],
			"defaultConfiguration": "production",
			"options": {
				"platform": "node",
				"outputPath": "{projectRoot}/build",
				"format": ["cjs"],
				"bundle": true,
				"main": "apps/auth-service/src/main.ts",
				"tsConfig": "apps/auth-service/tsconfig.app.json",
				"assets": ["apps/auth-service/src/assets"],
				"generatePackageJson": true,
				"esbuildOptions": {
					"sourcemap": true,
					"outExtension": {
						".js": ".js"
					}
				}
			},
			"configurations": {
				"development": {},
				"production": {
					"generateLockfile": true,
					"esbuildOptions": {
						"sourcemap": false,
						"outExtension": {
							".js": ".js"
						}
					}
				}
			}
		},

amiranvarov avatar May 21 '24 08:05 amiranvarov

Isse came back again, I have no idea why. I didn't change anything. tried to update to latest Sentry, didn't help. Tried to nuke node_modules. lock file and everything, still didn't work. This is some weird mystery...

I figured why I stopped having this issue at that time, I removed sentry.setupExpressErrorHandler(app), that's why I didn't get that message. But the problem was still there, although I didn't see it in the console. So this issue is persistent

amiranvarov avatar May 21 '24 08:05 amiranvarov

So you should def. not remove the error handler :D Do you know if your app is being run as a CommonJS or ESM app? Basically, if you look into your build/dist folder, are the files in there using import or require?

mydea avatar May 22 '24 12:05 mydea

it's CJS, for sure :)

amiranvarov avatar May 22 '24 13:05 amiranvarov

could you please post snippets of instrument.ts and the top of your app where you import sentry and express and setup the handler?

andreiborza avatar May 22 '24 14:05 andreiborza

Hi, I'm also seeing:

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

in my application logs despite following the sentry onboarding guide for express apps.

Errors are still sent to sentry and everything seems to work fine though.

Here's the code that I'm running: https://github.com/Nabil372/sentry-playground

Nabil372 avatar May 22 '24 14:05 Nabil372

Hi, I'm also seeing:

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

in my application logs despite following the sentry onboarding guide for express apps.

Errors are still sent to sentry and everything seems to work fine though.

Here's the code that I'm running: https://github.com/Nabil372/sentry-playground

Are you not using performance? This is a bug in the current version, that it will show this warning if performance is disabled - it's safe to ignore it! In 8.3.0 (which should be out soon) this warning will be fixed/removed.

mydea avatar May 22 '24 14:05 mydea

Hey @Nabil372, thank you for providing a sample project.

I've had a quick look over it, you do have performance (tracing) enabled so that's not the issue. I see that you're using esbuild with the output format ESM.

For ESM, you need to --import the instrument file, please see https://docs.sentry.io/platforms/javascript/guides/express/install/esm/

I tried that out for you, but your build output doesn't seem to be pure ESM either—I'm seeing this snippet in the output which probably throws off internals of sentry/opentelemetry.

andreiborza avatar May 22 '24 15:05 andreiborza

Hey @Nabil372, thank you for providing a sample project.

I've had a quick look over it, you do have performance (tracing) enabled so that's not the issue. I see that you're using esbuild with the output format ESM.

For ESM, you need to --import the instrument file, please see https://docs.sentry.io/platforms/javascript/guides/express/install/esm/

I tried that out for you, but your build output doesn't seem to be pure ESM either—I'm seeing this snippet in the output which probably throws off internals of sentry/opentelemetry.

@andreiborza Thanks for taking a look! I've implemented what you've suggested and I'm no longer seeing the warning message.

Nabil372 avatar May 22 '24 16:05 Nabil372

@amiranvarov could you please update to the latest SDK? Should be 8.3.0 at the time of writing and report back? We updated some of the warnings around this.

andreiborza avatar May 23 '24 14:05 andreiborza

Ran into this same error with Koa (instead of express) image

"@sentry/node": "^8.4.0",
// main.ts
import './initSentry'
import * as Sentry from '@sentry/node'
import Koa from 'koa'

import { env } from './env'

const { HOST, PORT } = env

const app = new Koa()
Sentry.setupKoaErrorHandler(app)
// initSentry.ts

import * as Sentry from '@sentry/node'
import { env } from './env'

Sentry.init({
  dsn: env.SENTRY_DSN,
  sampleRate: env.SENTRY_SAMPLE_RATE,
  tracesSampleRate: env.SENTRY_TRACE_SAMPLE_RATE,
  environment: env.ENVIRONMENT,
  enableTracing: true,
})

cyrus-za avatar May 23 '24 20:05 cyrus-za

@cyrus-za just to make sure, SENTRY_TRACE_SAMPLE_RATE is set? Could you try hard coding it to 1.0?

andreiborza avatar May 24 '24 07:05 andreiborza

Side note, enableTracing should be set instead of tracesSampleRate, this basically means tracesSampleRate: 1 - so you can/should probably just remove this.

If you enable debug: true, could you share the logs you see?

mydea avatar May 24 '24 07:05 mydea

I'm also getting this error with Sentry v8.10.0 and it's preventing me from migrating to v8:

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

This seems to be related to esbuild and how it bundles code. Our code is bundled using esbuild (CJS), and in the bundled code there doesn't seem to be any guarantee that Sentry is initialized before Koa is imported, no matter how early I initialize Sentry. Is there any workaround? I tried late initialization (https://docs.sentry.io/platforms/javascript/guides/koa/install/late-initializtion/) but that didn't help.

ollipa avatar Jun 20 '24 13:06 ollipa

Side note, enableTracing should be set instead of tracesSampleRate, this basically means tracesSampleRate: 1 - so you can/should probably just remove this.

If you enable debug: true, could you share the logs you see?

Hello @mydea, enable tracing is set to true yet I'm still getting:

[Sentry] express is not instrumented. Please make sure to initialize Sentry in a separate file that you `--import` when running node, see: https://docs.sentry.io/platforms/javascript/guides/express/install/esm/.

This is my set up ./src/config/sentry.js

import * as Sentry from "@sentry/node";
import { nodeProfilingIntegration } from "@sentry/profiling-node";
import env from "./env.js";

Sentry.init({
    dsn: env.sentry_dsn,
    environment: env.node_env,
    integrations: [nodeProfilingIntegration()],
    enableTracing: true,
    profilesSampleRate: 1.0
});

./app.js

import "./config/sentry.js";
import express from "express";
import * as Sentry from "@sentry/node";
import v1Router from "./v1/index.js";
import ErrorHandler from "./middleware/error-handler.js";


const app = express();

app.use("/v1", v1Router);

Sentry.setupExpressErrorHandler(app);

app.use(ErrorHandler);

if i remove Sentry.setupExpressErrorHandler(app); i don't get that message.

adding debug:true i get this

entry Logger [log]: Initializing Sentry: process: 70659, thread: main.
Sentry Logger [log]: Integration installed: InboundFilters
Sentry Logger [log]: Integration installed: FunctionToString
Sentry Logger [log]: Integration installed: LinkedErrors
Sentry Logger [log]: Integration installed: RequestData
Sentry Logger [log]: Integration installed: Console
Sentry Logger [log]: Integration installed: Http
Sentry Logger [log]: Integration installed: NodeFetch
Sentry Logger [log]: Integration installed: OnUncaughtException
Sentry Logger [log]: Integration installed: OnUnhandledRejection
Sentry Logger [log]: Integration installed: ContextLines
Sentry Logger [log]: Integration installed: LocalVariablesAsync
Sentry Logger [log]: Integration installed: Context
Sentry Logger [log]: Integration installed: Express
Sentry Logger [log]: Integration installed: Fastify
Sentry Logger [log]: Integration installed: Graphql
Sentry Logger [log]: Integration installed: Mongo
Sentry Logger [log]: Integration installed: Mongoose
Sentry Logger [log]: Integration installed: Mysql
Sentry Logger [log]: Integration installed: Mysql2
Sentry Logger [log]: Integration installed: Redis
Sentry Logger [log]: Integration installed: Postgres
Sentry Logger [log]: Integration installed: Nest
Sentry Logger [log]: Integration installed: Hapi
Sentry Logger [log]: Integration installed: Koa
Sentry Logger [log]: Integration installed: Connect
Sentry Logger [log]: [Profiling] Profiling integration setup.
Sentry Logger [log]: [Profiling] Span profiler mode enabled.
Sentry Logger [log]: Integration installed: ProfilingIntegration
Sentry Logger [log]: Running in ESM mode.
Sentry Logger [debug]: @opentelemetry/api: Registered a global for diag v1.8.0.
Sentry Logger [debug]: @opentelemetry/api: Registered a global for trace v1.8.0.
Sentry Logger [debug]: @opentelemetry/api: Registered a global for context v1.8.0.
Sentry Logger [debug]: @opentelemetry/api: Registered a global for propagation v1.8.0.
Sentry Logger [debug]: @opentelemetry/instrumentation-http Applying instrumentation patch for nodejs core module on require hook { module: 'http' }
Sentry Logger [debug]: @opentelemetry/instrumentation-mongodb Applying instrumentation patch for nodejs module file on require hook {
  module: 'mongodb',
  version: '4.17.2',
  fileName: 'mongodb/lib/sessions.js',
  baseDir: '/~/node_modules/mongodb'
}
Sentry Logger [debug]: @opentelemetry/instrumentation-mongodb Applying instrumentation patch for nodejs module file on require hook {
  module: 'mongodb',
  version: '4.17.2',
  fileName: 'mongodb/lib/cmap/connection.js',
  baseDir: '/~/node_modules/mongodb'
}
Sentry Logger [debug]: @opentelemetry/instrumentation-mongodb Applying instrumentation patch for nodejs module file on require hook {
  module: 'mongodb',
  version: '4.17.2',
  fileName: 'mongodb/lib/cmap/connect.js',
  baseDir: '/~/node_modules/mongodb'
}
Sentry Logger [debug]: @opentelemetry/instrumentation-mongodb Applying instrumentation patch for nodejs module file on require hook {
  module: 'mongodb',
  version: '4.17.2',
  fileName: 'mongodb/lib/cmap/connection_pool.js',
  baseDir: '/~/node_modules/mongodb'
}
Sentry Logger [debug]: @opentelemetry/instrumentation-http Applying instrumentation patch for nodejs core module on require hook { module: 'https' }
[Sentry] express is not instrumented. Please make sure to initialize Sentry in a separate file that you `--import` when running node, see: https://docs.sentry.io/platforms/javascript/guides/express/install/esm/.

Node version - 20.14.0 sentry version 8.7

ebosetalee avatar Jun 20 '24 14:06 ebosetalee

I have same problem!! image

RubeCarton6231 avatar Jun 20 '24 19:06 RubeCarton6231

@ebosetalee in your case, if you read the warning message carefully it will tell you the problem, I believe - you have to use --import when running your app in ESM mode: https://docs.sentry.io/platforms/javascript/guides/express/install/esm/

@RubeCarton6231 & @ollipa:

If you are already following https://docs.sentry.io/platforms/javascript/guides/express/install/commonjs/, then the problem is probably that you are bundling your code. If this is the case, you need to define the packages you want to instrument (e.g. or koa fastify) as externals in your build process. For now, there is sadly no way around this :( We have not documented this properly yet, but it's on our radar: https://github.com/getsentry/sentry-docs/issues/10416 basically, the problem is that the underlying instrumentation works by patching require, and when stuff is bundled in there is no require, so it cannot instrument.

FWIW basic instrumentation should still work, so you should still be seeing http.server spans and basic other things. But you'll miss nice route parametrization and middleware spans, which is obviously not ideal.

mydea avatar Jun 21 '24 07:06 mydea

@mydea adding the --import causes a build error with Mongoose instrumentation

> [email protected] start
> node --import ./src/config/sentry.js src/app.js

TypeError: Cannot read properties of undefined (reading 'prototype')
    at MongooseInstrumentation.patch (/~/node_modules/@opentelemetry/instrumentation-mongoose/build/src/mongoose.js:66:44)
    at MongooseInstrumentation._onRequire (/~/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js:168:39)
    at hookFn (/~/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js:226:29)
    at callHookFn (/~/node_modules/import-in-the-middle/index.js:28:22)
    at Hook._iitmHook (/~i/node_modules/import-in-the-middle/index.js:76:11)
    at /~/node_modules/import-in-the-middle/lib/register.js:28:31
    at Array.forEach (<anonymous>)
    at register (/~/node_modules/import-in-the-middle/lib/register.js:28:15)
    at file:///~node_modules/mongoose/index.js?iitm=true:381:1
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)

And since I can't upgrade Mongoose from v6xx to 7xx due to breaking changes, I switched sentry to v7.57.0

ebosetalee avatar Jun 21 '24 11:06 ebosetalee

Damn, that sucks. We have a tracking issue for this here: https://github.com/getsentry/sentry-javascript/issues/11499, we hope to find some time to resolve this soon.

For now, you should be able to remove this integration, avoiding the build error:

// instrument.js

Sentry.init({
  integrations: integrations => {
    return integrations.filter(integration => integration.name !== 'Mongoose')
  }
});

See https://docs.sentry.io/platforms/javascript/guides/node/configuration/integrations/#removing-a-default-integration

mydea avatar Jun 21 '24 12:06 mydea

Yep, thanks. I'd try this during my spare time and give feedback soon. But for now, the version works. I'm also keeping an eye out for both issues.

ebosetalee avatar Jun 21 '24 13:06 ebosetalee

If you are already following https://docs.sentry.io/platforms/javascript/guides/express/install/commonjs/, then the problem is probably that you are bundling your code. If this is the case, you need to define the packages you want to instrument (e.g. or koa fastify) as externals in your build process. For now, there is sadly no way around this :( We have not documented this properly yet, but it's on our radar: getsentry/sentry-docs#10416 basically, the problem is that the underlying instrumentation works by patching require, and when stuff is bundled in there is no require, so it cannot instrument.

Thank you for your response @mydea. I got this working by configuring koa as an external package and changing how I initialized Sentry. I was initializing Sentry as follows in index.ts:

import { initSentry } from "./sentry";
initSentry();

When I look at the bundled code in the generated index.js file, initSentry call and the content of index.ts were at the bottom of the bundle. I moved the initSentry() call to to sentry.ts file and updated index.ts to:

import "./sentry";

ollipa avatar Jun 25 '24 07:06 ollipa

Having the same issue. Not using TS, plain mjs. node --import ./src/sentry.mjs --watch ./src/main.mjs Results in same warning: [Sentry] express is not instrumented. Please make sure to initialize Sentry in a separate file that you `--import` when running node, see: https://docs.sentry.io/platforms/javascript/guides/express/install/esm/.

Node 20, @sentry/node:8.15.0,@sentry/profiling-node:8.15.0.

mr-moon avatar Jul 07 '24 20:07 mr-moon

@mr-moon can you share the contents of your sentry.mjs file or even better share a reproduction example we can use to debug locally? Thanks!

lforst avatar Jul 09 '24 12:07 lforst