-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(hono): Add base for Sentry Hono middleware (Cloudflare) #18787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
57d1ee1
add base hono sdk
s1gr1d 42b3106
specific request and response handlers
s1gr1d bf3691e
move test files
s1gr1d d8f3188
move middleware
s1gr1d 872d161
update tests
s1gr1d 72c50a5
add hono to verdaccio
s1gr1d d29486b
upgrade versions
s1gr1d 2ea9455
Merge branch 'develop' into sig/hono-package-1-cloudflare
s1gr1d 9f32bb6
add integration tests
s1gr1d c710e05
update wrangler
s1gr1d 3f43971
fix lint error
s1gr1d dc5cfb9
update readme etc.
s1gr1d 43f0e20
Review suggestions
s1gr1d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
38 changes: 38 additions & 0 deletions
38
dev-packages/cloudflare-integration-tests/suites/hono-sdk/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { sentry } from '@sentry/hono/cloudflare'; | ||
| import { Hono } from 'hono'; | ||
|
|
||
| interface Env { | ||
| SENTRY_DSN: string; | ||
| } | ||
|
|
||
| const app = new Hono<{ Bindings: Env }>(); | ||
|
|
||
| app.use( | ||
| '*', | ||
| sentry(app, { | ||
| dsn: process.env.SENTRY_DSN, | ||
| tracesSampleRate: 1.0, | ||
| debug: true, | ||
| // fixme - check out what removing this integration changes | ||
| // integrations: integrations => integrations.filter(integration => integration.name !== 'Hono'), | ||
|
Comment on lines
+16
to
+17
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| }), | ||
| ); | ||
|
|
||
| app.get('/', c => { | ||
| return c.text('Hello from Hono on Cloudflare!'); | ||
| }); | ||
|
|
||
| app.get('/json', c => { | ||
| return c.json({ message: 'Hello from Hono', framework: 'hono', platform: 'cloudflare' }); | ||
| }); | ||
|
|
||
| app.get('/error', () => { | ||
| throw new Error('Test error from Hono app'); | ||
| }); | ||
|
|
||
| app.get('/hello/:name', c => { | ||
| const name = c.req.param('name'); | ||
| return c.text(`Hello, ${name}!`); | ||
| }); | ||
|
|
||
| export default app; | ||
99 changes: 99 additions & 0 deletions
99
dev-packages/cloudflare-integration-tests/suites/hono-sdk/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { expect, it } from 'vitest'; | ||
| import { eventEnvelope, SHORT_UUID_MATCHER, UUID_MATCHER } from '../../expect'; | ||
| import { createRunner } from '../../runner'; | ||
|
|
||
| it('Hono app captures errors (Hono SDK)', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expect( | ||
| eventEnvelope( | ||
| { | ||
| level: 'error', | ||
| transaction: 'GET /error', | ||
| exception: { | ||
| values: [ | ||
| { | ||
| type: 'Error', | ||
| value: 'Test error from Hono app', | ||
| stacktrace: { | ||
| frames: expect.any(Array), | ||
| }, | ||
| mechanism: { type: 'auto.faas.hono.error_handler', handled: false }, | ||
| }, | ||
| ], | ||
| }, | ||
| request: { | ||
| headers: expect.any(Object), | ||
| method: 'GET', | ||
| url: expect.any(String), | ||
| }, | ||
| }, | ||
| { includeSampleRand: true, sdk: 'hono' }, | ||
| ), | ||
| ) | ||
| .expect(envelope => { | ||
| const [, envelopeItems] = envelope; | ||
| const [itemHeader, itemPayload] = envelopeItems[0]; | ||
|
|
||
| expect(itemHeader.type).toBe('transaction'); | ||
|
|
||
| expect(itemPayload).toMatchObject({ | ||
| type: 'transaction', | ||
| platform: 'javascript', | ||
| transaction: 'GET /error', | ||
| contexts: { | ||
| trace: { | ||
| span_id: expect.any(String), | ||
| trace_id: expect.any(String), | ||
| op: 'http.server', | ||
| status: 'internal_error', | ||
| origin: 'auto.http.cloudflare', | ||
| }, | ||
| }, | ||
| request: expect.objectContaining({ | ||
| method: 'GET', | ||
| url: expect.stringContaining('/error'), | ||
| }), | ||
| }); | ||
| }) | ||
|
|
||
| .unordered() | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/error', { expectError: true }); | ||
| await runner.completed(); | ||
| }); | ||
|
|
||
| it('Hono app captures parametrized names', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const [, envelopeItems] = envelope; | ||
| const [itemHeader, itemPayload] = envelopeItems[0]; | ||
|
|
||
| expect(itemHeader.type).toBe('transaction'); | ||
|
|
||
| expect(itemPayload).toMatchObject({ | ||
| type: 'transaction', | ||
| platform: 'javascript', | ||
| transaction: 'GET /hello/:name', | ||
| contexts: { | ||
| trace: { | ||
| span_id: SHORT_UUID_MATCHER, | ||
| trace_id: UUID_MATCHER, | ||
| op: 'http.server', | ||
| status: 'ok', | ||
| origin: 'auto.http.cloudflare', | ||
| }, | ||
| }, | ||
| request: expect.objectContaining({ | ||
| method: 'GET', | ||
| url: expect.stringContaining('/hello/:name'), | ||
| }), | ||
| }); | ||
| }) | ||
|
|
||
| .unordered() | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/hello/:name', { expectError: false }); | ||
| await runner.completed(); | ||
| }); |
7 changes: 7 additions & 0 deletions
7
dev-packages/cloudflare-integration-tests/suites/hono-sdk/wrangler.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "hono-sdk-worker", | ||
| "compatibility_date": "2025-06-17", | ||
| "main": "index.ts", | ||
| "compatibility_flags": ["nodejs_compat"] | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| module.exports = { | ||
| env: { | ||
| node: true, | ||
| }, | ||
| extends: ['../../.eslintrc.js'], | ||
| rules: { | ||
| '@sentry-internal/sdk/no-class-field-initializers': 'off', | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2026 Functional Software, Inc. dba Sentry | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| this software and associated documentation files (the "Software"), to deal in | ||
| the Software without restriction, including without limitation the rights to | ||
| use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
| of the Software, and to permit persons to whom the Software is furnished to do | ||
| so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <p align="center"> | ||
| <a href="https://sentry.io/?utm_source=github&utm_medium=logo" target="_blank"> | ||
| <img src="https://sentry-brand.storage.googleapis.com/sentry-wordmark-dark-280x84.png" alt="Sentry" width="280" height="84"> | ||
| </a> | ||
| </p> | ||
|
|
||
| # Official Sentry SDK for Hono (ALPHA) | ||
|
|
||
| [](https://www.npmjs.com/package/@sentry/hono) | ||
| [](https://www.npmjs.com/package/@sentry/hono) | ||
| [](https://www.npmjs.com/package/@sentry/hono) | ||
|
|
||
| ## Links | ||
|
|
||
| - [Official SDK Docs](https://docs.sentry.io/quickstart/) | ||
|
|
||
| ## Install | ||
|
|
||
| To get started, first install the `@sentry/hono` package: | ||
|
|
||
| ```bash | ||
| npm install @sentry/hono | ||
| ``` | ||
|
|
||
| ## Setup (Cloudflare Workers) | ||
|
|
||
| ### Enable Node.js compatibility | ||
|
|
||
| Either set the `nodejs_compat` compatibility flags in your `wrangler.jsonc`/`wrangler.toml` config. This is because the SDK needs access to the `AsyncLocalStorage` API to work correctly. | ||
|
|
||
| ```jsonc {tabTitle:JSON} {filename:wrangler.jsonc} | ||
| { | ||
| "compatibility_flags": ["nodejs_compat"], | ||
| } | ||
| ``` | ||
|
|
||
| ```toml {tabTitle:Toml} {filename:wrangler.toml} | ||
| compatibility_flags = ["nodejs_compat"] | ||
| ``` | ||
|
|
||
| ### Initialize Sentry in your Hono app | ||
|
|
||
| Initialize the Sentry Hono middleware as early as possible in your app: | ||
|
|
||
| ```typescript | ||
| import { sentry } from '@sentry/hono/cloudflare'; | ||
|
|
||
| const app = new Hono(); | ||
|
|
||
| app.use( | ||
| '*', | ||
| sentry(app, { | ||
| dsn: 'your-sentry-dsn', | ||
| }), | ||
| ); | ||
|
|
||
| export default app; | ||
| ``` |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
process.envinstead of Cloudflare environment bindingsMedium Severity
The test file uses
process.env.SENTRY_DSNwhich is not available in Cloudflare Workers runtime. All other tests in this directory correctly useenv.SENTRY_DSNfrom the Cloudflare environment bindings. Sinceprocess.envdoesn't exist in Workers, the DSN will beundefined, 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 Cloudflareenvobject per-request.