Skip to content
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

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

Open
3 tasks done
amiranvarov opened this issue May 15, 2024 · 20 comments

Comments

@amiranvarov
Copy link

amiranvarov commented May 15, 2024

Is there an existing issue for this?

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
Copy link
Author

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
Copy link
Author

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...

@andreiborza
Copy link
Member

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?

@amiranvarov
Copy link
Author

amiranvarov commented May 19, 2024

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
Copy link
Author

If it helps, you may check my comment on another similar issue that was opened 2 days ago:
#12105

@amiranvarov
Copy link
Author

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

@andreiborza
Copy link
Member

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?

@amiranvarov
Copy link
Author

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
Copy link
Author

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

@mydea
Copy link
Member

mydea commented May 22, 2024

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?

@amiranvarov
Copy link
Author

it's CJS, for sure :)

@andreiborza
Copy link
Member

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

@Nabil372
Copy link

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

@mydea
Copy link
Member

mydea commented May 22, 2024

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.

@andreiborza
Copy link
Member

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.

@Nabil372
Copy link

Nabil372 commented May 22, 2024

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.

@andreiborza
Copy link
Member

@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.

@cyrus-za
Copy link

cyrus-za commented May 23, 2024

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,
})

@andreiborza
Copy link
Member

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

@mydea
Copy link
Member

mydea commented May 24, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Waiting for: Community
Development

No branches or pull requests

6 participants