From b6ffd4cb0b264a94b7d9e63c41c665418ba07385 Mon Sep 17 00:00:00 2001 From: KyleTryon Date: Fri, 23 Jan 2026 14:54:20 -0500 Subject: [PATCH 1/2] fix: distributed tracing --- packages/api/src/hono/app.ts | 51 ++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/packages/api/src/hono/app.ts b/packages/api/src/hono/app.ts index 734e4b9..1ee16c9 100644 --- a/packages/api/src/hono/app.ts +++ b/packages/api/src/hono/app.ts @@ -38,8 +38,9 @@ export function createHonoApp(config: HonoAppConfig) { await next(); }); - // Sentry HTTP tracing middleware - // Creates spans for all HTTP requests with proper transaction names + // Distributed tracing middleware + // Manually extracts sentry-trace and baggage headers to continue traces from frontend + // Required per https://docs.sentry.io/platforms/javascript/guides/cloudflare/tracing/distributed-tracing/custom-instrumentation app.use("*", async (c, next) => { const Sentry = c.get("sentry"); const env = c.get("env"); @@ -49,30 +50,36 @@ export function createHonoApp(config: HonoAppConfig) { return await next(); } - // Create transaction name from method and path - const method = c.req.method; - const path = c.req.path; - - // Use Sentry.startSpan to create a trace for this HTTP request - return await Sentry.startSpan( - { - name: `${method} ${path}`, - op: "http.server", - attributes: { - "http.method": method, - "http.route": path, - "http.url": c.req.url, - }, - }, - async (span) => { - await next(); - - // Add response status to span using the provided span parameter - span.setAttribute("http.status_code", c.res.status); + // Extract distributed tracing headers sent by frontend + const sentryTrace = c.req.header("sentry-trace"); + const baggage = c.req.header("baggage"); + + // Continue trace from frontend (or start new trace if no headers) + return await Sentry.continueTrace( + { sentryTrace, baggage }, + async () => { + // Create HTTP server span within the continued trace + return await Sentry.startSpan( + { + name: `${c.req.method} ${c.req.path}`, + op: "http.server", + attributes: { + "http.method": c.req.method, + "http.route": c.req.path, + "http.url": c.req.url, + }, + }, + async (span) => { + await next(); + // Add response status after request completes + span.setAttribute("http.status_code", c.res.status); + } + ); } ); }); + // CORS middleware (must be before routes) const corsOrigins = getCorsOrigins(config.env); console.log("🔧 CORS Configuration:", { From 072d2903306af516304209812547c6d7e97b38fe Mon Sep 17 00:00:00 2001 From: KyleTryon Date: Fri, 23 Jan 2026 14:58:43 -0500 Subject: [PATCH 2/2] chore: format --- packages/api/src/hono/app.ts | 40 ++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/packages/api/src/hono/app.ts b/packages/api/src/hono/app.ts index 1ee16c9..322a1de 100644 --- a/packages/api/src/hono/app.ts +++ b/packages/api/src/hono/app.ts @@ -55,31 +55,27 @@ export function createHonoApp(config: HonoAppConfig) { const baggage = c.req.header("baggage"); // Continue trace from frontend (or start new trace if no headers) - return await Sentry.continueTrace( - { sentryTrace, baggage }, - async () => { - // Create HTTP server span within the continued trace - return await Sentry.startSpan( - { - name: `${c.req.method} ${c.req.path}`, - op: "http.server", - attributes: { - "http.method": c.req.method, - "http.route": c.req.path, - "http.url": c.req.url, - }, + return await Sentry.continueTrace({ sentryTrace, baggage }, async () => { + // Create HTTP server span within the continued trace + return await Sentry.startSpan( + { + name: `${c.req.method} ${c.req.path}`, + op: "http.server", + attributes: { + "http.method": c.req.method, + "http.route": c.req.path, + "http.url": c.req.url, }, - async (span) => { - await next(); - // Add response status after request completes - span.setAttribute("http.status_code", c.res.status); - } - ); - } - ); + }, + async (span) => { + await next(); + // Add response status after request completes + span.setAttribute("http.status_code", c.res.status); + } + ); + }); }); - // CORS middleware (must be before routes) const corsOrigins = getCorsOrigins(config.env); console.log("🔧 CORS Configuration:", {