Patch Changes
-
#810
efcfe52Thanks @omeraplak! - fix: ensure reliable trace export and context propagation in serverless environmentsThe Problem
Trigger-initiated agent executions in serverless environments (Cloudflare Workers, Vercel Edge Functions) were experiencing inconsistent trace exports and missing parent-child span relationships. This manifested as:
- Agent traces not appearing in observability tools despite successful execution
- Trigger and agent spans appearing as separate, disconnected traces instead of a single coherent trace tree
- Spans being lost due to serverless functions terminating before export completion
The Solution
Serverless Trace Export (
@voltagent/serverless-hono):- Implemented reliable span flushing using Cloudflare's
waitUntilAPI to ensure spans are exported before function termination - Switched from
SimpleSpanProcessortoBatchSpanProcessorwith serverless-optimized configuration (immediate export, small batch sizes) - Added automatic flush on trigger completion with graceful fallback to
forceFlushwhenwaitUntilis unavailable
Context Propagation (
@voltagent/core):- Integrated official
@opentelemetry/context-async-hookspackage to replace custom context manager implementation - Ensured
AsyncHooksContextManageris registered in both Node.js and serverless environments for consistent async context tracking - Fixed
resolveParentSpanlogic to correctly identify scorer spans while avoiding framework-generated ambient spans - Exported
propagationandROOT_CONTEXTfrom@opentelemetry/apifor HTTP header-based trace context injection/extraction
Node.js Reliability:
- Updated
NodeVoltAgentObservability.flushOnFinish()to callforceFlush()instead of being a no-op, ensuring spans are exported in short-lived processes
Impact
- ✅ Serverless traces are now reliably exported and visible in observability tools
- ✅ Trigger and agent spans form a single, coherent trace tree with proper parent-child relationships
- ✅ Consistent tracing behavior across Node.js and serverless runtimes
- ✅ No more missing or orphaned spans in Cloudflare Workers, Vercel Edge Functions, or similar platforms
Technical Details
- Uses
BatchSpanProcessorwithmaxExportBatchSize: 32andscheduledDelayMillis: 100for serverless - Leverages
globalThis.___voltagent_wait_untilfor non-blocking span export in Cloudflare Workers - Implements
AsyncHooksContextManagerfor robust async context tracking acrossPromisechains andasync/await - Maintains backward compatibility with existing Node.js deployments
Migration Notes
No breaking changes. Existing deployments will automatically benefit from improved trace reliability. Ensure your
wrangler.tomlincludesnodejs_compatflag for Cloudflare Workers:compatibility_flags = ["nodejs_compat"]