Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions dev-packages/cloudflare-integration-tests/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ function dropUndefinedKeys<T extends Record<string, unknown>>(obj: T): T {
return obj;
}

function getSdk(): SdkInfo {
function getSdk(sdk: 'cloudflare' | 'hono'): SdkInfo {
return {
integrations: expect.any(Array),
name: 'sentry.javascript.cloudflare',
name: `sentry.javascript.${sdk}`,
packages: [
{
name: 'npm:@sentry/cloudflare',
name: `npm:@sentry/${sdk}`,
version: SDK_VERSION,
},
],
Expand All @@ -46,24 +46,27 @@ function defaultContexts(eventContexts: Contexts = {}): Contexts {
});
}

export function expectedEvent(event: Event): Event {
export function expectedEvent(event: Event, { sdk }: { sdk: 'cloudflare' | 'hono' }): Event {
return dropUndefinedKeys({
event_id: UUID_MATCHER,
timestamp: expect.any(Number),
environment: 'production',
platform: 'javascript',
sdk: getSdk(),
sdk: getSdk(sdk),
...event,
contexts: defaultContexts(event.contexts),
});
}

export function eventEnvelope(event: Event, includeSampleRand = false): Envelope {
export function eventEnvelope(
event: Event,
{ includeSampleRand = false, sdk = 'cloudflare' }: { includeSampleRand?: boolean; sdk?: 'cloudflare' | 'hono' } = {},
): Envelope {
return [
{
event_id: UUID_MATCHER,
sent_at: ISO_DATE_MATCHER,
sdk: { name: 'sentry.javascript.cloudflare', version: SDK_VERSION },
sdk: { name: `sentry.javascript.${sdk}`, version: SDK_VERSION },
trace: {
environment: event.environment || 'production',
public_key: 'public',
Expand All @@ -74,6 +77,6 @@ export function eventEnvelope(event: Event, includeSampleRand = false): Envelope
transaction: expect.any(String),
},
},
[[{ type: 'event' }, expectedEvent(event)]],
[[{ type: 'event' }, expectedEvent(event, { sdk })]],
];
}
1 change: 1 addition & 0 deletions dev-packages/cloudflare-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"@langchain/langgraph": "^1.0.1",
"@sentry/cloudflare": "10.38.0",
"@sentry/hono": "10.38.0",
"hono": "^4.11.7"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ app.get('/json', c => {
});

app.get('/error', () => {
throw new Error('Test error from Hono app');
throw new Error('Test error from Hono app (Sentry Cloudflare SDK)');
});

app.get('/hello/:name', c => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, it } from 'vitest';
import { eventEnvelope } from '../../../expect';
import { createRunner } from '../../../runner';
import { eventEnvelope } from '../../expect';
import { createRunner } from '../../runner';

it('Hono app captures errors', async ({ signal }) => {
const runner = createRunner(__dirname)
Expand All @@ -14,7 +14,7 @@ it('Hono app captures errors', async ({ signal }) => {
values: [
{
type: 'Error',
value: 'Test error from Hono app',
value: 'Test error from Hono app (Sentry Cloudflare SDK)',
stacktrace: {
frames: expect.any(Array),
},
Expand All @@ -28,7 +28,7 @@ it('Hono app captures errors', async ({ signal }) => {
url: expect.any(String),
},
},
true,
{ includeSampleRand: true },
),
)
// Second envelope: transaction event
Expand Down
38 changes: 38 additions & 0 deletions dev-packages/cloudflare-integration-tests/suites/hono-sdk/index.ts
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,
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

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
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

}),
);

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 dev-packages/cloudflare-integration-tests/suites/hono-sdk/test.ts
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();
});
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"]
}

6 changes: 6 additions & 0 deletions dev-packages/e2e-tests/verdaccio-config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ packages:
unpublish: $all
# proxy: npmjs # Don't proxy for E2E tests!

'@sentry/hono':
access: $all
publish: $all
unpublish: $all
# proxy: npmjs # Don't proxy for E2E tests!

'@sentry/nestjs':
access: $all
publish: $all
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"packages/feedback",
"packages/gatsby",
"packages/google-cloud-serverless",
"packages/hono",
"packages/integration-shims",
"packages/nestjs",
"packages/nextjs",
Expand Down
9 changes: 9 additions & 0 deletions packages/hono/.eslintrc.js
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',
},
};
21 changes: 21 additions & 0 deletions packages/hono/LICENSE
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.
58 changes: 58 additions & 0 deletions packages/hono/README.md
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)

[![npm version](https://img.shields.io/npm/v/@sentry/hono.svg)](https://www.npmjs.com/package/@sentry/hono)
[![npm dm](https://img.shields.io/npm/dm/@sentry/hono.svg)](https://www.npmjs.com/package/@sentry/hono)
[![npm dt](https://img.shields.io/npm/dt/@sentry/hono.svg)](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;
```
Loading
Loading