Cloudflare Workers Package
@pydantic/logfire-cf-workers instruments Cloudflare Workers and exports the manual logfire API through the core logfire package.
npm install @pydantic/logfire-cf-workers logfire
Enable Node.js compatibility in Wrangler:
{
"compatibility_flags": ["nodejs_compat"]
}
For wrangler.toml, use:
compatibility_flags = ["nodejs_compat"]
For local development, set a write token in .dev.vars:
LOGFIRE_TOKEN=your-write-token
LOGFIRE_ENVIRONMENT=development
LOGFIRE_ENVIRONMENT is optional and names the environment on emitted telemetry. For production, store LOGFIRE_TOKEN as a Worker secret:
npx wrangler secret put LOGFIRE_TOKEN
import * as logfire from 'logfire'
import { instrument } from '@pydantic/logfire-cf-workers'
const handler = {
async fetch(): Promise<Response> {
logfire.info('worker request handled')
return new Response('hello from Logfire')
},
} satisfies ExportedHandler
export default instrument(handler, {
service: {
name: 'checkout-worker',
namespace: '',
version: '1.0.0',
},
})
instrument() is an alias for instrumentInProcess(). It reads LOGFIRE_TOKEN, LOGFIRE_ENVIRONMENT, and LOGFIRE_BASE_URL from the Worker environment.
This instrument(handler, config) function is the Cloudflare Worker runtime
entrypoint. If you also want to wrap an individual business function in a
manual span, import the core wrapper from logfire directly:
import { instrument as instrumentFunction } from 'logfire'
import { instrument as instrumentWorker } from '@pydantic/logfire-cf-workers'
Use instrumentDO() for Durable Object classes. It uses the same Logfire
configuration path as instrument(), including LOGFIRE_TOKEN,
LOGFIRE_ENVIRONMENT, LOGFIRE_BASE_URL, and scrubbing configuration.
Wrap every Durable Object class that calls Logfire. Each Durable Object runs in
its own invocation, so the Worker’s instrument() call does not cover it. Spans
started in an unwrapped class are dropped, and Logfire logs a warning once.
import { DurableObject } from 'cloudflare:workers'
import * as logfire from 'logfire'
import { instrument, instrumentDO } from '@pydantic/logfire-cf-workers'
interface Env {
COUNTER: DurableObjectNamespace<CounterDurableObject>
}
class CounterDurableObject extends DurableObject<Env> {
async increment(): Promise<number> {
const value = ((await this.ctx.storage.get<number>('value')) ?? 0) + 1
await this.ctx.storage.put('value', value)
logfire.info('counter incremented to {value}', { value })
return value
}
}
// Export the wrapped class under the `class_name` from your Wrangler configuration.
export const Counter = instrumentDO(CounterDurableObject, {
service: {
name: 'counter-do',
namespace: '',
version: '1.0.0',
},
})
export default instrument(
{
async fetch(request, env): Promise<Response> {
const value = await env.COUNTER.getByName('global').increment()
return Response.json({ value })
},
} satisfies ExportedHandler<Env>,
{
service: {
name: 'checkout-worker',
namespace: '',
version: '1.0.0',
},
}
)
instrumentDO() creates a span for fetch, alarm, and each RPC method or
WebSocket handler call, with child spans for ctx.storage operations. Classes
that implement the DurableObject interface and receive state in the
constructor work the same way.
- RPC calls do not carry trace context, so each RPC method call starts a new
trace.
fetchcalls through a stub fromget()orgetByName()continue the caller’s trace. - In a class that extends
DurableObject, the constructor receives the runtime’s ownctxandenv; the instrumented versions replace them after the constructor returns. Storage calls made in the constructor, and references it keeps such asthis.storage = ctx.storage, are not traced. Readthis.ctx.storagein your methods instead. - Methods run with the uninstrumented object as
this, so a method that callsthis.fetch()orthis.alarm()records that work inside its own span instead of a separatefetchoralarmspan.
The package also supports Cloudflare Tail Worker flows:
- use
instrumentTail()in the producer Worker - use
exportTailEventsToLogfire()in the Tail Worker
See examples/cf-producer-worker and examples/cf-tail-worker in this repository for the full wiring.
Cloudflare Workers do not have a process-style shutdown hook. Logfire relies on
the Worker request lifetime instead: each instrumented handler schedules span
export with ctx.waitUntil() after the handler finishes.
This applies to both Logfire entrypoints:
instrumentInProcess()and itsinstrument()alias configure in-process OTLP export.instrumentTail()configures tail Worker export.
Both entrypoints use this repository’s Cloudflare Worker OpenTelemetry
instrumentation internally. The instrumentation proxies the Worker execution
context, tracks promises passed to ctx.waitUntil() inside the user handler,
then schedules span export on the original context with ctx.waitUntil().
Export waits for a scheduler tick and the tracked promises before force-flushing
the configured span processors.
Because export is tied to each Worker event, there is no long-lived Logfire
runtime to shut down after deployment. Use ctx.waitUntil() for any asynchronous
work that should be included in request-lifetime telemetry.
If you test Workers with Vitest, ensure the Workers test pool can optimize the package for module loading:
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'
export default defineWorkersConfig({
test: {
deps: {
optimizer: {
ssr: {
enabled: true,
include: ['@pydantic/logfire-cf-workers'],
},
},
},
poolOptions: {
workers: {
// Your Workers test configuration.
},
},
},
})
Pass scrubbing to instrument, instrumentDO, or instrumentTail to control sensitive data scrubbing before telemetry is sent:
export default instrument(handler, {
scrubbing: {
extraPatterns: ['secret_token'],
},
service: {
name: 'checkout-worker',
namespace: '',
version: '1.0.0',
},
})