Cloudflare Worker SDK API Reference
Cloudflare Worker SDK
Highlight's Cloudflare Worker SDK lets you track your errors and responses in Cloudflare Workers with no impact on performance..
Just getting started?
Check out our getting started guide to get up and running quickly.
H.init
H.init() configures the highlight SDK and records console log methods. The session/trace context is inferred based on the incoming network request headers.
Method Parameters
import { H } from "@highlight-run/cloudflare";
export default {
async fetch(request: Request, env: {}, ctx: ExecutionContext) {
H.init(request, { HIGHLIGHT_PROJECT_ID: '<YOUR_PROJECT_ID>' }, ctx)
// do something...
console.log('hi!', {hello: 'world'})
return new Response('hello!')
},
}
H.consumeError
H.consumeError() reports an error and its corresponding stack trace to Highlight. The session is inferred based on the incoming network request headers.
Method Parameters
import { H } from "@highlight-run/cloudflare";
export default {
async fetch(request: Request, env: {}, ctx: ExecutionContext) {
H.init(request, { HIGHLIGHT_PROJECT_ID: '<YOUR_PROJECT_ID>' }, ctx)
try {
// do something...
return new Response('hello!')
} catch (e: any) {
H.consumeError(request, { HIGHLIGHT_PROJECT_ID: '<YOUR_PROJECT_ID>' }, ctx, e)
throw e
}
},
}
H.runWithHeaders
H.runWithHeaders() traces a response from your backend. This allows tracking incoming and outgoing headers and bodies and automatically propagating the trace context to child spans.
Method Parameters
import { H } from '@highlight-run/cloudflare'
async function doRequest() {
const someHost = ',[object Object],'
const url = someHost + '/index.js'
async function gatherResponse(response: any) {
H.setAttributes({ foo: 'bar', random: Math.random() })
console.log('yo! gathering a cloudflare worker response', {
another: Math.random(),
bar: 'bar',
})
console.warn('warning! gathering a cloudflare worker response', {
bar: 'warning',
})
console.warn('error! gathering a cloudflare worker response', {
another: Math.random(),
})
if (Math.random() < 0.2) {
throw new Error('random error from cloudflare worker!')
}
const { headers } = response
const contentType = headers.get('content-type') || ''
if (contentType.includes('application/json')) {
return JSON.stringify(await response.json())
} else if (contentType.includes('application/text')) {
return response.text()
} else if (contentType.includes('text/html')) {
return response.text()
} else {
return response.text()
}
}
const init = {
headers: {
'content-type': 'text/html;charset=UTF-8',
},
}
const response = await fetch(url, init)
const results = await gatherResponse(response)
return new Response(results, init)
}
export default {
async fetch(request: Request, env: {}, ctx: ExecutionContext) {
H.init({ HIGHLIGHT_PROJECT_ID: '1' }, 'e2e-cloudflare-app')
try {
return await H.runWithHeaders('worker', request.headers, doRequest)
} catch (e: any) {
H.consumeError(e)
throw e
}
},
}
H.setAttributes
H.setAttributes() attached structured log attributes to all subsequent console methods. Repeat calls with the same key update the value.
Method Parameters
import { H } from "@highlight-run/cloudflare";
export default {
async fetch(request: Request, env: {}, ctx: ExecutionContext) {
H.init({ HIGHLIGHT_PROJECT_ID: '1' }, 'example-cloudflare-service')
// do something...
console.log('hi!', {hello: 'world'})
H.setAttributes({my: 'attribute', is: Math.random()})
console.warn('whoa')
return new Response('hello!')
},
}