1+ use super :: IdGenerator ;
12use crate :: error:: { OTelSdkError , OTelSdkResult } ;
23/// # Trace Provider SDK
34///
@@ -74,8 +75,7 @@ use opentelemetry::{otel_info, InstrumentationScope};
7475use std:: borrow:: Cow ;
7576use std:: sync:: atomic:: { AtomicBool , Ordering } ;
7677use std:: sync:: { Arc , OnceLock } ;
77-
78- use super :: IdGenerator ;
78+ use std:: time:: Duration ;
7979
8080static PROVIDER_RESOURCE : OnceLock < Resource > = OnceLock :: new ( ) ;
8181
@@ -112,10 +112,10 @@ pub(crate) struct TracerProviderInner {
112112impl TracerProviderInner {
113113 /// Crate-private shutdown method to be called both from explicit shutdown
114114 /// and from Drop when the last reference is released.
115- pub ( crate ) fn shutdown ( & self ) -> Vec < OTelSdkResult > {
115+ pub ( crate ) fn shutdown_with_timeout ( & self , timeout : Duration ) -> Vec < OTelSdkResult > {
116116 let mut results = vec ! [ ] ;
117117 for processor in & self . processors {
118- let result = processor. shutdown ( ) ;
118+ let result = processor. shutdown_with_timeout ( timeout ) ;
119119 if let Err ( err) = & result {
120120 // Log at debug level because:
121121 // - The error is also returned to the user for handling (if applicable)
@@ -128,6 +128,10 @@ impl TracerProviderInner {
128128 }
129129 results
130130 }
131+ /// shutdown with default timeout
132+ pub ( crate ) fn shutdown ( & self ) -> Vec < OTelSdkResult > {
133+ self . shutdown_with_timeout ( Duration :: from_secs ( 5 ) )
134+ }
131135}
132136
133137impl Drop for TracerProviderInner {
@@ -239,15 +243,15 @@ impl SdkTracerProvider {
239243 /// Shuts down the current `TracerProvider`.
240244 ///
241245 /// Note that shut down doesn't means the TracerProvider has dropped
242- pub fn shutdown ( & self ) -> OTelSdkResult {
246+ pub fn shutdown_with_timeout ( & self , timeout : Duration ) -> OTelSdkResult {
243247 if self
244248 . inner
245249 . is_shutdown
246250 . compare_exchange ( false , true , Ordering :: SeqCst , Ordering :: SeqCst )
247251 . is_ok ( )
248252 {
249253 // propagate the shutdown signal to processors
250- let results = self . inner . shutdown ( ) ;
254+ let results = self . inner . shutdown_with_timeout ( timeout ) ;
251255
252256 if results. iter ( ) . all ( |res| res. is_ok ( ) ) {
253257 Ok ( ( ) )
@@ -264,6 +268,11 @@ impl SdkTracerProvider {
264268 Err ( OTelSdkError :: AlreadyShutdown )
265269 }
266270 }
271+
272+ /// shutdown with default timeout
273+ pub fn shutdown ( & self ) -> OTelSdkResult {
274+ self . shutdown_with_timeout ( Duration :: from_secs ( 5 ) )
275+ }
267276}
268277
269278impl opentelemetry:: trace:: TracerProvider for SdkTracerProvider {
@@ -471,6 +480,7 @@ mod tests {
471480 use std:: env;
472481 use std:: sync:: atomic:: { AtomicBool , AtomicU32 , Ordering } ;
473482 use std:: sync:: Arc ;
483+ use std:: time:: Duration ;
474484
475485 // fields below is wrapped with Arc so we can assert it
476486 #[ derive( Default , Debug ) ]
@@ -528,7 +538,7 @@ mod tests {
528538 }
529539 }
530540
531- fn shutdown ( & self ) -> OTelSdkResult {
541+ fn shutdown_with_timeout ( & self , _timeout : Duration ) -> OTelSdkResult {
532542 if self . assert_info . 0 . is_shutdown . load ( Ordering :: SeqCst ) {
533543 Ok ( ( ) )
534544 } else {
@@ -787,7 +797,7 @@ mod tests {
787797 Ok ( ( ) )
788798 }
789799
790- fn shutdown ( & self ) -> OTelSdkResult {
800+ fn shutdown_with_timeout ( & self , _timeout : Duration ) -> OTelSdkResult {
791801 self . shutdown_count . fetch_add ( 1 , Ordering :: SeqCst ) ;
792802 Ok ( ( ) )
793803 }
0 commit comments