Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one implement Sentry Performance tracing (@sentry/tracing) in Node/Express

I am trying to integrate @sentry/tracing into a NodeJS + Express app.

Doesn't seem to be any documentation about that I can find (and I've submitted an issue to the documentation repo here).

I have poked around @sentry/tracing code and there seems to be an Express integration available.

Based on the integration source code, it looks like it should be instantiated liked this:

const express = require('express');

const app = express();

const Sentry = require('@sentry/node');
const { Integrations } = require( "@sentry/tracing");

Sentry.init({ 
    dsn: __MY_SENTRY_DSN__,
    integrations: [
        new Integrations.Express({app}),
    ]
});

I've tried this and i'm not getting any trace data in Sentry.

like image 485
Ben Avatar asked Dec 18 '25 11:12

Ben


1 Answers

Ok, so basically, the main tracing component we need here is a separate handler for express.

That tracing handler is actually part of the @sentry/node package (because logic) and is accessible as Sentry.Handlers.tracingHandler

So the correct initialisation steps are:

Sentry.init({ 
    tracesSampleRate: 1.0,  // <- ** you need this one here (Note: the docs for the BrowserTracing version says to use less for production environment or it might suck all your quota) **
    dsn: __MY_SENTRY_DSN__,
    // ...
});

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

app.use(Sentry.Handlers.tracingHandler()); // <- ** and add this one here **

// your route handlers

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

Once that's configured, you should start seeing tracing data in Performance tab.

This leaves one un-answered question though.. what is Integrations.Express?

Basically what it seems to be doing (and it's optional) is showing sub-frames (Spans in their terminology) for each middleware calls in the trace.

Here's a trace without:

without Integrations.Express

Here's a trace with:

with Integrations.Express

That's what I was able to figure out. If you think there is any mistake or if there is a better way, please correct me.

like image 179
Ben Avatar answered Dec 21 '25 03:12

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!