@@ -124,12 +124,17 @@ unsafe impl Send for ProcessorPtr {}
124124
125125unsafe impl Sync for ProcessorPtr { }
126126
127+ thread_local ! {
128+ // Processor spans are created on the hot path, so cache the current thread name per thread.
129+ static CURRENT_THREAD_NAME : String = std:: thread:: current( )
130+ . name( )
131+ . unwrap_or( "unnamed" )
132+ . to_string( ) ;
133+ }
134+
127135impl ProcessorPtr {
128136 fn current_thread_name ( ) -> String {
129- std:: thread:: current ( )
130- . name ( )
131- . unwrap_or ( "unnamed" )
132- . to_string ( )
137+ CURRENT_THREAD_NAME . with ( |thread_name| thread_name. clone ( ) )
133138 }
134139
135140 #[ allow( clippy:: arc_with_non_send_sync) ]
@@ -181,10 +186,9 @@ impl ProcessorPtr {
181186 /// # Safety
182187 pub unsafe fn process ( & self ) -> Result < ( ) > {
183188 unsafe {
184- let thread_name = Self :: current_thread_name ( ) ;
185189 let span = LocalSpan :: enter_with_local_parent ( format ! ( "{}::process" , self . name( ) ) )
186190 . with_property ( || ( "graph-node-id" , self . id ( ) . index ( ) . to_string ( ) ) )
187- . with_property ( || ( "thread_name" , thread_name . clone ( ) ) ) ;
191+ . with_property ( || ( "thread_name" , Self :: current_thread_name ( ) ) ) ;
188192
189193 match ( * self . inner . get ( ) ) . process ( ) {
190194 Ok ( _) => Ok ( ( ) ) ,
@@ -210,7 +214,6 @@ impl ProcessorPtr {
210214 let id = self . id ( ) ;
211215 let mut name = self . name ( ) ;
212216 name. push_str ( "::async_process" ) ;
213- let thread_name = Self :: current_thread_name ( ) ;
214217
215218 let task = ( * self . inner . get ( ) ) . async_process ( ) ;
216219
@@ -225,9 +228,12 @@ impl ProcessorPtr {
225228
226229 let inner = self . inner . clone ( ) ;
227230 async move {
228- let span = Span :: enter_with_local_parent ( name)
229- . with_property ( || ( "graph-node-id" , id. index ( ) . to_string ( ) ) )
230- . with_property ( || ( "thread_name" , thread_name. clone ( ) ) ) ;
231+ let span = match SpanContext :: current_local_parent ( ) {
232+ Some ( parent) if parent. sampled => Span :: enter_with_local_parent ( name)
233+ . with_property ( || ( "graph-node-id" , id. index ( ) . to_string ( ) ) )
234+ . with_property ( || ( "thread_name" , Self :: current_thread_name ( ) ) ) ,
235+ _ => Span :: noop ( ) ,
236+ } ;
231237
232238 match task. await {
233239 Ok ( _) => {
0 commit comments