Giter Site home page Giter Site logo

[Sentry] Express is not instrumented. This is likely because you required/imported express before calling `Sentry.init()` about sentry-javascript HOT 27 OPEN

amiranvarov avatar amiranvarov commented on July 23, 2024
[Sentry] Express is not instrumented. This is likely because you required/imported express before calling `Sentry.init()`

from sentry-javascript.

Comments (27)

mydea avatar mydea commented on July 23, 2024 1

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.

from sentry-javascript.

Nabil372 avatar Nabil372 commented on July 23, 2024 1

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.

from sentry-javascript.

andreiborza avatar andreiborza commented on July 23, 2024 1

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

from sentry-javascript.

mydea avatar mydea commented on July 23, 2024 1

Damn, that sucks. We have a tracking issue for this here: #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

from sentry-javascript.

amiranvarov avatar amiranvarov commented on July 23, 2024

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

from sentry-javascript.

amiranvarov avatar amiranvarov commented on July 23, 2024

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

from sentry-javascript.

andreiborza avatar andreiborza commented on July 23, 2024

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?

from sentry-javascript.

amiranvarov avatar amiranvarov commented on July 23, 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.

from sentry-javascript.

amiranvarov avatar amiranvarov commented on July 23, 2024

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

from sentry-javascript.

amiranvarov avatar amiranvarov commented on July 23, 2024

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

from sentry-javascript.

andreiborza avatar andreiborza commented on July 23, 2024

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?

from sentry-javascript.

amiranvarov avatar amiranvarov commented on July 23, 2024

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

from sentry-javascript.

amiranvarov avatar amiranvarov commented on July 23, 2024

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

from sentry-javascript.

mydea avatar mydea commented on July 23, 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?

from sentry-javascript.

amiranvarov avatar amiranvarov commented on July 23, 2024

it's CJS, for sure :)

from sentry-javascript.

andreiborza avatar andreiborza commented on July 23, 2024

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

from sentry-javascript.

Nabil372 avatar Nabil372 commented on July 23, 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

from sentry-javascript.

andreiborza avatar andreiborza commented on July 23, 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.

from sentry-javascript.

andreiborza avatar andreiborza commented on July 23, 2024

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

from sentry-javascript.

cyrus-za avatar cyrus-za commented on July 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,
})

from sentry-javascript.

mydea avatar mydea commented on July 23, 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?

from sentry-javascript.

ollipa avatar ollipa commented on July 23, 2024

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.

from sentry-javascript.

ebosetalee avatar ebosetalee commented on July 23, 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?

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

from sentry-javascript.

RubeCarton6231 avatar RubeCarton6231 commented on July 23, 2024

I have same problem!!
image

from sentry-javascript.

mydea avatar mydea commented on July 23, 2024

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

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.

from sentry-javascript.

ebosetalee avatar ebosetalee commented on July 23, 2024

@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

from sentry-javascript.

ebosetalee avatar ebosetalee commented on July 23, 2024

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.

from sentry-javascript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.