Star us on GitHub
Star
Menu

Cloudflare Workers Error Monitoring

Learn how to set up highlight.io in Cloudflare Workers.
1
Add `tracingOrigins` to your client Highlight snippet.

This backend SDK requires one of the Highlight frontend SDKs to be installed, so please make sure you've followed the fullstack mapping guide first.

H.init("<YOUR_PROJECT_ID>", { tracingOrigins: ['localhost', 'example.myapp.com/backend'], networkRecording: { enabled: true, recordHeadersAndBody: true, }, });
Copy
2
Install the relevant Highlight SDK(s).

Install @highlight-run/cloudflare with your package manager.

npm install --save @highlight-run/cloudflare
Copy
3
Add the Cloudflare Worker Highlight integration.

Use the Cloudflare Worker SDK in your response handler. The sendResponse method traces successful requests while consumeError reports exceptions. All Highlight data submission uses waitUntil to make sure that we have no impact on request handling performance.

import { H } from '@highlight-run/cloudflare' async function doRequest() { return new Response('hello!') } export default { async fetch(request: Request, env: {}, ctx: ExecutionContext) { H.init(request, { HIGHLIGHT_PROJECT_ID: '<YOUR_PROJECT_ID>' }, ctx) console.log('starting some work...') try { const response = await doRequest() H.sendResponse(response) return response } catch (e: any) { H.consumeError(e) throw e } }, }
Copy
4
Verify that your SDK is reporting errors.

You'll want to throw an exception in one of your cloudflare handlers. Access the API handler and make sure the error shows up in Highlight.

export default { async fetch(request: Request, env: {}, ctx: ExecutionContext) { H.init(request, { HIGHLIGHT_PROJECT_ID: '<YOUR_PROJECT_ID>' }, ctx) H.consumeError(new Error('example error!')) }, }
Copy
5
Having TypeScript issues?

Using our library requires setting skipLibCheck because of one of our dependencies that references node types. At runtime, this does not cause issues because of dynamic imports and other polyfilling done to ensure the sdk works in the cloud flare worker runtime, but the types are still referenced.

{ /* ... your other options ... */ "compilerOptions": { /* required due to our sdk's usage of 'opentelemetry-sdk-workers' which works around node syntax in its dependencies by dynamically replacing the imported javascript bundle, but does not replace the '@types/node' dependency */ "skipLibCheck": true, "types": ["@cloudflare/workers-types"] }, }
Copy
6
Set up logging.

With the JS SDKs, your logs are reported automatically from console methods. See the JS logging setup guide for more details.