Sentry Tracing Node appears to work but noting logged, how to debug?

Hello :wave:

TLDR at the end.

Context: I’m using StrapiJS, is a framework built on top of KoaJS. Strapi has a custom plugin for Sentry that only handles Error Tracking but no tracing, and it works perfect.

But I want to add Tracing to my app, so I just decided to add it myself on top of the plug in following the Koa documentation (Koa | Sentry Documentation).

And it seems to work pretty well, by this I mean the middleware gets called on every single route call, it gets all the context right (e.g. method, status, timestamp, etc…) and calls transaction.finish(); at the end every single time.

But nothing shows up in the Sentry dashboard. But Error Tracking still works. I checked the DSN of the Performance Tracing and the instance has the proper DSN.

So I ran out of ideas of debugging.

Here is all the code if you want it.

The plugin: strapi-plugin-sentry - npm
My Middleware based on the Sentry Koa doc:

const {
  extractTraceparentData,
  Span,
  stripUrlQueryAndFragment,
} = require("@sentry/tracing");
const domain = require("domain");

// not mandatory, but adding domains does help a lot with breadcrumbs
const requestHandler = (ctx, next) => {
  const sentryInstance = strapi.plugins.sentry.services.sentry.getInstance();
  return new Promise((resolve, _) => {
    const local = domain.create();
    local.add(ctx);
    local.on("error", (err) => {
      ctx.status = err.status || 500;
      ctx.body = err.message;
      ctx.app.emit("error", err, ctx);
    });
    local.run(async () => {
      sentryInstance.getCurrentHub().configureScope((scope) =>
        scope.addEventProcessor((event) =>
          sentryInstance.Handlers.parseRequest(event, ctx.request, {
            user: false,
          })
        )
      );
      await next();
      resolve();
    });
  });
};

// this tracing middleware creates a transaction per request
const tracingMiddleWare = async (ctx, next) => {
  console.log("tracingMiddleWare called");
  const sentryInstance = strapi.plugins.sentry.services.sentry.getInstance();
  const reqMethod = (ctx.method || "").toUpperCase();
  const reqUrl = ctx.url && stripUrlQueryAndFragment(ctx.url);

  console.log(
    "sentryInstance.getCurrentHub()._stack[0].client",
    sentryInstance.getCurrentHub()._stack[0].client
  );

  // connect to trace of upstream app
  let traceparentData;
  if (ctx.request.get("sentry-trace")) {
    traceparentData = extractTraceparentData(ctx.request.get("sentry-trace"));
  }

  const transaction = sentryInstance.startTransaction({
    name: `${reqMethod} ${reqUrl}`,
    op: "http.server",
    ...traceparentData,
  });

  ctx.__sentry_transaction = transaction;
  // We put the transaction on the scope so users can attach children to it
  sentryInstance.getCurrentHub().configureScope((scope) => {
    scope.setSpan(transaction);
  });

  ctx.res.on("finish", () => {
    // Push `transaction.finish` to the next event loop so open spans have a chance to finish before the transaction closes
    setImmediate(() => {
      // if using koa router, a nicer way to capture transaction using the matched route
      if (ctx._matchedRoute) {
        const mountPath = ctx.mountPath || "";
        transaction.setName(`${reqMethod} ${mountPath}${ctx._matchedRoute}`);
      }
      transaction.setHttpStatus(ctx.status);
      transaction.finish();
      // console.log("transaction done 2", transaction);
    });
  });

  await next();
};

module.exports = (strapi) => {
  return {
    initialize() {
      strapi.app.use(requestHandler);
      strapi.app.use(tracingMiddleWare);
    },
  };
};

strapi.plugins.sentry.services.sentry.getInstance(); is how you get the instance, checked the DSN (sentryInstance.getCurrentHub()._stack[0].client) and is the good one, so I guess that works.

TLDR:

  • Middleware appears to work
  • Error Tracking works
  • DSN properly recorded

But nothing shows up in the performance tab in the sentry dashboard.

Thanks! any help debugging this would be appreciated

Can not specifying traces_sample_rate be the problem?

I think that is the problem