flush()
flush()
Manually flush pending spans to Respan
Overview
flush() forces immediate export of all pending spans to Respan without closing the tracer. The JavaScript SDK automatically drains pending spans on normal Node.js process exit, so short-lived scripts do not need a final flush() only to make their last trace appear. Use flush() when your code needs an explicit export checkpoint while the process is still running.
Signature
Basic Usage
Explicit Export Checkpoints
Periodic Flushing
Serverless Functions
Serverless runtimes can freeze or reuse the process before Node.js reaches normal process exit. Flush before returning when the trace must be exported within the invocation window.
Express Middleware
Testing
With shutdown()
For complete cleanup, use shutdown() instead of flush(). shutdown() flushes and then closes the tracer.
Best Practices
- Do not add a final
flush()to short-lived Node.js scripts only for normal process exit; the SDK drains pending spans automatically - Use
flush()for explicit checkpoints, tests that read exported traces before exit, critical operations, and serverless invocations that may freeze before process exit - Use
shutdown()when you want complete cleanup and to close the tracer - Flush is automatically called periodically when batching is enabled
- Awaiting
flush()waits for export to complete - In long-running processes, prefer
shutdown()overflush()when the process is done tracing