Skip to content

Commit 2f8756e

Browse files
committed
demo some shutdown code
1 parent 8a4230c commit 2f8756e

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

examples/metrics-basic/src/main.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
2323
}
2424

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

@@ -140,6 +140,32 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
140140
// Metrics are exported by default every 30 seconds when using stdout exporter,
141141
// however shutting down the MeterProvider here instantly flushes
142142
// the metrics, instead of waiting for the 30 sec interval.
143-
meter_provider.shutdown()?;
144-
Ok(())
143+
let shutdown_result = meter_provider.shutdown();
144+
145+
// Demonstrate handling the shutdown result.
146+
match shutdown_result {
147+
Ok(_) => println!("MeterProvider shutdown successfully"),
148+
Err(e) => {
149+
match e {
150+
opentelemetry_sdk::error::ShutdownError::Failed(e) => {
151+
// This indicates some failure during shutdown.
152+
// Not much to do here, but log the error.
153+
// So users at least know something went wrong,
154+
// and possibly explain why some metrics were not exported.
155+
println!("MeterProvider shutdown failed: {}", e)
156+
}
157+
opentelemetry_sdk::error::ShutdownError::AlreadyShutdown => {
158+
// This indicates some user code tried to shutdown elsewhere.
159+
// user need to review their code to ensure shutdown is called only once.
160+
println!("MeterProvider already shutdown")
161+
}
162+
opentelemetry_sdk::error::ShutdownError::Timeout(e) => {
163+
// This indicates the shutdown timed out, and a good
164+
// hint to user to increase the timeout or even retry.
165+
// (Shutdown method does not allow custom timeout today, but that is temporary)
166+
println!("MeterProvider shutdown timed out after {:?}", e)
167+
}
168+
}
169+
}
170+
}
145171
}

0 commit comments

Comments
 (0)