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() #17

Open
3 tasks done
hiroshinishio opened this issue Oct 6, 2024 · 56 comments · May be fixed by #24

Comments

@hiroshinishio
Copy link
Owner

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

@hiroshinishio
Copy link
Owner 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

@hiroshinishio
Copy link
Owner 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...

Copy link

gitauto-ai bot commented Oct 6, 2024

Click the checkbox below to generate a PR!

  • Generate PR

@hiroshinishio, You have 2 requests left in this cycle which refreshes on 2024-11-04 02:56:25+00:00.
If you have any questions or concerns, please contact us at [email protected].

Copy link

gitauto-for-dev bot commented Oct 6, 2024

Click the checkbox below to generate a PR!

  • Generate PR - gitauto-wes

@hiroshinishio, You have 3 requests left in this cycle which refreshes on 2024-11-05 22:45:17+00:00.
If you have any questions or concerns, please contact us at [email protected].

@hiroshinishio
Copy link
Owner Author

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 /migr8 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?

@hiroshinishio
Copy link
Owner Author

hey, thanks for getting back ! I did follow the migration guide, and used npx /migr8 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.

@hiroshinishio
Copy link
Owner Author

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

@hiroshinishio
Copy link
Owner Author

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

@hiroshinishio
Copy link
Owner Author

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?

@hiroshinishio
Copy link
Owner 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 /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": "/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"
						}
					}
				}
			}
		},

@hiroshinishio
Copy link
Owner 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

@hiroshinishio
Copy link
Owner Author

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?

@hiroshinishio
Copy link
Owner Author

it's CJS, for sure :)

@hiroshinishio
Copy link
Owner Author

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

@hiroshinishio
Copy link
Owner Author

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

@hiroshinishio
Copy link
Owner Author

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.

@hiroshinishio
Copy link
Owner Author

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

@hiroshinishio
Copy link
Owner Author

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

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

@hiroshinishio
Copy link
Owner Author

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.

@hiroshinishio
Copy link
Owner Author

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

"/node": "^8.4.0",
// main.ts
import './initSentry'
import * as Sentry from '/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 '/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,
})

@hiroshinishio
Copy link
Owner Author

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

@hiroshinishio
Copy link
Owner Author

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?

@hiroshinishio
Copy link
Owner Author

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.

@hiroshinishio
Copy link
Owner Author

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 , 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 "/node";
import { nodeProfilingIntegration } from "/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 "/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]: /api: Registered a global for diag v1.8.0.
Sentry Logger [debug]: /api: Registered a global for trace v1.8.0.
Sentry Logger [debug]: /api: Registered a global for context v1.8.0.
Sentry Logger [debug]: /api: Registered a global for propagation v1.8.0.
Sentry Logger [debug]: /instrumentation-http Applying instrumentation patch for nodejs core module on require hook { module: 'http' }
Sentry Logger [debug]: /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]: /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]: /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]: /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]: /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

@hiroshinishio
Copy link
Owner Author

I have same problem!!
image

@hiroshinishio
Copy link
Owner Author

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/

& :

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.

@hiroshinishio
Copy link
Owner Author

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

> nodejs-api.0.0 start
> node --import ./src/config/sentry.js src/app.js

TypeError: Cannot read properties of undefined (reading 'prototype')
    at MongooseInstrumentation.patch (/~/node_modules//instrumentation-mongoose/build/src/mongoose.js:66:44)
    at MongooseInstrumentation._onRequire (/~/node_modules//instrumentation/build/src/platform/node/instrumentation.js:168:39)
    at hookFn (/~/node_modules//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

@hiroshinishio
Copy link
Owner Author

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

@hiroshinishio
Copy link
Owner Author

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.

@hiroshinishio
Copy link
Owner Author

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

@hiroshinishio
Copy link
Owner Author

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, /node:8.15.0,/profiling-node:8.15.0.

@hiroshinishio
Copy link
Owner Author

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

@hiroshinishio
Copy link
Owner Author

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

I don't have a production example, I am new to sentry and was following the docs: https://docs.sentry.io/platforms/javascript/guides/express/

My sentry.mjs file looks like this.

import * as Sentry from '/node';
import { nodeProfilingIntegration } from '/profiling-node';

Sentry.init({
  dsn: 'https://####.ingest.de.sentry.io/4507562360111184',
  integrations: [nodeProfilingIntegration()],
  // Performance Monitoring
  tracesSampleRate: 1.0, //  Capture 100% of the transactions
  // Set sampling rate for profiling - this is relative to tracesSampleRate
  profilesSampleRate: 1.0,
});

However, I was using hyper-express instead of plain express. Maybe that was the issue. Express seems to be abandoned, stuck at v4 since 2016, yet it is maintained.

@hiroshinishio
Copy link
Owner Author

However, I was using hyper-express instead of plain express.

Well that is an important detail. Assuming you mean: https://www.npmjs.com/package/hyper-express We don't support hyper-express for automatic instrumentation and don't have any plans to do so out of the box. If you're able to find or build OpenTelemetry instrumentation for that package, our SDK will collect that data.

@hiroshinishio
Copy link
Owner Author

I am facing the same thing.
Node v20.11.1
TypeScript, CJS
Latest version of Sentry SDK

I notice there is an uncaught error happening in an opentelemetry package after the express warning is issued, related to fetch(''). Not sure if that is worth me giving more detail on or not.

  • app.js:
import { makeSentry } from "utils";
makeSentry();

import * as Sentry from "/node";

import express, { Request, Response } from "express";

...

export const createApp = async () => {
  const { logger } = dependencies();

  const app = express();
  ...
  Sentry.setupExpressErrorHandler(app);
  app.use(createErrorMiddleware(logger));
  return app;
}

With my makeSentry() method here:

  • makeSentry.ts
// Import with `import * as Sentry from "/node"` if you are using ESM
import * as Sentry from "/node";
import { nodeProfilingIntegration } from "/profiling-node";

export function makeSentry() : Sentry.NodeClient | undefined {
  
  const enabled = process.env.SENTRY_ENABLED;
  if (!enabled) {
    console.info("Sentry is not enabled");
    return;
  }

  const dsn = process.env.SENTRY_DSN;
  const org = process.env.SENTRY_ORG;
  const project = process.env.SENTRY_PROJECT;

  if (!dsn || !org || !project) {
    console.error("Missing required environment variables for Sentry");
    return;
  }

  const sentryDSN = `https://${dsn}@${org}.ingest.us.sentry.io/${project}`;
  console.log(sentryDSN);
  const client = Sentry.init({
    dsn: sentryDSN,
//    environment: process.env.NODE_ENV,
    integrations: [
      nodeProfilingIntegration(),
    ],
    // Performance Monitoring
    tracesSampleRate: 1.0, //  Capture 100% of the transactions

    // Set sampling rate for profiling - this is relative to tracesSampleRate
    profilesSampleRate: 1.0,
  });
  
  return client;
}


@hiroshinishio
Copy link
Owner Author

Hello . Thanks for adding more context to this. We'll take a look.

@hiroshinishio
Copy link
Owner Author

I notice there is an uncaught error happening in an opentelemetry package after the express warning is issued, related to fetch(''). Not sure if that is worth me giving more detail on or not.

What error exactly are you getting?
If you set debug: true in your Sentry.init, could you paste the debug logs you get for your app start & around the error, possibly?

@hiroshinishio
Copy link
Owner Author

I tried generating logs to Sentry via JEST testing. I'm wondering if that's part of the issue? Some googling on the error code below has a lot of JEST related hits. Have you seen this before? Just as a last ditch, I deleted by node_modules folders and tried it again, with the same result. Also, the uncaught error I mentioned above stopped happening for me, so I'm unable to help there, sorry! I'm pretty sure I didn't halucinate it, but after switching branches etc, I guess something "fixed" itself there.

console.log
    Sentry Logger [log]: Initializing Sentry: process: 15825, thread: main.

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37

  console.log
    Sentry Logger [log]: Integration installed: InboundFilters

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: FunctionToString

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: LinkedErrors

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: RequestData

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Console

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Http

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: NodeFetch

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: OnUncaughtException

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: OnUnhandledRejection

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: ContextLines

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: LocalVariablesAsync

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Context

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Modules

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Express

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Fastify

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Graphql

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Mongo

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Mongoose

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Mysql

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Mysql2

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Redis

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Postgres

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Nest

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Hapi

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Koa

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: Connect

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: [Profiling] Profiling integration setup.

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: [Profiling] Span profiler mode enabled.

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Integration installed: ProfilingIntegration

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37
          at Array.forEach (<anonymous>)

  console.log
    Sentry Logger [log]: Running in CommonJS mode.

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37

  console.debug
    Sentry Logger [debug]: /api: Registered a global for diag v1.9.0.

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37

  console.debug
    Sentry Logger [debug]: /api: Registered a global for trace v1.9.0.

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37

  console.debug
    Sentry Logger [debug]: /api: Registered a global for context v1.9.0.

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37

  console.debug
    Sentry Logger [debug]: /api: Registered a global for propagation v1.9.0.

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37

  console.log
    Sentry Logger [log]: Error while loading NodeFetch instrumentation: 
     TypeError: A dynamic import callback was invoked without --experimental-vm-modules
        at importModuleDynamicallyCallback (node:internal/modules/esm/utils:211:11)
        at getInstrumentation (/Users/gregory/Work/booking-backend/node_modules/.pnpm/+node.24.0/node_modules//node/src/integrations/node-fetch.ts:57:19)
        at Object.setupOnce (/Users/gregory/Work/booking-backend/node_modules/.pnpm/+node.24.0/node_modules//node/src/integrations/node-fetch.ts:131:35)
        at setupIntegration (/Users/gregory/Work/booking-backend/node_modules/.pnpm/+core.24.0/node_modules//core/src/integration.ts:122:105)
        at /Users/gregory/Work/booking-backend/node_modules/.pnpm/+core.24.0/node_modules//core/src/integration.ts:93:7
        at Array.forEach (<anonymous>)
        at Object.setupIntegrations (/Users/gregory/Work/booking-backend/node_modules/.pnpm/+core.24.0/node_modules//core/src/integration.ts:90:16)
        at NodeClient.setupIntegrations [as _setupIntegrations] (/Users/gregory/Work/booking-backend/node_modules/.pnpm/+core.24.0/node_modules//core/src/baseclient.ts:575:20)
        at NodeClient.init (/Users/gregory/Work/booking-backend/node_modules/.pnpm/+core.24.0/node_modules//core/src/baseclient.ts:315:12)
        at _init (/Users/gregory/Work/booking-backend/node_modules/.pnpm/+node.24.0/node_modules//node/src/sdk/index.ts:148:12)
        at Object.init (/Users/gregory/Work/booking-backend/node_modules/.pnpm/+node.24.0/node_modules//node/src/sdk/index.ts:103:10)
        at init (/Users/gregory/Work/booking-backend/utils/dist/sentry.js:45:27)
        at Object.<anonymous> (/Users/gregory/Work/booking-backend/account/src/app.ts:2:11)
        at Runtime._execModule (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runtime.7.0/node_modules/jest-runtime/build/index.js:1439:24)
        at Runtime._loadModule (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runtime.7.0/node_modules/jest-runtime/build/index.js:1022:12)
        at Runtime.requireModule (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runtime.7.0/node_modules/jest-runtime/build/index.js:882:12)
        at Runtime.requireModuleOrMock (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runtime.7.0/node_modules/jest-runtime/build/index.js:1048:21)
        at Object.<anonymous> (/Users/gregory/Work/booking-backend/account/__tests__/integration/app.test.ts:1:1)
        at Runtime._execModule (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runtime.7.0/node_modules/jest-runtime/build/index.js:1439:24)
        at Runtime._loadModule (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runtime.7.0/node_modules/jest-runtime/build/index.js:1022:12)
        at Runtime.requireModule (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runtime.7.0/node_modules/jest-runtime/build/index.js:882:12)
        at jestAdapter (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-circus.7.0/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:77:13)
        at processTicksAndRejections (node:internal/process/task_queues:95:5)
        at runTestInternal (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runner.7.0/node_modules/jest-runner/build/runTest.js:367:16)
        at runTest (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runner.7.0/node_modules/jest-runner/build/runTest.js:444:34) {
      code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG'
    }

      at ../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:86:37

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

      91 |   });
      92 |   
    > 93 |   Sentry.setupExpressErrorHandler(app);
         |          ^
      94 |   app.use(createErrorMiddleware(logger));
      95 |
      96 |   consumeNotificationMessages();

      at ../node_modules/.pnpm/+node.24.0/node_modules//node/src/utils/ensureIsWrapped.ts:18:17
      at Object.consoleSandbox (../node_modules/.pnpm/+utils.24.0/node_modules//utils/src/logger.ts:59:12)
      at Object.consoleSandbox [as ensureIsWrapped] (../node_modules/.pnpm/+node.24.0/node_modules//node/src/utils/ensureIsWrapped.ts:15:5)
      at Object.ensureIsWrapped [as setupExpressErrorHandler] (../node_modules/.pnpm/+node.24.0/node_modules//node/src/integrations/tracing/express.ts:145:60)
      at setupExpressErrorHandler (src/app.ts:93:10)
      at Object.<anonymous> (__tests__/integration/app.test.ts:34:11)

@hiroshinishio
Copy link
Owner Author

Can you elaborate what you are trying to do? I don't fully understand how jest is involved or what you are trying to achieve.

@hiroshinishio
Copy link
Owner Author

Is this only happening in Jest tests? This is not really something we support (well) right now, because the Jest environment is not a proper/full Node environment, so stuff may not be working there properly 🤔

@hiroshinishio
Copy link
Owner Author

I don't know if it's happening correctly in prod / stage. I couldn't generate the correct events in test, so I didn't go further. , I just used my tests as an easy way to generate events for Sentry (normally I would turn off events from local / test). First time I've used Sentry with Node. I'll try running it for real and see what happens.

@hiroshinishio
Copy link
Owner Author

/ : I guess I should apologize since I can see events in my sentry account when I just run it normally. We're moving really fast here, but I should have tried this before logging a ticket. My bad. If Sentry doesn't work within a Jest context, perhaps Sentry can test for this and log a warning to the console for a better developer experience? Not sure if that's easy to detect or not. Anyways, glad we can get up and running with the product!

@hiroshinishio
Copy link
Owner Author

🙏 all good, that's great to hear!

I think we could potentially add a check for process.env.JEST_WORKER_ID when calling init in /node and log a small, helpful warning there. PRs for this are welcome! :)

@hiroshinishio
Copy link
Owner Author

/ : I'm not entirely sure what's happened, but I managed to get some events, but I'm also still getting the warning and most of the events are not happening. It's intermittent. I think it's because we are using express v5.0.0-beta.3 on most of our services. I downgraded to 4, and I don't see the Sentry warning. But I'm not convinced this is 100% it.

Also, I get this error consistently.

Exception has occurred: TypeError: Failed to parse URL from 
  at new Request (node:internal/deps/undici/undici:5855:19)
    at fetch (node:internal/deps/undici/undici:10123:25)
    at Object.fetch (node:internal/deps/undici/undici:12344:10)
    at fetch (node:internal/process/pre_execution:336:27)
    at loadFetch (/Users/gregory/Work/booking-backend/node_modules/.pnpm/opentelemetry-instrumentation-fetch-node.2.3_+api.9.0/node_modules/opentelemetry-instrumentation-fetch-node/src/index.ts:81:11)
    at new FetchInstrumentation (/Users/gregory/Work/booking-backend/node_modules/.pnpm/opentelemetry-instrumentation-fetch-node.2.3_+api.9.0/node_modules/opentelemetry-instrumentation-fetch-node/src/index.ts:121:5)

@hiroshinishio
Copy link
Owner Author

We'll continue to look into this next week as this week is Hackweek at Sentry.

@hiroshinishio
Copy link
Owner Author

@hiroshinishio
Copy link
Owner Author

We should add an option to opt-out of these warnings, IMHO. I opened an issue for this: getsentry#13471

In this case it is true/correct that express will not be properly performance instrumented. But there is nothing you can do about this as a user, and error monitoring should still work, so you can safely ignore this warning - you'll get basic (un-parametrized) http.server spans through the http instrumentation, but no express-specific tracing data, sadly. I think it is unlikely that OpenTelemetry support will be added before v5 of express is out of beta (which, who nows if/when this will happen :O )

@hiroshinishio
Copy link
Owner Author

if Sentry will simply not work with Express v5, you really need to put that on your documentation. This page would be a good spot. I reviewed it and I didn't see any block indicating express version that Sentry is compatible with.

https://docs.sentry.io/platforms/javascript/guides/express/

Tooling with express is a critical requirement for what we need, so I have a hard choice to either downgrade express or find a different provider for error capture.

@hiroshinishio
Copy link
Owner Author

if Sentry will simply not work with Express v5, you really need to put that on your documentation. This page would be a good spot. I reviewed it and I didn't see any block indicating express version that Sentry is compatible with.

https://docs.sentry.io/platforms/javascript/guides/express/

Tooling with express is a critical requirement for what we need, so I have a hard choice to either downgrade express or find a different provider for error capture.

So error monitoring should still work, I believe. As long as middlewares have not changed, ours should still be compatible. Only tracing (performance monitoring) will not work in detail, as mentioned above - meaning you will still get traces, only without parametrisation, and no middleware spans. But there should still be enough details to see what's generally going on!

Generally we do not support beta-level releases, unless otherwise stated. When express v5 goes stable, we will make sure to support it as soon as possible. From what I see from release notes etc. middlewares should not (?) have changed, so you can ignore the warning that instrumentation did not work for now. Alternatively, if you really want to get rid of the warning, you can also manually add the middleware like this:

import * as Sentry from '/node';

app.use(Sentry.expressErrorHandler());

@hiroshinishio
Copy link
Owner Author

Issue with Feathers Framework

In case anyone finds this issue and uses feathers with express: The warning is generated because the function setupExpressErrorHandler() checks if the express function app.use() was wrapped by Sentry. Even if it was, the check always fails and produces a warning because the feathers function feathersExpress() overwrites the express app's app.use() with its own implementation - thus the warning.

Solution/Workaround

Skip the check.
Sentry's setupExpressErrorHandler() looks like this:

function setupExpressErrorHandler(
  app,
  options,
) {
  app.use(expressErrorHandler(options));
  ensureIsWrapped(app.use, 'express');
}

Therefore, you can simply replace Sentry.setupExpressErrorHandler(app, options) with app.use(expressErrorHandler(options));

@hiroshinishio
Copy link
Owner Author

I removed this error middleware:
Sentry.setupExpressErrorHandler(app);

and instead used this one:
app.use(Sentry.expressErrorHandler());

The [Sentry] Express is not instrumented warning went away.

@hiroshinishio
Copy link
Owner Author

Hi, I wouldn't recommend removing the setupExpressErrorHandler function as we could theoretically also use it for more things than just for adding the middleware. Unless of course, you know what you're doing and you're willing to take that chance :)

getsentry#13471 will provide you an option to silence these warnings once it is merged and released (soon).

@hiroshinishio
Copy link
Owner Author

FYI you can now configure disableInstrumentationWarnings: true in your init to avoid the warning!

see getsentry#13693

Copy link

gitauto-for-dev bot commented Oct 6, 2024

Sorry, we have an error. Please try again.

Have feedback or need help?
Feel free to email [email protected].

Copy link

gitauto-for-dev bot commented Oct 6, 2024

@hiroshinishio Pull request completed! Check it out here #21 🚀
Should you have any questions or wish to change settings or limits, please feel free to contact [email protected] or invite us to Slack Connect.

Copy link

gitauto-ai bot commented Oct 11, 2024

@hiroshinishio Pull request completed! Check it out here #24 🚀

Note: I automatically create a pull request for an unassigned and open issue in order from oldest to newest once a day at 00:00 UTC, as long as you have remaining automation usage. Should you have any questions or wish to change settings or limits, please feel free to contact [email protected] or invite us to Slack Connect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment