Skip to content

feat(hono): Add base for Sentry Hono middleware (Cloudflare)#18787

Merged
s1gr1d merged 13 commits intodevelopfrom
sig/hono-package-1-cloudflare
Feb 5, 2026
Merged

feat(hono): Add base for Sentry Hono middleware (Cloudflare)#18787
s1gr1d merged 13 commits intodevelopfrom
sig/hono-package-1-cloudflare

Conversation

@s1gr1d
Copy link
Member

@s1gr1d s1gr1d commented Jan 12, 2026

Exporting a hono middleware for applications on Cloudflare with @sentry/hono/cloudflare.

This is the base setup for continuing work on this SDK. It adds the export and some integration tests.

Internal tracking issue: https://linear.app/getsentry/issue/FE-655/hono-add-cloudflare-support-1

@github-actions
Copy link
Contributor

github-actions bot commented Jan 12, 2026

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 9,090 - 9,480 -4%
GET With Sentry 1,751 19% 1,734 +1%
GET With Sentry (error only) 6,094 67% 6,163 -1%
POST Baseline 1,199 - 1,192 +1%
POST With Sentry 599 50% 604 -1%
POST With Sentry (error only) 1,042 87% 1,060 -2%
MYSQL Baseline 3,265 - 3,325 -2%
MYSQL With Sentry 511 16% 512 -0%
MYSQL With Sentry (error only) 2,659 81% 2,724 -2%

View base workflow run

@s1gr1d s1gr1d force-pushed the sig/hono-package-1-cloudflare branch from 0124325 to 872d161 Compare January 26, 2026 09:11
@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

Codecov Results 📊


Generated by Codecov Action

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

size-limit report 📦

Path Size % Change Change
@sentry/browser 25.43 kB - -
@sentry/browser - with treeshaking flags 23.9 kB - -
@sentry/browser (incl. Tracing) 42.27 kB - -
@sentry/browser (incl. Tracing, Profiling) 46.92 kB - -
@sentry/browser (incl. Tracing, Replay) 80.91 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 70.52 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 85.61 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 97.79 kB - -
@sentry/browser (incl. Feedback) 42.15 kB - -
@sentry/browser (incl. sendFeedback) 30.11 kB - -
@sentry/browser (incl. FeedbackAsync) 35.13 kB - -
@sentry/browser (incl. Metrics) 26.54 kB - -
@sentry/browser (incl. Logs) 26.69 kB - -
@sentry/browser (incl. Metrics & Logs) 27.36 kB - -
@sentry/react 27.14 kB - -
@sentry/react (incl. Tracing) 44.52 kB - -
@sentry/vue 29.87 kB - -
@sentry/vue (incl. Tracing) 44.09 kB - -
@sentry/svelte 25.44 kB - -
CDN Bundle 27.97 kB - -
CDN Bundle (incl. Tracing) 43.04 kB - -
CDN Bundle (incl. Logs, Metrics) 28.82 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 43.87 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 67.75 kB - -
CDN Bundle (incl. Tracing, Replay) 79.8 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 80.67 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 85.23 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 86.13 kB - -
CDN Bundle - uncompressed 81.83 kB - -
CDN Bundle (incl. Tracing) - uncompressed 127.54 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 84.66 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 130.37 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 208.04 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 244.14 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 246.96 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 256.94 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 259.75 kB - -
@sentry/nextjs (client) 46.87 kB - -
@sentry/sveltekit (client) 42.66 kB - -
@sentry/node-core 52.18 kB - -
@sentry/node 166.3 kB +0.01% +1 B 🔺
@sentry/node - without tracing 93.97 kB - -
@sentry/aws-serverless 109.48 kB +0.01% +1 B 🔺

View base workflow run

Comment on lines +16 to +17
// fixme - check out what removing this integration changes
// integrations: integrations => integrations.filter(integration => integration.name !== 'Hono'),
Copy link
Member Author

Choose a reason for hiding this comment

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

I only added this for future reference - this is something that I need to keep in mind

@s1gr1d s1gr1d changed the title feat(hono): Add Sentry Hono middleware for Cloudflare feat(hono): Add base for Sentry Hono middleware (Cloudflare) Feb 4, 2026
@s1gr1d s1gr1d marked this pull request as ready for review February 4, 2026 09:36
Copy link

@cursor cursor bot left a comment

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 3 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.


if (context.error) {
getClient()?.captureException(context.error);
}
Copy link

Choose a reason for hiding this comment

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

Missing mechanism option in captureException call

Medium Severity

The captureException call on line 41 is missing the required mechanism option. Per the review rules, when calling captureException, the mechanism must include handled (set to true or false) and type (a proper origin like 'auto.middleware.hono'). Other Sentry integrations (e.g., Node.js Hono at line 115-120, Cloudflare Hono at line 73) consistently include this metadata. The missing mechanism may result in incomplete exception metadata in Sentry.

Fix in Cursor Fix in Web

app.use(
'*',
sentry(app, {
dsn: process.env.SENTRY_DSN,
Copy link

Choose a reason for hiding this comment

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

Using process.env instead of Cloudflare environment bindings

Medium Severity

The test file uses process.env.SENTRY_DSN which is not available in Cloudflare Workers runtime. All other tests in this directory correctly use env.SENTRY_DSN from the Cloudflare environment bindings. Since process.env doesn't exist in Workers, the DSN will be undefined, causing the SDK to not send events. The middleware API needs to accept a callback or accessor function to properly retrieve the DSN from the Cloudflare env object per-request.

Fix in Cursor Fix in Web


if (context.error) {
getClient()?.captureException(context.error);
}
Copy link

Choose a reason for hiding this comment

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

Duplicate exception capture from multiple error handlers

High Severity

The responseHandler captures context.error via getClient()?.captureException(context.error), but the Cloudflare SDK's honoIntegration (enabled by default) already captures exceptions via the proxied errorHandler. When an error occurs, Hono sets context.error and invokes the error handler, where honoIntegration.handleHonoException captures it. Then responseHandler runs after next() completes and captures the same error again. This results in duplicate exception reports being sent to Sentry.

Fix in Cursor Fix in Web

@@ -0,0 +1,2 @@
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const sentryNoOp = () => {};
Copy link
Member

Choose a reason for hiding this comment

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

l: Wouldn't it be better to just not export anything? It feels odd to get a suggestion here that doesn't do anything

Image

```jsonc {tabTitle:JSON} {filename:wrangler.jsonc}
{
"compatibility_flags": [
"nodejs_als",
Copy link
Member

Choose a reason for hiding this comment

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

l: Let's remove nodejs_als entirely and only recommend nodejs_compat

Comment on lines +40 to +42
if (context.error) {
getClient()?.captureException(context.error);
}
Copy link

Choose a reason for hiding this comment

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

Bug: The middleware's check for context.error will not capture errors from route handlers, as Hono processes them through its errorHandler before the middleware can inspect the context.
Severity: MEDIUM

Suggested Fix

Remove the error handling logic from the shared middleware (lines 40-42 in middlewareHandlers.ts). Error capturing should be handled exclusively by instrumenting Hono's errorHandler, which is the established and working pattern in the Cloudflare integration.

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/hono/src/shared/middlewareHandlers.ts#L40-L42

Potential issue: The error capturing logic within the `responseHandler` at lines 40-42
relies on checking `context.error`. However, when a route handler in Hono throws an
error, Hono's internal mechanism catches it and invokes the registered `errorHandler`.
The error is not populated in `context.error` at the middleware level for this common
case. The Cloudflare integration already correctly instruments the `errorHandler` via a
proxy. This means the middleware's error check will silently fail to capture the most
common type of errors (from route handlers), creating redundant and ineffective code.

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

@s1gr1d s1gr1d merged commit a04e10b into develop Feb 5, 2026
213 checks passed
@s1gr1d s1gr1d deleted the sig/hono-package-1-cloudflare branch February 5, 2026 14:07
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