Skip to content

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

Closed
@amiranvarov

Description

@amiranvarov

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Package: nodeIssues related to the Sentry Node SDK

    Type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions