Skip to content

Make "frame" spans go on a separate trace line than others #4451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/bin/log/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ fn init_logger_once(early_dcx: &EarlyDiagCtxt) {

#[cfg(feature = "tracing")]
{
let (chrome_layer, chrome_guard) =
super::tracing_chrome::ChromeLayerBuilder::new().include_args(true).build();
let (chrome_layer, chrome_guard) = super::tracing_chrome::ChromeLayerBuilder::new()
.include_args(true)
// "frame" spans indicate stack frames in the interpreted program:
// let's make them appear on a separate trace line than other spans.
.trace_style(super::tracing_chrome::TraceStyle::ThreadedWithExceptions { separate_span_names: vec!["frame".to_string()] })
.build();
rustc_driver::init_logger_with_additional_layer(
early_dcx,
rustc_logger_config(),
Expand Down
54 changes: 34 additions & 20 deletions src/bin/log/tracing_chrome.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: Copyright (c) 2020 Thoren Paulson
//! This file is taken unmodified from the following link, except for file attributes and
//! `extern crate` at the top.
//! This file was initially taken from the following link, the changes that were made to the
//! original file can be found in git history (`git log -- path/to/tracing_chrome.rs`):
//! https://github.com/thoren-d/tracing-chrome/blob/7e2625ab4aeeef2f0ef9bde9d6258dd181c04472/src/lib.rs
//! Depending on the tracing-chrome crate from crates.io is unfortunately not possible, since it
//! depends on `tracing_core` which conflicts with rustc_private's `tracing_core` (meaning it would
Expand Down Expand Up @@ -86,6 +86,13 @@ pub enum TraceStyle {
#[default]
Threaded,

/// Like [TraceStyle::Threaded], except that spans whose name is in
/// [TraceStyle::ThreadedWithExceptions::separate_span_names] will be grouped by name separately
/// than all other spans.
ThreadedWithExceptions {
separate_span_names: Vec<String>
},

/// Traces will recorded as a group of asynchronous operations.
Async,
}
Expand Down Expand Up @@ -497,31 +504,38 @@ where
}
}

fn get_root_id(span: SpanRef<S>) -> u64 {
span.scope()
.from_root()
.take(1)
.next()
.unwrap_or(span)
.id()
.into_u64()
fn get_root_id(&self, span: SpanRef<S>) -> Option<u64> {
match &self.trace_style {
TraceStyle::Async => Some(
span.scope()
.from_root()
.take(1)
.next()
.unwrap_or(span)
.id()
.into_u64()
),
TraceStyle::ThreadedWithExceptions { separate_span_names } => {
let span_name = span.metadata().name();
// This returns None if a span is not in the list, making all such spans appear on a
// separate line. Furthermore, the `pos + 1` is because root_id must be > 0.
separate_span_names.iter()
.position(|r| r == span_name)
.map(|pos| (pos + 1) as u64)
}
TraceStyle::Threaded => None,
}
}

fn enter_span(&self, span: SpanRef<S>, ts: f64) {
let callsite = self.get_callsite(EventOrSpan::Span(&span));
let root_id = match self.trace_style {
TraceStyle::Async => Some(ChromeLayer::get_root_id(span)),
_ => None,
};
let root_id = self.get_root_id(span);
self.send_message(Message::Enter(ts, callsite, root_id));
}

fn exit_span(&self, span: SpanRef<S>, ts: f64) {
let callsite = self.get_callsite(EventOrSpan::Span(&span));
let root_id = match self.trace_style {
TraceStyle::Async => Some(ChromeLayer::get_root_id(span)),
_ => None,
};
let root_id = self.get_root_id(span);
self.send_message(Message::Exit(ts, callsite, root_id));
}

Expand Down Expand Up @@ -591,7 +605,7 @@ where
args: Arc::new(args),
});
}
if let TraceStyle::Threaded = self.trace_style {
if !matches!(self.trace_style, TraceStyle::Async) {
return;
}

Expand All @@ -600,7 +614,7 @@ where
}

fn on_close(&self, id: span::Id, ctx: Context<'_, S>) {
if let TraceStyle::Threaded = self.trace_style {
if !matches!(self.trace_style, TraceStyle::Async) {
return;
}

Expand Down