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

Fastify integration does not work in ESM mode #12119

Closed
3 tasks done
mohd-akram opened this issue May 19, 2024 · 7 comments
Closed
3 tasks done

Fastify integration does not work in ESM mode #12119

mohd-akram opened this issue May 19, 2024 · 7 comments

Comments

@mohd-akram
Copy link
Contributor

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

Framework Version

No response

Link to Sentry event

No response

SDK Setup

// app.mjs
import * as Sentry from "@sentry/node";
import { fastify } from "fastify";
const app = fastify();
Sentry.setupFastifyErrorHandler(app);
const address = await app.listen();
console.info(`server listening on ${address}`);
// instrument.mjs
import * as Sentry from "@sentry/node";
Sentry.init({ dsn: process.env.SENTRY_DSN, debug: true });

Steps to Reproduce

  1. node --import ./instrument.mjs app.mjs

Expected Result

Works

Actual Result

[Sentry] Fastify is not instrumented. This is likely because you required/imported fastify before calling `Sentry.init()`.
@hornta
Copy link

hornta commented May 20, 2024

I'm also getting the same issue. I followed this page: https://docs.sentry.io/platforms/javascript/guides/fastify/.

I also double checked that I import Sentry before importing anything from fastify.

I'm running node v22.1.0 and sentry v8.2.1

@andreiborza
Copy link
Member

andreiborza commented May 21, 2024

Hi, thank you for filing this.

It looks like open-telemetry doesn't properly instrument the named export of fastify. As a workaround, could you change your code to use the default export and report back please?

import fastify from 'fastify'

We are looking into raising this upstream with open-telemetry in the meantime.

@mohd-akram
Copy link
Contributor Author

It does not seem to make a difference. The full log:

Sentry Logger [log]: Initializing Sentry: process: 90697, 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 [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-http Applying instrumentation patch for nodejs core module on require hook { module: 'https' }
[Sentry] Fastify is not instrumented. This is likely because you required/imported fastify before calling `Sentry.init()`.

@andreiborza
Copy link
Member

Which node version are you running?

@mohd-akram
Copy link
Contributor Author

I'm running v20.13.1. Weirdly, making that change actually works in my production code. There, I have a bin/server in CJS that await imports fastify and the ESM app and sets up the server. I'll try to see what exactly is causing it to work or not.

@andreiborza
Copy link
Member

andreiborza commented May 21, 2024

From my testing, the issue with your sample code is two-fold. One is the named export not being instrumented properly by otel. The other is that you do not have tracesSampleRate set up in your Sentry.init call, which is why nothing's instrumented.

The messaging around that is definitely confusing though and leads users down the wrong path. I'll raise it with the team.

@mohd-akram
Copy link
Contributor Author

Ah, that explains it. Yes, the following works:

// app.mjs
import * as Sentry from "@sentry/node";
import fastify from "fastify";
const app = fastify();
Sentry.setupFastifyErrorHandler(app);
const address = await app.listen();
console.info(`server listening on ${address}`);
// instrument.mjs
import * as Sentry from "@sentry/node";
Sentry.init({
  dsn: process.env.SENTRY_DSN,
  debug: true,
  tracesSampleRate: 0.05,
});

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

No branches or pull requests

3 participants