11use opentelemetry:: { global, KeyValue } ;
2+ use opentelemetry_sdk:: error:: ShutdownError ;
23use opentelemetry_sdk:: metrics:: { PeriodicReader , SdkMeterProvider } ;
34use opentelemetry_sdk:: Resource ;
45use std:: error:: Error ;
@@ -23,7 +24,7 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
2324}
2425
2526#[ tokio:: main]
26- async fn main ( ) {
27+ async fn main ( ) -> Result < ( ) , Box < dyn Error > > {
2728 // Initialize the MeterProvider with the stdout Exporter.
2829 let meter_provider = init_meter_provider ( ) ;
2930
@@ -137,35 +138,41 @@ async fn main() {
137138 } )
138139 . build ( ) ;
139140
140- // Metrics are exported by default every 30 seconds when using stdout exporter,
141- // however shutting down the MeterProvider here instantly flushes
142- // the metrics, instead of waiting for the 30 sec interval.
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 :: InternalFailure ( 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- }
141+ // Metrics are exported by default every 30 seconds when using stdout
142+ // exporter, however shutting down the MeterProvider here instantly flushes
143+ // the metrics, instead of waiting for the 30 sec interval. Shutdown returns
144+ // a result, which is bubbled up to the caller The commented code below
145+ // demonstrates handling the shutdown result, instead of bubbling up the
146+ // error.
147+ meter_provider. shutdown ( ) ?;
148+
149+ // let shutdown_result = meter_provider.shutdown();
150+
151+ // Handle the shutdown result.
152+ // match shutdown_result {
153+ // Ok(_) => println!("MeterProvider shutdown successfully"),
154+ // Err(e) => {
155+ // match e {
156+ // opentelemetry_sdk::error::ShutdownError::InternalFailure(message) => {
157+ // // This indicates some internal failure during shutdown. The
158+ // // error message is intended for logging purposes only and
159+ // // should not be used to make programmatic decisions.
160+ // println!("MeterProvider shutdown failed: {}", message)
161+ // }
162+ // opentelemetry_sdk::error::ShutdownError::AlreadyShutdown => {
163+ // // This indicates some user code tried to shutdown
164+ // // elsewhere. user need to review their code to ensure
165+ // // shutdown is called only once.
166+ // println!("MeterProvider already shutdown")
167+ // }
168+ // opentelemetry_sdk::error::ShutdownError::Timeout(e) => {
169+ // // This indicates the shutdown timed out, and a good hint to
170+ // // user to increase the timeout. (Shutdown method does not
171+ // // allow custom timeout today, but that is temporary)
172+ // println!("MeterProvider shutdown timed out after {:?}", e)
173+ // }
174+ // }
175+ // }
176+ // }
177+ Ok ( ( ) )
171178}
0 commit comments