Skip to content

Commit

Permalink
feat(server): force a new Sentry trace id for each request
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkdev98 committed May 2, 2024
1 parent dfc0daa commit 6ca6806
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions packages/server/src/middleware/sentry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _compasSentryExport } from "@compas/stdlib";
import { _compasSentryExport, isNil, uuid } from "@compas/stdlib";

/**
* Sentry support;
Expand All @@ -21,28 +21,32 @@ export function sentry() {
};
}

return async (ctx, next) => {
return (ctx, next) => {
if (ctx.method === "OPTIONS" || ctx.method === "HEAD") {
return next();
}

let traceParentData = {
forceTransaction: true,
};
if (ctx.request.get("sentry-trace")) {
// @ts-expect-error
traceParentData = _compasSentryExport.extractTraceparentData(
ctx.request.get("sentry-trace"),
);
if (!_compasSentryExport) {
return next();
}

// @ts-expect-error
return await _compasSentryExport.startSpanManual(
const traceHeader = ctx.request.get("sentry-trace");
/** @type {any} */
const traceParentData =
_compasSentryExport.extractTraceparentData(traceHeader) ?? {};

// Use a manual span, so we can end it right after the body is send.
return _compasSentryExport.startSpanManual(
{
// Force a new trace for every request. This keeps the traces view usable.
traceId: uuid().replace(/-/g, ""),
...traceParentData,
spanId: uuid().replace(/-/g, "").slice(16),
forceTransaction: isNil(traceParentData.parentSpanId),

op: "http.server",
name: "http",
description: "http",
...traceParentData,
attributes: {
"http.request.method": ctx.method,
"http.request.url": ctx.url,
Expand Down

0 comments on commit 6ca6806

Please sign in to comment.