@@ -68,32 +68,38 @@ use crate::trace::{
6868} ;
6969use crate :: Resource ;
7070use crate :: { export:: trace:: SpanExporter , trace:: SpanProcessor } ;
71- use once_cell:: sync:: { Lazy , OnceCell } ;
7271use opentelemetry:: trace:: TraceError ;
7372use opentelemetry:: InstrumentationScope ;
7473use opentelemetry:: { otel_debug, trace:: TraceResult } ;
7574use std:: borrow:: Cow ;
7675use std:: sync:: atomic:: { AtomicBool , Ordering } ;
77- use std:: sync:: Arc ;
76+ use std:: sync:: { Arc , OnceLock } ;
7877
7978use super :: IdGenerator ;
8079
81- static PROVIDER_RESOURCE : OnceCell < Resource > = OnceCell :: new ( ) ;
80+ static PROVIDER_RESOURCE : OnceLock < Resource > = OnceLock :: new ( ) ;
8281
8382// a no nop tracer provider used as placeholder when the provider is shutdown
84- static NOOP_TRACER_PROVIDER : Lazy < TracerProvider > = Lazy :: new ( || TracerProvider {
85- inner : Arc :: new ( TracerProviderInner {
86- processors : Vec :: new ( ) ,
87- config : Config {
88- // cannot use default here as the default resource is not empty
89- sampler : Box :: new ( Sampler :: ParentBased ( Box :: new ( Sampler :: AlwaysOn ) ) ) ,
90- id_generator : Box :: < RandomIdGenerator > :: default ( ) ,
91- span_limits : SpanLimits :: default ( ) ,
92- resource : Cow :: Owned ( Resource :: empty ( ) ) ,
93- } ,
94- is_shutdown : AtomicBool :: new ( true ) ,
95- } ) ,
96- } ) ;
83+ // TODO Replace with LazyLock once it is stable
84+ static NOOP_TRACER_PROVIDER : OnceLock < TracerProvider > = OnceLock :: new ( ) ;
85+ #[ inline]
86+ fn noop_tracer_provider ( ) -> & ' static TracerProvider {
87+ NOOP_TRACER_PROVIDER . get_or_init ( || {
88+ TracerProvider {
89+ inner : Arc :: new ( TracerProviderInner {
90+ processors : Vec :: new ( ) ,
91+ config : Config {
92+ // cannot use default here as the default resource is not empty
93+ sampler : Box :: new ( Sampler :: ParentBased ( Box :: new ( Sampler :: AlwaysOn ) ) ) ,
94+ id_generator : Box :: < RandomIdGenerator > :: default ( ) ,
95+ span_limits : SpanLimits :: default ( ) ,
96+ resource : Cow :: Owned ( Resource :: empty ( ) ) ,
97+ } ,
98+ is_shutdown : AtomicBool :: new ( true ) ,
99+ } ) ,
100+ }
101+ } )
102+ }
97103
98104/// TracerProvider inner type
99105#[ derive( Debug ) ]
@@ -269,7 +275,7 @@ impl opentelemetry::trace::TracerProvider for TracerProvider {
269275
270276 fn tracer_with_scope ( & self , scope : InstrumentationScope ) -> Self :: Tracer {
271277 if self . inner . is_shutdown . load ( Ordering :: Relaxed ) {
272- return Tracer :: new ( scope, NOOP_TRACER_PROVIDER . clone ( ) ) ;
278+ return Tracer :: new ( scope, noop_tracer_provider ( ) . clone ( ) ) ;
273279 }
274280 Tracer :: new ( scope, self . clone ( ) )
275281 }
@@ -392,16 +398,13 @@ impl Builder {
392398 // For the uncommon case where there are multiple tracer providers with different resource
393399 // configurations, users can optionally provide their own borrowed static resource.
394400 if matches ! ( config. resource, Cow :: Owned ( _) ) {
395- config. resource = match PROVIDER_RESOURCE . try_insert ( config. resource . into_owned ( ) ) {
396- Ok ( static_resource) => Cow :: Borrowed ( static_resource) ,
397- Err ( ( prev, new) ) => {
398- if prev == & new {
399- Cow :: Borrowed ( prev)
400- } else {
401- Cow :: Owned ( new)
401+ config. resource =
402+ match PROVIDER_RESOURCE . get_or_init ( || config. resource . clone ( ) . into_owned ( ) ) {
403+ static_resource if * static_resource == * config. resource . as_ref ( ) => {
404+ Cow :: Borrowed ( static_resource)
402405 }
403- }
404- }
406+ _ => config . resource , // Use the new resource if different
407+ } ;
405408 }
406409
407410 // Create a new vector to hold the modified processors
0 commit comments