getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.87k stars 1.55k forks source link

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

Open amiranvarov opened 4 months ago

amiranvarov commented 4 months ago

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 commented 4 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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

andreiborza commented 3 months ago

Have you updated to 8.2.1 yet? Same result?

Could you please provide

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

amiranvarov commented 3 months ago

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

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

it's CJS, for sure :)

andreiborza commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

@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 commented 3 months ago

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 commented 3 months ago

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

mydea commented 3 months ago

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?

ollipa commented 2 months ago

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.

ebosetalee commented 2 months ago

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

RubeCarton6231 commented 2 months ago

I have same problem!! image

mydea commented 2 months ago

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

ebosetalee commented 2 months ago

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

> nodejs-api@1.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/@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

mydea commented 2 months ago

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

ebosetalee commented 2 months ago

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.

ollipa commented 2 months ago

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";
mr-moon commented 2 months ago

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.

lforst commented 2 months ago

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

mr-moon commented 2 months ago

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

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 '@sentry/node';
import { nodeProfilingIntegration } from '@sentry/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.

lforst commented 2 months ago

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

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

gdbjohnson commented 1 month ago

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.

import * as Sentry from "@sentry/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 "@sentry/node" if you are using ESM import * as Sentry from "@sentry/node"; import { nodeProfilingIntegration } from "@sentry/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; }

andreiborza commented 1 month ago

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

mydea commented 1 month ago

@gdbjohnson

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?

gdbjohnson commented 1 month ago

@mydea 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/@sentry+utils@8.24.0/node_modules/@sentry/utils/src/logger.ts:86:37

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      at ../node_modules/.pnpm/@sentry+utils@8.24.0/node_modules/@sentry/utils/src/logger.ts:86:37

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

      at ../node_modules/.pnpm/@sentry+utils@8.24.0/node_modules/@sentry/utils/src/logger.ts:86:37

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

      at ../node_modules/.pnpm/@sentry+utils@8.24.0/node_modules/@sentry/utils/src/logger.ts:86:37

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

      at ../node_modules/.pnpm/@sentry+utils@8.24.0/node_modules/@sentry/utils/src/logger.ts:86:37

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

      at ../node_modules/.pnpm/@sentry+utils@8.24.0/node_modules/@sentry/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/@sentry+node@8.24.0/node_modules/@sentry/node/src/integrations/node-fetch.ts:57:19)
        at Object.setupOnce (/Users/gregory/Work/booking-backend/node_modules/.pnpm/@sentry+node@8.24.0/node_modules/@sentry/node/src/integrations/node-fetch.ts:131:35)
        at setupIntegration (/Users/gregory/Work/booking-backend/node_modules/.pnpm/@sentry+core@8.24.0/node_modules/@sentry/core/src/integration.ts:122:105)
        at /Users/gregory/Work/booking-backend/node_modules/.pnpm/@sentry+core@8.24.0/node_modules/@sentry/core/src/integration.ts:93:7
        at Array.forEach (<anonymous>)
        at Object.setupIntegrations (/Users/gregory/Work/booking-backend/node_modules/.pnpm/@sentry+core@8.24.0/node_modules/@sentry/core/src/integration.ts:90:16)
        at NodeClient.setupIntegrations [as _setupIntegrations] (/Users/gregory/Work/booking-backend/node_modules/.pnpm/@sentry+core@8.24.0/node_modules/@sentry/core/src/baseclient.ts:575:20)
        at NodeClient.init (/Users/gregory/Work/booking-backend/node_modules/.pnpm/@sentry+core@8.24.0/node_modules/@sentry/core/src/baseclient.ts:315:12)
        at _init (/Users/gregory/Work/booking-backend/node_modules/.pnpm/@sentry+node@8.24.0/node_modules/@sentry/node/src/sdk/index.ts:148:12)
        at Object.init (/Users/gregory/Work/booking-backend/node_modules/.pnpm/@sentry+node@8.24.0/node_modules/@sentry/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@29.7.0/node_modules/jest-runtime/build/index.js:1439:24)
        at Runtime._loadModule (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runtime@29.7.0/node_modules/jest-runtime/build/index.js:1022:12)
        at Runtime.requireModule (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runtime@29.7.0/node_modules/jest-runtime/build/index.js:882:12)
        at Runtime.requireModuleOrMock (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runtime@29.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@29.7.0/node_modules/jest-runtime/build/index.js:1439:24)
        at Runtime._loadModule (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runtime@29.7.0/node_modules/jest-runtime/build/index.js:1022:12)
        at Runtime.requireModule (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runtime@29.7.0/node_modules/jest-runtime/build/index.js:882:12)
        at jestAdapter (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-circus@29.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@29.7.0/node_modules/jest-runner/build/runTest.js:367:16)
        at runTest (/Users/gregory/Work/booking-backend/node_modules/.pnpm/jest-runner@29.7.0/node_modules/jest-runner/build/runTest.js:444:34) {
      code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG'
    }

      at ../node_modules/.pnpm/@sentry+utils@8.24.0/node_modules/@sentry/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/@sentry+node@8.24.0/node_modules/@sentry/node/src/utils/ensureIsWrapped.ts:18:17
      at Object.consoleSandbox (../node_modules/.pnpm/@sentry+utils@8.24.0/node_modules/@sentry/utils/src/logger.ts:59:12)
      at Object.consoleSandbox [as ensureIsWrapped] (../node_modules/.pnpm/@sentry+node@8.24.0/node_modules/@sentry/node/src/utils/ensureIsWrapped.ts:15:5)
      at Object.ensureIsWrapped [as setupExpressErrorHandler] (../node_modules/.pnpm/@sentry+node@8.24.0/node_modules/@sentry/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)
lforst commented 1 month ago

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

mydea commented 1 month ago

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 🤔

gdbjohnson commented 4 weeks ago

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

gdbjohnson commented 4 weeks ago

@mydea / @lforst : 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!

mydea commented 4 weeks ago

🙏 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 @sentry/node and log a small, helpful warning there. PRs for this are welcome! :)

gdbjohnson commented 3 weeks ago

@mydea / @lforst : 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@1.2.3_@opentelemetry+api@1.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@1.2.3_@opentelemetry+api@1.9.0/node_modules/opentelemetry-instrumentation-fetch-node/src/index.ts:121:5)
Lms24 commented 3 weeks ago

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

lforst commented 2 weeks ago

@gdbjohnson That is very likely the cause as the OpenTelemetry express instrumentation is version-locked to version 4: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/de7a6cb77e643ed0de82e514510089fba5ae0405/plugins/node/opentelemetry-instrumentation-express/src/instrumentation.ts#L62

mydea commented 2 weeks ago

We should add an option to opt-out of these warnings, IMHO. I opened an issue for this: https://github.com/getsentry/sentry-javascript/issues/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 )

gdbjohnson commented 2 weeks ago

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

mydea commented 2 weeks ago

@mydea 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 '@sentry/node';

app.use(Sentry.expressErrorHandler());
SteffenLanger commented 1 day ago

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));