The Problem
In long-running servers, background batching works fine—spans accumulate and flush periodically. In Lambda or a CLI script, the process may exit before the batch is sent.The Solution
Flush before exit. Call the flush method at the end of your handler or script. This sends all pending spans synchronously. In Python, useforce_flush() for serverless specifically—it ensures spans are sent even when daemon threads would be terminated.
When to Flush
| Scenario | Action |
|---|---|
| Web server (long-running) | Nothing needed—batching works |
| CLI script | Flush at the end |
| Lambda / serverless | Flush at the end of each invocation |
| Graceful shutdown | Shutdown the SDK |
Examples
- TypeScript
- Python
Laminar.flush and Laminar.shutdownPython Serverless Caveat (Why force_flush() Exists)
In Python, Laminar.flush() triggers the export, but the export itself runs in a background thread. In short-lived processes (like Lambda), the process can exit before that thread completes, and spans can be lost.
Laminar.force_flush() blocks until export completes by internally resetting the tracing pipeline, so it’s the safest option at the end of each serverless invocation.