11// SPDX-License-Identifier: MIT
22// SPDX-FileCopyrightText: Copyright (c) 2020 Thoren Paulson
3- //! This file is taken unmodified from the following link, except for file attributes and
4- //! `extern crate` at the top.
3+ //! This file was initially taken from the following link, the changes that were made to the
4+ //! original file can be found in git history (`git log -- path/to/tracing_chrome.rs`):
55//! https://github.com/thoren-d/tracing-chrome/blob/7e2625ab4aeeef2f0ef9bde9d6258dd181c04472/src/lib.rs
66//! Depending on the tracing-chrome crate from crates.io is unfortunately not possible, since it
77//! depends on `tracing_core` which conflicts with rustc_private's `tracing_core` (meaning it would
@@ -86,6 +86,13 @@ pub enum TraceStyle {
8686 #[ default]
8787 Threaded ,
8888
89+ /// Like [TraceStyle::Threaded], except that spans whose name is in
90+ /// [TraceStyle::ThreadedWithExceptions::separate_span_names] will be grouped by name separately
91+ /// than all other spans.
92+ ThreadedWithExceptions {
93+ separate_span_names : Vec < String >
94+ } ,
95+
8996 /// Traces will recorded as a group of asynchronous operations.
9097 Async ,
9198}
@@ -497,31 +504,38 @@ where
497504 }
498505 }
499506
500- fn get_root_id ( span : SpanRef < S > ) -> u64 {
501- span. scope ( )
502- . from_root ( )
503- . take ( 1 )
504- . next ( )
505- . unwrap_or ( span)
506- . id ( )
507- . into_u64 ( )
507+ fn get_root_id ( & self , span : SpanRef < S > ) -> Option < u64 > {
508+ match & self . trace_style {
509+ TraceStyle :: Async => Some (
510+ span. scope ( )
511+ . from_root ( )
512+ . take ( 1 )
513+ . next ( )
514+ . unwrap_or ( span)
515+ . id ( )
516+ . into_u64 ( )
517+ ) ,
518+ TraceStyle :: ThreadedWithExceptions { separate_span_names } => {
519+ let span_name = span. metadata ( ) . name ( ) ;
520+ // This returns None if a span is not in the list, making all such spans appear on a
521+ // separate line. Furthermore, the `pos + 1` is because root_id must be > 0.
522+ separate_span_names. iter ( )
523+ . position ( |r| r == span_name)
524+ . map ( |pos| ( pos + 1 ) as u64 )
525+ }
526+ TraceStyle :: Threaded => None ,
527+ }
508528 }
509529
510530 fn enter_span ( & self , span : SpanRef < S > , ts : f64 ) {
511531 let callsite = self . get_callsite ( EventOrSpan :: Span ( & span) ) ;
512- let root_id = match self . trace_style {
513- TraceStyle :: Async => Some ( ChromeLayer :: get_root_id ( span) ) ,
514- _ => None ,
515- } ;
532+ let root_id = self . get_root_id ( span) ;
516533 self . send_message ( Message :: Enter ( ts, callsite, root_id) ) ;
517534 }
518535
519536 fn exit_span ( & self , span : SpanRef < S > , ts : f64 ) {
520537 let callsite = self . get_callsite ( EventOrSpan :: Span ( & span) ) ;
521- let root_id = match self . trace_style {
522- TraceStyle :: Async => Some ( ChromeLayer :: get_root_id ( span) ) ,
523- _ => None ,
524- } ;
538+ let root_id = self . get_root_id ( span) ;
525539 self . send_message ( Message :: Exit ( ts, callsite, root_id) ) ;
526540 }
527541
@@ -591,7 +605,7 @@ where
591605 args : Arc :: new ( args) ,
592606 } ) ;
593607 }
594- if let TraceStyle :: Threaded = self . trace_style {
608+ if ! matches ! ( self . trace_style , TraceStyle :: Async ) {
595609 return ;
596610 }
597611
@@ -600,7 +614,7 @@ where
600614 }
601615
602616 fn on_close ( & self , id : span:: Id , ctx : Context < ' _ , S > ) {
603- if let TraceStyle :: Threaded = self . trace_style {
617+ if ! matches ! ( self . trace_style , TraceStyle :: Async ) {
604618 return ;
605619 }
606620
0 commit comments