Hono on Cloudflare
Learn how to add Cloudflare instrumentation to your Hono app.
This guide explains how to set up Sentry in your Hono application running on Cloudflare Workers.
npm install @sentry/cloudflare --save
The main Sentry configuration should happen as early as possible in your app's lifecycle.
Since the SDK needs access to the AsyncLocalStorage
API, you need to set either the nodejs_compat
or nodejs_als
compatibility flags in your wrangler.(jsonc|toml)
configuration file:
wrangler.jsonc
{
"compatibility_flags": [
"nodejs_als",
// "nodejs_compat"
],
}
Additionally, add the CF_VERSION_METADATA
binding in the same file:
wrangler.jsonc
{
// ...
"version_metadata": {
"binding": "CF_VERSION_METADATA",
},
}
Wrap your worker handler with the withSentry
function, for example, in your index.ts
file, to initialize the Sentry SDK and hook into the environment:
index.ts
import { Hono, HTTPException } from "hono";
import * as Sentry from "@sentry/cloudflare";
export default Sentry.withSentry(
(env: Env) => {
const { id: versionId } = env.CF_VERSION_METADATA;
return {
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
release: versionId,
// Adds request headers and IP for users, for more info visit:
// https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/options/#sendDefaultPii
sendDefaultPii: true,
// logs
// Enable logs to be sent to Sentry
enableLogs: true,
// logs
// performance
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
// Learn more at
https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/options/#tracesSampleRate
tracesSampleRate: 1.0,
// performance
};
},
// your existing worker export
app
);
instrument.js
const Sentry = require("@sentry/node");
// profiling
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
// profiling
// Ensure to call this before requiring any other modules!
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
// Adds request headers and IP for users, for more info visit:
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#sendDefaultPii
sendDefaultPii: true,
// profiling
integrations: [
// Add our Profiling integration
nodeProfilingIntegration(),
],
// profiling
// performance
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for tracing.
// We recommend adjusting this value in production
// Learn more at
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#tracesSampleRate
tracesSampleRate: 1.0,
// performance
// profiling
// Set profilesSampleRate to 1.0 to profile 100%
// of sampled transactions.
// This is relative to tracesSampleRate
// Learn more at
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#profilesSampleRate
profilesSampleRate: 1.0,
// profiling
// logs
// Enable logs to be sent to Sentry
enableLogs: true,
// logs
});
First, let's verify that Sentry captures errors and creates issues in your Sentry project. Add the following code snippet to your main application file, adding a route that triggers an error that Sentry will capture:
app.get("/debug-sentry", () => {
throw new Error("My first Sentry error!");
});
To test your tracing configuration, update the previous code snippet by starting a trace to measure the time it takes for the execution of your code:
app.get("/debug-sentry", async () => {
await Sentry.startSpan(
{
op: "test",
name: "My First Test Transaction",
},
async () => {
await new Promise((resolve) => setTimeout(resolve, 100)); // Wait for 100ms
throw new Error("My first Sentry error!");
},
);
});
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").