-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(hono): Add @sentry/hono/bun for Bun runtime
#20355
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
2 commits
Select commit
Hold shift + click to select a range
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
31 changes: 31 additions & 0 deletions
31
dev-packages/bun-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,31 @@ | ||
| import { sentry } from '@sentry/hono/bun'; | ||
| import { Hono } from 'hono'; | ||
|
|
||
| const app = new Hono(); | ||
|
|
||
| app.use( | ||
| sentry(app, { | ||
| dsn: process.env.SENTRY_DSN, | ||
| tracesSampleRate: 1.0, | ||
| }), | ||
| ); | ||
|
|
||
| app.get('/', c => { | ||
| return c.text('Hello from Hono on Bun!'); | ||
| }); | ||
|
|
||
| app.get('/hello/:name', c => { | ||
| const name = c.req.param('name'); | ||
| return c.text(`Hello, ${name}!`); | ||
| }); | ||
|
|
||
| app.get('/error/:param', () => { | ||
| throw new Error('Test error from Hono app'); | ||
| }); | ||
|
|
||
| const server = Bun.serve({ | ||
| port: 0, | ||
| fetch: app.fetch, | ||
| }); | ||
|
|
||
| process.send?.(JSON.stringify({ event: 'READY', port: server.port })); |
131 changes: 131 additions & 0 deletions
131
dev-packages/bun-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,131 @@ | ||
| import { expect, it } from 'vitest'; | ||
| import { eventEnvelope, SHORT_UUID_MATCHER, UUID_MATCHER } from '../../expect'; | ||
| import { createRunner } from '../../runner'; | ||
|
|
||
| it('Hono app captures parametrized errors (Hono SDK on Bun)', 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: 'node', | ||
| transaction: 'GET /error/:param', | ||
| transaction_info: { | ||
| source: 'route', | ||
| }, | ||
| contexts: { | ||
| trace: { | ||
| span_id: expect.any(String), | ||
| trace_id: expect.any(String), | ||
| op: 'http.server', | ||
| status: 'internal_error', | ||
| origin: 'auto.http.bun.serve', | ||
| }, | ||
| response: { | ||
| status_code: 500, | ||
| }, | ||
| }, | ||
| request: expect.objectContaining({ | ||
| method: 'GET', | ||
| url: expect.stringContaining('/error/param-123'), | ||
| }), | ||
| breadcrumbs: [ | ||
| { | ||
| timestamp: expect.any(Number), | ||
| category: 'console', | ||
| level: 'error', | ||
| message: 'Error: Test error from Hono app', | ||
| data: expect.objectContaining({ | ||
| logger: 'console', | ||
| arguments: [{ message: 'Test error from Hono app', name: 'Error', stack: expect.any(String) }], | ||
| }), | ||
| }, | ||
| ], | ||
| }); | ||
| }) | ||
|
|
||
| .expect( | ||
| eventEnvelope( | ||
| { | ||
| level: 'error', | ||
| transaction: 'GET /error/:param', | ||
| exception: { | ||
| values: [ | ||
| { | ||
| type: 'Error', | ||
| value: 'Test error from Hono app', | ||
| stacktrace: { | ||
| frames: expect.any(Array), | ||
| }, | ||
| mechanism: { type: 'auto.http.hono.context_error', handled: false }, | ||
| }, | ||
| ], | ||
| }, | ||
| request: { | ||
| cookies: {}, | ||
| headers: expect.any(Object), | ||
| method: 'GET', | ||
| url: expect.stringContaining('/error/param-123'), | ||
| }, | ||
| breadcrumbs: [ | ||
| { | ||
| timestamp: expect.any(Number), | ||
| category: 'console', | ||
| level: 'error', | ||
| message: 'Error: Test error from Hono app', | ||
| data: expect.objectContaining({ | ||
| logger: 'console', | ||
| arguments: [{ message: 'Test error from Hono app', name: 'Error', stack: expect.any(String) }], | ||
| }), | ||
| }, | ||
| ], | ||
| }, | ||
| { sdk: 'hono', includeSampleRand: true, includeTransaction: true }, | ||
| ), | ||
| ) | ||
| .unordered() | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/error/param-123', { expectError: true }); | ||
| await runner.completed(); | ||
| }); | ||
|
|
||
| it('Hono app captures parametrized route names on Bun', 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: 'node', | ||
| transaction: 'GET /hello/:name', | ||
| transaction_info: { | ||
| source: 'route', | ||
| }, | ||
| contexts: { | ||
| trace: { | ||
| span_id: SHORT_UUID_MATCHER, | ||
| trace_id: UUID_MATCHER, | ||
| op: 'http.server', | ||
| status: 'ok', | ||
| origin: 'auto.http.bun.serve', | ||
| }, | ||
| }, | ||
| request: expect.objectContaining({ | ||
| method: 'GET', | ||
| url: expect.stringContaining('/hello/world'), | ||
| }), | ||
| }); | ||
| }) | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/hello/world'); | ||
| await runner.completed(); | ||
| }); |
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,28 @@ | ||
| import { type BaseTransportOptions, debug, type Options } from '@sentry/core'; | ||
| import { init } from './sdk'; | ||
| import type { Hono, MiddlewareHandler } from 'hono'; | ||
| import { patchAppUse } from '../shared/patchAppUse'; | ||
| import { requestHandler, responseHandler } from '../shared/middlewareHandlers'; | ||
|
|
||
| export interface HonoBunOptions extends Options<BaseTransportOptions> {} | ||
|
|
||
| /** | ||
| * Sentry middleware for Hono running in a Bun runtime environment. | ||
| */ | ||
| export const sentry = (app: Hono, options: HonoBunOptions | undefined = {}): MiddlewareHandler => { | ||
| const isDebug = options.debug; | ||
|
|
||
| isDebug && debug.log('Initialized Sentry Hono middleware (Bun)'); | ||
|
|
||
| init(options); | ||
|
|
||
| patchAppUse(app); | ||
|
|
||
| return async (context, next) => { | ||
| requestHandler(context); | ||
|
|
||
| await next(); // Handler runs in between Request above ⤴ and Response below ⤵ | ||
|
|
||
| responseHandler(context); | ||
| }; | ||
| }; |
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,24 @@ | ||
| import type { Client } from '@sentry/core'; | ||
| import { applySdkMetadata } from '@sentry/core'; | ||
| import { init as initBun } from '@sentry/bun'; | ||
| import type { HonoBunOptions } from './middleware'; | ||
| import { buildFilteredIntegrations } from '../shared/buildFilteredIntegrations'; | ||
|
|
||
| /** | ||
| * Initializes Sentry for Hono running in a Bun runtime environment. | ||
| * | ||
| * In general, it is recommended to initialize Sentry via the `sentry()` middleware, as it sets up everything by default and calls `init` internally. | ||
| * | ||
| * When manually calling `init`, add the `honoIntegration` to the `integrations` array to set up the Hono integration. | ||
| */ | ||
| export function init(options: HonoBunOptions): Client | undefined { | ||
| applySdkMetadata(options, 'hono', ['hono', 'bun']); | ||
|
|
||
| // Remove Hono from the SDK defaults to prevent double instrumentation: @sentry/bun | ||
| const filteredOptions: HonoBunOptions = { | ||
| ...options, | ||
| integrations: buildFilteredIntegrations(options.integrations, false), | ||
| }; | ||
|
|
||
| return initBun(filteredOptions); | ||
| } | ||
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,5 @@ | ||
| export { sentry } from './bun/middleware'; | ||
|
|
||
| export * from '@sentry/bun'; | ||
|
|
||
| export { init } from './bun/sdk'; |
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
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.
Uh oh!
There was an error while loading. Please reload this page.