Performance Monitoring

When integrating Sentry with a Fastify application, you can leverage provided middlewares to automatically instrument and monitor the performance of your application.

Copied
const Sentry = require("@sentry/node");

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",

  // We recommend adjusting this value in production, or using tracesSampler
  // for finer control
  tracesSampleRate: 1.0,
});

const { fastify } = require("fastify");
const app = fastify();

Sentry.setupFastifyErrorHandler(app);

// the rest of your app

app.listen({ port: 3030 });

You can also manually create spans in your app:

Copied
const Sentry = require("@sentry/node");

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  tracesSampleRate: 1.0,
});

// make sure to only require `http` after Sentry has been initialized!
const { fastify } = require("fastify");
const http = require("http");

const app = fastify();
Sentry.setupFastifyErrorHandler(app);

app.get("/my-route", function (req, res) {
  try {
    // this should generate an http span
    request = http.get("http://sentry.io", (res) => {
      console.log(`STATUS: ${res.statusCode}`);
      console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
    });

    // this error event should have trace context
    foo();
  } catch (err) {
    Sentry.captureException(err);
  }
});

app.listen({ port: 3030 });

See Automatic Instrumentation to learn about all the things that the SDK automatically instruments for you.

You can also manually start spans to instrument specific parts of your code. This is useful when you want to measure the performance of a specific operation or function.

See Custom Instrumentation on how to manually start spans.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").