Skip to main content

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

  1. Flushes all pending spans - Sends any buffered spans to Keywords AI
  2. Closes the tracer - Gracefully closes the OpenTelemetry tracer
  3. Cleans up resources - Releases any held resources

shutdown() vs flush()

Best Practices

  • Always call shutdown() before application exit
  • Use in finally blocks 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() over shutdown() if the environment is reused