Cloudflare Workers & Edge Deployment
Vestig works fully on Edge runtimes with zero configuration changes. Logging, tracing, OTLP export, PII sanitization, wide events, and sampling all work out of the box on Cloudflare Workers, Vercel Edge Runtime, and Deno Deploy.
Overview
Edge runtimes run JavaScript at the network edge, close to your users. Unlike traditional Node.js servers, they use web-standard APIs and have shorter execution time limits. Vestig is built entirely on these web standards — no Node.js-specific dependencies like http, net, or fs — so everything works natively on Edge.
What works on Edge runtimes:
- Logging — Structured logging with all log levels
- Tracing — Span creation, nesting, and attribute tracking
- OTLP Export — Send traces to Honeycomb, Grafana, Jaeger, or any OTLP backend
- PII Sanitization — All sanitization presets (GDPR, HIPAA, PCI-DSS)
- Wide Events — Rich event aggregation across request lifecycles
- Sampling — Head and tail sampling strategies
- W3C Trace Context — Full
traceparentandtracestatepropagation
Runtime Detection
Vestig automatically detects Edge runtimes through its runtime.ts module. The detection logic checks for platform-specific globals:
Once detected, you can use the convenience flags anywhere in your code:
These flags are resolved once at startup and remain constant for the lifetime of the isolate.
OTLP on Edge
The OTLPExporter uses native fetch() under the hood, making it fully compatible with Edge runtimes. There are no Node.js APIs involved — no http module, no net sockets, no filesystem access. Pure web standards.
Edge runtimes have shorter execution time limits than traditional servers, so you should tune the exporter for smaller batches, more frequent flushes, and shorter timeouts:
These settings keep the exporter well within the typical 30-second CPU time limit of Cloudflare Workers while ensuring spans are reliably delivered.
Context Management
Vestig adapts its context management strategy based on the runtime:
- Node.js / Bun / Deno — Uses
AsyncLocalStoragefor proper request isolation across async boundaries - Edge runtimes (Cloudflare Workers) — Falls back to
GlobalContextManager, a stack-based global context
The GlobalContextManager works correctly for sequential operations within a single request. Since Cloudflare Workers handle one request at a time per isolate, this is safe for most deployments. Concurrent requests in the same isolate can potentially share context, but this is rare in practice.
The API is identical regardless of runtime:
Basic Cloudflare Worker Example
A complete Cloudflare Worker with logging, tracing, and OTLP export:
Important: Always use ctx.waitUntil() to flush spans. Cloudflare Workers terminate immediately after the response is sent. Without waitUntil, pending spans would be lost. The ExecutionContext.waitUntil() method extends the Worker's lifetime to complete background tasks like flushing telemetry.
Next.js Edge Routes
Vestig works seamlessly in Next.js Edge API routes. Add export const runtime = 'edge' to opt into Edge execution:
Vestig detects the Edge runtime automatically via the NEXT_RUNTIME environment variable and adjusts its behavior accordingly.
W3C Trace Context Propagation
For cross-service correlation, parse incoming traceparent headers and propagate them to downstream services:
This ensures a single trace ID follows a request from the Edge Worker through any number of downstream services, giving you a complete picture in your tracing backend.
Feature Compatibility Matrix
| Feature | CF Workers | Vercel Edge | Node.js |
|---|---|---|---|
| Logging | ✅ | ✅ | ✅ |
| Structured Output | ✅ | ✅ | ✅ |
| PII Sanitization | ✅ | ✅ | ✅ |
| Tracing (spans) | ✅ | ✅ | ✅ |
| OTLP Export | ✅ | ✅ | ✅ |
| Wide Events | ✅ | ✅ | ✅ |
| Context (AsyncLocalStorage) | ❌ Global fallback | ❌ Global fallback | ✅ |
| File Transport | ❌ | ❌ | ✅ |
| Console Capture | ✅ | ✅ | ✅ |
| W3C Trace Context | ✅ | ✅ | ✅ |
Best Practices for Edge Deployments
- Flush before termination — Always use
ctx.waitUntil(exporter.forceFlush())in Cloudflare Workers to ensure spans are exported before the isolate shuts down - Smaller batches — Use
batchSize: 25andflushInterval: 1000to stay within execution time limits - Shorter timeouts — Edge functions have strict CPU time budgets; use
timeout: 5000to avoid hitting them - Fewer retries — Set
maxRetries: 1to prevent retry loops from exhausting execution time - Log sparingly — Edge runtimes have no persistent storage; rely on OTLP export for observability rather than high-volume console logging
- Trace context — Propagate
traceparentheaders to downstream services for end-to-end distributed tracing
Related
- OTLP Export — Configure OTLP backends
- Tracing Overview — Span creation and management
- Runtime Detection — How Vestig detects environments
- Context Propagation — Request correlation
- W3C Trace Context — traceparent and tracestate