Skip to content

Strapi performance monitoring broken in strapi v4? #5716

@DawnMD

Description

@DawnMD

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/node

SDK Version

7.1.2

Framework Version

Strapi 4.3.4

Link to Sentry event

No response

Steps to Reproduce

  1. Self host strapi@latest
  2. Add official @strapi/sentry plugin
  3. Add @sentry/node and @sentry/tracing latest
  4. Add a sentry middleware at src/middlewares/sentry.js
const Sentry = require("@sentry/node");
const {
  extractTraceparentData,
  stripUrlQueryAndFragment,
} = require("@sentry/tracing");

//https://docs.sentry.io/platforms/node/guides/koa/
Sentry.init({
  dsn: process.env.DSN,
  environment: strapi.config.environment,
  integrations: [
    // enable HTTP calls tracing
    new Sentry.Integrations.Http({ tracing: true }),
  ],
  tracesSampleRate: 1,
});

const tracingMiddleWare = async (ctx) => {
  const reqMethod = (ctx.method || "").toUpperCase();
  const reqUrl = ctx.url && stripUrlQueryAndFragment(ctx.url);

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

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

  ctx.__sentry_transaction = transaction;

  Sentry.getCurrentHub().configureScope((scope) => {
    scope.setSpan(transaction);
  });

  ctx.res.on("finish", () => {
    setImmediate(() => {
      if (ctx._matchedRoute) {
        const mountPath = ctx.mountPath || "";
        transaction.setName(`${reqMethod} ${mountPath}${ctx._matchedRoute}`);
      }
      transaction.setHttpStatus(ctx.status);
      transaction.finish();
    });
  });
};

module.exports = (config, { strapi }) => {
  strapi.server.use(async (context, next) => {
    try {
      await tracingMiddleWare(context, next);
    } catch (error) {
      Sentry.captureException(error);
      throw error;
    }
  });
};
  1. Initialise @strapi/sentry at config/middlewares.js as global::sentry
  2. Add sentry plugin to config/plugins.js as
 sentry: {
    enabled: true,
    config: {
      dsn: env("DSN"),
      sendMetadata: true,
    },
  },

Expected Result

Sentry should trace the transactions made by the API calls during navigation strapi or doing graphql queries. The exact thing worked for strapi v3 but after upgrading to v4 it broke and in the context request there are no headers for sentry-tracing

Actual Result

This was from strapi v3 when it was working fine
image

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions