@@ -68,32 +68,38 @@ use crate::trace::{
68
68
} ;
69
69
use crate :: Resource ;
70
70
use crate :: { export:: trace:: SpanExporter , trace:: SpanProcessor } ;
71
- use once_cell:: sync:: { Lazy , OnceCell } ;
72
71
use opentelemetry:: trace:: TraceError ;
73
72
use opentelemetry:: InstrumentationScope ;
74
73
use opentelemetry:: { otel_debug, trace:: TraceResult } ;
75
74
use std:: borrow:: Cow ;
76
75
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
77
- use std:: sync:: Arc ;
76
+ use std:: sync:: { Arc , OnceLock } ;
78
77
79
78
use super :: IdGenerator ;
80
79
81
- static PROVIDER_RESOURCE : OnceCell < Resource > = OnceCell :: new ( ) ;
80
+ static PROVIDER_RESOURCE : OnceLock < Resource > = OnceLock :: new ( ) ;
82
81
83
82
// 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
+ }
97
103
98
104
/// TracerProvider inner type
99
105
#[ derive( Debug ) ]
@@ -269,7 +275,7 @@ impl opentelemetry::trace::TracerProvider for TracerProvider {
269
275
270
276
fn tracer_with_scope ( & self , scope : InstrumentationScope ) -> Self :: Tracer {
271
277
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 ( ) ) ;
273
279
}
274
280
Tracer :: new ( scope, self . clone ( ) )
275
281
}
@@ -392,16 +398,13 @@ impl Builder {
392
398
// For the uncommon case where there are multiple tracer providers with different resource
393
399
// configurations, users can optionally provide their own borrowed static resource.
394
400
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)
402
405
}
403
- }
404
- }
406
+ _ => config . resource , // Use the new resource if different
407
+ } ;
405
408
}
406
409
407
410
// Create a new vector to hold the modified processors
0 commit comments