Skip to content

feat(hono): add orchestrion-based auto-instrumentation - #24497

Open
mydea wants to merge 8 commits into
feat/hono-move-to-server-utilsfrom
feat/hono-orchestrion-instrumentation
Open

mydea wants to merge 8 commits into
feat/hono-move-to-server-utilsfrom
feat/hono-orchestrion-instrumentation

Conversation

@mydea

@mydea mydea commented Sep 18, 2026

Copy link
Copy Markdown
Member

Second of two stacked PRs splitting the Hono instrumentation rework (originally #24371). Stacked on #24496 — review/merge that first; the diff here is against the base PR's branch.

Adds honoIntegration, the auto-instrumentation that hooks Hono through the orchestrion module transform (node:diagnostics_channel) so requests are route-enriched without a manual sentry() middleware, plus its manual counterpart honoMiddleware. Registers it in getTracingIntegrations() and re-exports both from the server runtimes (node, cloudflare, bun, deno, and the serverless / meta-framework packages).

Also adds the orchestrion transform config for hono, node-integration-tests for the auto-instrumentation, and a new orchestrion-based hono-4 e2e app (the middleware-based app now lives as hono-4-legacy, added in the base PR). node-mastra now asserts route-enriched Hono spans in prod, where Hono is external and orchestrion-instrumented.

🤖 Generated with Claude Code

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@github-actions

github-actions Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 29.09 kB +0.05% +14 B 🔺
@sentry/browser - with treeshaking flags 27.35 kB +0.07% +17 B 🔺
@sentry/browser - with treeshaking flags tracing without tracing 27.26 kB +0.07% +19 B 🔺
@sentry/browser (incl. Tracing) 50.56 kB +0.03% +15 B 🔺
@sentry/browser (incl. Tracing + Span Streaming) 50.58 kB +0.03% +14 B 🔺
@sentry/browser (incl. Tracing, Profiling) 53.56 kB +0.03% +15 B 🔺
@sentry/browser (incl. Tracing, Replay) 90.11 kB +0.03% +22 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 79.21 kB +0.04% +24 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 94.81 kB +0.03% +23 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 107.78 kB +0.03% +25 B 🔺
@sentry/browser (incl. Feedback) 46.62 kB +0.04% +15 B 🔺
@sentry/browser (incl. sendFeedback) 34.15 kB +0.05% +15 B 🔺
@sentry/browser (incl. FeedbackAsync) 39.26 kB +0.05% +16 B 🔺
@sentry/browser (incl. Metrics) 30.1 kB +0.05% +15 B 🔺
@sentry/browser (incl. Logs) 30.35 kB +0.05% +13 B 🔺
@sentry/browser (incl. Metrics & Logs) 31.02 kB +0.05% +14 B 🔺
@sentry/react 30.84 kB +0.06% +18 B 🔺
@sentry/react (incl. Tracing) 52.88 kB +0.05% +24 B 🔺
@sentry/vue 36.34 kB +0.04% +14 B 🔺
@sentry/vue (incl. Tracing) 52.86 kB +0.03% +14 B 🔺
@sentry/svelte 29.11 kB +0.06% +15 B 🔺
CDN Bundle 30.8 kB +0.12% +34 B 🔺
CDN Bundle (incl. Tracing) 51.1 kB +0.09% +43 B 🔺
CDN Bundle (incl. Logs, Metrics) 33.06 kB +0.11% +36 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) 53.09 kB +0.09% +43 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 73.75 kB +0.06% +38 B 🔺
CDN Bundle (incl. Tracing, Replay) 88.66 kB +0.05% +43 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 90.58 kB +0.05% +44 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 94.7 kB +0.05% +41 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 96.71 kB +0.05% +44 B 🔺
CDN Bundle - uncompressed 91.16 kB +0.1% +89 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 152.53 kB +0.06% +89 B 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 97.73 kB +0.1% +89 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 158.49 kB +0.06% +89 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 227.14 kB +0.04% +89 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 272.1 kB +0.04% +89 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 278.04 kB +0.04% +89 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 285.81 kB +0.04% +89 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 291.74 kB +0.04% +89 B 🔺
@sentry/nextjs (client) 55.21 kB +0.04% +21 B 🔺
@sentry/sveltekit (client) 50.99 kB +0.03% +14 B 🔺
@sentry/core/server 40 kB +0.08% +30 B 🔺
@sentry/core/browser 13.66 kB +0.24% +32 B 🔺
@sentry/node 135.29 kB +1.46% +1.94 kB 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 82.44 kB +0.35% +287 B 🔺
@sentry/node - without tracing 91.78 kB +2.09% +1.88 kB 🔺
@sentry/node - without channel injection 113.87 kB +1.55% +1.74 kB 🔺
@sentry/aws-serverless 100.03 kB +1.9% +1.86 kB 🔺
@sentry/cloudflare (withSentry) - minified 204.74 kB +0.04% +63 B 🔺
@sentry/cloudflare (withSentry) 509.41 kB +0.02% +71 B 🔺

View base workflow run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread packages/server-utils/src/integrations/hono/createHonoMiddleware.ts
Comment thread packages/server-utils/src/integrations/hono/honoIntegration.ts
if (!IS_DEV) {
expect(attrValue(serverSpan, 'sentry.segment.name.source')).toBe('route');
expect(attrValue(serverSpan, 'http.route')).toBe('/manual-route');
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Conditional assertions in one test

Low Severity

This was flagged because the testing conventions ask to split conditionals in a single test into separate tests per path. Both Mastra e2e tests branch on IS_DEV inside one case, so the route-enriched prod assertions and the un-enriched dev assertions never run as independent tests.

Additional Locations (1)
Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 74f5d86. Configure here.

@mydea
mydea force-pushed the feat/hono-orchestrion-instrumentation branch from d3071cb to bef613e Compare September 21, 2026 08:13

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread dev-packages/e2e-tests/test-applications/hono-4/package.json
// A Hono `matchResult[0]` entry: `[[handler, routeMeta], paramIndexMap]`. `compose` reads the handler
// at `entry[0][0]`; the `matchedRoutes` getter reads `routeMeta` at `entry[0][1]`.
// oxlint-disable-next-line typescript/no-explicit-any
type MatchedHandlerEntry = [[any, any], any];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unguarded any in Hono integration

Low Severity

New SDK source uses any (and oxlint suppressions) on MatchedHandlerEntry, channel arguments, and tracingChannel payloads without a comment explaining why a safer type is not possible. This was flagged because the review rules require that explanation on each new any in production code.

Additional Locations (2)
Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit bef613e. Configure here.

mydea and others added 4 commits September 21, 2026 11:04
Add `honoIntegration`, the auto-instrumentation that hooks Hono through the
orchestrion module transform (`node:diagnostics_channel`) so requests are
route-enriched without a manual `sentry()` middleware, plus its manual
counterpart `honoMiddleware`. Register it in `getTracingIntegrations()` and
re-export both from the server runtimes (node, cloudflare, bun, deno, and the
serverless/meta-framework packages).

Adds the orchestrion transform config for `hono`, node-integration-tests for
the auto-instrumentation, and a new orchestrion-based `hono-4` e2e app (the
middleware-based app now lives as `hono-4-legacy`). node-mastra now asserts
route-enriched Hono spans in prod, where Hono is external and instrumented.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
An internal app.request() runs in a new Hono context but the same isolation scope,
so the request-handling dedup short-circuited the inner Sentry middleware before it
could capture context.error. When an outer handler swallowed a failed internal
response (degrading to a 200), the inner route's error was never reported. The
deduplicated middleware now still captures its own context's error, without
re-naming the transaction or overwriting request data.

Also streamline the orchestrion hono config comments, add a named-function
middleware span test, and add node-integration coverage for the inner-error case.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The auto-instrumentation injection loop used a raw arity check to decide which
matched handlers to wrap as middleware spans. Use the shared isMiddleware helper
instead, which unwraps onError-composed sub-app handlers before checking arity — a
case the inline check missed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Arity alone cannot tell a middleware from a route handler declared with an unused
`next` param. The matched entries carry their registration routeMeta, so apply the
same positional heuristic as wrapSubAppMiddleware: within a method+path group the
last handler is the route handler and earlier ones are middleware; `.use()` (method
'ALL') falls back to arity. Adds node-integration coverage for the arity-2 route
handler case.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@mydea
mydea force-pushed the feat/hono-orchestrion-instrumentation branch from bef613e to 8941894 Compare September 21, 2026 09:10

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 3 total unresolved issues (including 2 from previous reviews).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8941894. Configure here.

expect(attrValue(serverSpan!, 'sentry.segment.name.source')).toBe('route');
expect(attrValue(serverSpan!, 'http.route')).toMatch(/^\/api\/agents\/:[^/]+\/generate$/);
expect(serverSpan!.name).toMatch(/^POST \/api\/agents\/:[^/]+\/generate$/);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Conditional assertions in Mastra tests

Low Severity

These tests branch on IS_DEV inside a single case instead of splitting prod and dev. The dev path also never asserts that http.route and sentry.segment.name.source stay absent, so the un-instrumented Hono behavior is not actually locked in. This is flagged because the review rules require splitting conditional tests and asserting omitted payload fields.

Additional Locations (1)
Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 8941894. Configure here.

Comment on lines +108 to +121
/**
* Per-request Context hook: the heart of the automatic instrumentation.
*
* `#dispatch` builds `new Context(req, { matchResult })` before its single-handler fast-path check,
* passing the live `matchResult` array. We:
* 1. wrap the already-matched MIDDLEWARE handlers (arity ≥ 2) for spans — route handlers (arity < 2)
* are covered by the request span and left as-is;
* 2. prepend the Sentry request/response middleware, so it runs first in the composed chain. That
* both drives route naming / request data / error capture (from inside the chain, with the
* Context) and forces the ≥2-handler `compose` path, so there is no fast-path gap.
*
* All of this runs per request, so it works on Cloudflare (no module-scope publish) and needs no
* app-instance patching or app-construction hook.
*/

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.

This comment is not up-to-date with the "detection" code anymore.

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 updated comments generally!


const effectiveShouldHandleError =
(scope[HONO_SHOULD_HANDLE_ERROR] as SentryHonoMiddlewareOptions['shouldHandleError']) ?? shouldHandleError;
responseHandler(context, effectiveShouldHandleError);

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.

Should we maybe clear the flags (HONO_REQUEST_HANDLED and HONO_SHOULD_HANDLE_ERROR) here after the response?

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.

this should not be necessary, because:

  • When there's a per-request isolation scope, getRequestScope stores them there. The SDK forks a fresh isolation scope per request, so the object (and its flags) is discarded when the request ends.
    • When there isn't one (isolationScope === getDefaultIsolationScope()), it deliberately falls back to the Hono Context, not the default scope — and Hono builds a new Context per dispatch. Also per-request.

mydea and others added 3 commits September 21, 2026 14:15
Restores coverage lost when the shared Hono unit tests were removed during
the move to @sentry/server-utils. The observable route-name and middleware
span-status behaviors move into the node-integration `hono` suite; the
route-hook's non-invasive prototype patching (invisible to spans/events)
is restored as a focused unit test.

- resolveRouteName: overlapping catch-all handler and middleware-only path
  fallback
- wrapMiddlewareWithSpan: 3xx/4xx middleware errors do not set an error span
  status, 5xx does
- installRouteHookOnPrototype: preserves name/length/symbols/prototype of the
  original route method

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The dedup branch of createHonoRequestMiddleware calls captureContextError, but
the mock only stubbed requestHandler/responseHandler, so the deduplication
tests threw "No captureContextError export is defined on the mock".

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Trim the comments in honoIntegration to the non-obvious reasoning (Hono's
internal matchResult shape, the cached-array injection guard, the synchronous
Context-constructor timing, the compose fast-path) and drop the narration of
what the code already shows.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@mydea
mydea marked this pull request as ready for review September 21, 2026 12:52
@mydea
mydea requested review from a team as code owners September 21, 2026 12:52
@mydea
mydea requested review from JPeer264, isaacs, nicohrubec and s1gr1d and removed request for a team September 21, 2026 12:52
The Hono auto-instrumentation added to the Node SDK defaults pushes the ESM
bundle to 135.3 KB, just over the 135 KB limit.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Comment on lines +85 to +88
// outer context never sees the error. Route naming and request data stay owned by the request
// that ran first, so only the error is captured here.
const dedupShouldHandleError =
(scope[HONO_SHOULD_HANDLE_ERROR] as SentryHonoMiddlewareOptions['shouldHandleError']) ?? shouldHandleError;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: When both automatic honoIntegration and manual honoMiddleware are used, an error can be captured twice, leading to duplicate reports if dedupeIntegration is disabled.
Severity: LOW

Suggested Fix

To prevent duplicate error capturing, the logic should be updated. One approach is to check if the error has already been captured before attempting to capture it again. For example, the deduplicated middleware branch could skip calling captureContextError, or the responseHandler in the auto-middleware could add a check to see if the error was already handled by a downstream manual middleware.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/server-utils/src/integrations/hono/createHonoMiddleware.ts#L85-L88

Potential issue: When a user enables both the automatic `honoIntegration` and manually
adds the `honoMiddleware`, an error in a route handler can be captured twice. The
auto-instrumented middleware runs first, sets a `HONO_REQUEST_HANDLED` flag, and
proceeds. The manual middleware then executes, sees the flag, and captures the error in
its deduplication branch. After the handler chain completes, the original
auto-instrumented middleware's `responseHandler` also runs and captures the same error
again. This results in duplicate error reports for users who have disabled the default
`dedupeIntegration`.

Did we get this right? 👍 / 👎 to inform future reviews.

This branch has not been deployed

No deployments
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.

2 participants