How to add Sentry breadcrumbs to Sentry transaction for Node.js?

  Kiến thức lập trình

I need a some help with Sentry for Node.js.
I’m trying to use breadcrumbs to integrate with Sentry. For errors everything is working fine, I can see breadcrumbs on the event page. But for transactions they are not included in the event for some reason.
It’s my code:

import * as Sentry from '@sentry/node';
import { nodeProfilingIntegration } from '@sentry/profiling-node';
import express from "express";

const app = express();

Sentry.init({
  dsn: '***',
  integrations: [
    new Sentry.Integrations.Http({ tracing: true }),
    new Sentry.Integrations.Express({ app }),
    nodeProfilingIntegration(),
  ],
  tracesSampleRate: 1.0,
  profilesSampleRate: 1.0,
});

app.use(Sentry.Handlers.requestHandler());

app.use(Sentry.Handlers.tracingHandler());

// I CAN'T see this breadcrumb on Event's page in Sentry UI! 
app.get("/broken-breadcrumb", function rootHandler(req, res) {
  const message = 'Hello word of breadcrumbs!';
  console.log(message);
  Sentry.addBreadcrumb({ message });
  res.end("Hello world!");
});

// I see this breadcrumb on Event's page as expected.
app.get("/debug-sentry", function mainHandler(req, res) {
  const message = 'It's a breadcrumb for exception!';
  console.log(message);
  Sentry.addBreadcrumb({ message });
  throw new Error("My first Sentry error!");
});

app.use(Sentry.Handlers.errorHandler());

app.use(function onError(err, req, res, next) {
  res.statusCode = 500;
  res.end(res.sentry + "n");
});

app.listen(3000);

Node.js version: 20.0.5

@sentry/node and @sentry/profiling-node version: 7.112.2

Thank you so much in advance for any help!

LEAVE A COMMENT