shutdown()
shutdown()
Gracefully shutdown the tracing client and flush all pending spans
Overview
shutdown() flushes pending spans and gracefully closes the tracer. Use it when your application is intentionally stopping tracing, such as in long-running servers, workers, signal handlers, or test teardown. Short-lived Node.js scripts do not need shutdown() solely to export their final spans because the SDK drains pending spans on normal process exit.
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()
Best Practices
- Use
shutdown()for long-running applications, workers, explicit signal handling, and test teardown - Use in
finallyblocks when your code owns a terminal tracing lifecycle - 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