Skip to content

fix(hono): Capture transaction name on request for correct culprit#20801

Merged
s1gr1d merged 2 commits into
developfrom
sig/hono-culprit
May 11, 2026
Merged

fix(hono): Capture transaction name on request for correct culprit#20801
s1gr1d merged 2 commits into
developfrom
sig/hono-culprit

Conversation

@s1gr1d
Copy link
Copy Markdown
Member

@s1gr1d s1gr1d commented May 11, 2026

Hono error events were showing a wrong culprit (?(index)) because the transaction name was only being set in the response handler. When an error was thrown during middleware execution, captureException ran before responseHandler had a chance to update the span name and isolation scope transaction name.

This PR moves the span and transaction name update into requestHandler so the correct route name is set before any middleware or route handler executes. The responseHandler now only captures errors.

Also switches from routePath(context) (last matched route so far) to routePath(context, -1) (final matched route) which avoids wildcard patterns like /error/* leaking into the transaction name.

Also adds another test for route parametrization when using middleware.

Closes #20399

@s1gr1d s1gr1d requested a review from a team as a code owner May 11, 2026 12:13
Comment thread packages/hono/src/shared/middlewareHandlers.ts
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 11, 2026

size-limit report 📦

Path Size % Change Change
@sentry/browser 26.37 kB - -
@sentry/browser - with treeshaking flags 24.82 kB - -
@sentry/browser (incl. Tracing) 44.24 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 46.46 kB - -
@sentry/browser (incl. Tracing, Profiling) 49.22 kB - -
@sentry/browser (incl. Tracing, Replay) 83.86 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 73.31 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 88.55 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 101.19 kB - -
@sentry/browser (incl. Feedback) 43.53 kB - -
@sentry/browser (incl. sendFeedback) 31.19 kB - -
@sentry/browser (incl. FeedbackAsync) 36.27 kB - -
@sentry/browser (incl. Metrics) 27.65 kB - -
@sentry/browser (incl. Logs) 27.78 kB - -
@sentry/browser (incl. Metrics & Logs) 28.47 kB - -
@sentry/react 28.11 kB - -
@sentry/react (incl. Tracing) 46.49 kB - -
@sentry/vue 31.24 kB - -
@sentry/vue (incl. Tracing) 46.09 kB - -
@sentry/svelte 26.39 kB - -
CDN Bundle 28.95 kB - -
CDN Bundle (incl. Tracing) 46.97 kB - -
CDN Bundle (incl. Logs, Metrics) 30.36 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 48.09 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 69.68 kB - -
CDN Bundle (incl. Tracing, Replay) 84.37 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 85.42 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 90.16 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 91.26 kB - -
CDN Bundle - uncompressed 84.98 kB - -
CDN Bundle (incl. Tracing) - uncompressed 140.53 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 89.17 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 144 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 214 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 259.24 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 262.69 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 272.94 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 276.38 kB - -
@sentry/nextjs (client) 49.01 kB - -
@sentry/sveltekit (client) 44.72 kB - -
@sentry/node-core 60.56 kB +0.02% +8 B 🔺
@sentry/node 165.63 kB +0.01% +8 B 🔺
@sentry/node - without tracing 73.59 kB +0.02% +12 B 🔺
@sentry/aws-serverless 107.74 kB +0.01% +7 B 🔺
@sentry/cloudflare (withSentry) - minified 169.66 kB - -
@sentry/cloudflare (withSentry) 428.38 kB - -

View base workflow run

Copy link
Copy Markdown
Member

@chargome chargome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Not sure if it would make sense to update the name in both request and reponse handler for best effort but also I'm not aware of hono internals for routing.


function updateSpanRouteName(isolationScope: Scope, context: Context): void {
const activeSpan = getActiveSpan();
const lastMatchedRoute = routePath(context, -1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: A comment explaining what this actually does would be helpful, but not worth a CI rerun

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is this always the correct one?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can include a comment in another PR.

And yes, -1 gets the last matched path which is the one where it's routed to.
https://hono.dev/docs/helpers/route#using-with-index-parameter

@s1gr1d s1gr1d merged commit b6cb7b6 into develop May 11, 2026
81 of 83 checks passed
@s1gr1d s1gr1d deleted the sig/hono-culprit branch May 11, 2026 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Hono] Use correct route names on errors

3 participants