Cloudflare Workers
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,
},
});
Install the @highlight-run/cloudflare package with your package manager.
# with yarn
yarn add @highlight-run/cloudflare
# with pnpm
pnpm add @highlight-run/cloudflare
# with npm
npm install @highlight-run/cloudflare
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) {
const hEnv = { HIGHLIGHT_PROJECT_ID: 'YOUR_PROJECT_ID' }
try {
const response = await doRequest()
H.sendResponse(request, hEnv, ctx, response)
return response
} catch (e: any) {
H.consumeError(request, hEnv, ctx, e)
throw e
}
},
}
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.consumeError(request, { HIGHLIGHT_PROJECT_ID: 'YOUR_PROJECT_ID' }, ctx, new Error('example error!'))
},
}
With the JS SDKs, your logs are reported automatically from console methods. Visit the highlight logs portal and check that backend logs are coming in.