Skip to content

Commit

Permalink
demo some shutdown code
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas committed Jan 29, 2025
1 parent 8a4230c commit 2f8756e
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions examples/metrics-basic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
async fn main() {
// Initialize the MeterProvider with the stdout Exporter.
let meter_provider = init_meter_provider();

Expand Down Expand Up @@ -140,6 +140,32 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// Metrics are exported by default every 30 seconds when using stdout exporter,
// however shutting down the MeterProvider here instantly flushes
// the metrics, instead of waiting for the 30 sec interval.
meter_provider.shutdown()?;
Ok(())
let shutdown_result = meter_provider.shutdown();

// Demonstrate handling the shutdown result.
match shutdown_result {
Ok(_) => println!("MeterProvider shutdown successfully"),
Err(e) => {
match e {
opentelemetry_sdk::error::ShutdownError::Failed(e) => {
// This indicates some failure during shutdown.
// Not much to do here, but log the error.
// So users at least know something went wrong,
// and possibly explain why some metrics were not exported.
println!("MeterProvider shutdown failed: {}", e)
}
opentelemetry_sdk::error::ShutdownError::AlreadyShutdown => {
// This indicates some user code tried to shutdown elsewhere.
// user need to review their code to ensure shutdown is called only once.
println!("MeterProvider already shutdown")
}
opentelemetry_sdk::error::ShutdownError::Timeout(e) => {
// This indicates the shutdown timed out, and a good
// hint to user to increase the timeout or even retry.
// (Shutdown method does not allow custom timeout today, but that is temporary)
println!("MeterProvider shutdown timed out after {:?}", e)
}
}
}
}
}

0 comments on commit 2f8756e

Please sign in to comment.