Overview
shutdown() flushes all pending spans and gracefully shuts down the tracer. Always call this before your application exits to ensure no data is lost.
Signature
Basic Usage
Complete Application Lifecycle
Graceful Shutdown on Signals
Express Application
Worker/Job Processing
CLI Application
Testing
What shutdown() Does
- Flushes all pending spans - Sends any buffered spans to Respan
- Closes the tracer - Gracefully closes the OpenTelemetry tracer
- Cleans up resources - Releases any held resources
shutdown() vs flush()
| Method | Purpose |
|---|---|
shutdown() | Complete cleanup, sends all data, closes tracer (use at app exit) |
flush() | Sends pending data but keeps tracer active (use for periodic flushing) |
Best Practices
- Always call
shutdown()before application exit - Use in
finallyblocks to ensure it runs even on errors - Handle SIGTERM and SIGINT signals for graceful shutdown
- Set a timeout for forced shutdown if graceful shutdown takes too long
- In testing, shutdown after all tests complete
- Never reuse the tracer after calling
shutdown() - For serverless, you might prefer
flush()overshutdown()if the environment is reused