Skip to content

Commit abd1b4d

Browse files
committed
Auto merge of #144058 - matthiaskrgr:rollup-xezozsk, r=matthiaskrgr
Rollup of 11 pull requests Successful merges: - rust-lang/rust#143326 (Remove deprecated `Error::description` impl from `c_str::FromBytesWithNulError`) - rust-lang/rust#143431 (Use relative visibility when noting sealed trait to reduce false positive) - rust-lang/rust#143550 (resolve: Use interior mutability for extern module map) - rust-lang/rust#143631 (update to literal-escaper-0.0.5) - rust-lang/rust#143793 (Opaque type collection: Guard against endlessly recursing free alias types) - rust-lang/rust#143880 (tests: Test line debuginfo for linebreaked function parameters) - rust-lang/rust#143914 (Reword mismatched-lifetime-syntaxes text based on feedback) - rust-lang/rust#143926 (Remove deprecated fields in bootstrap) - rust-lang/rust#143955 (Make frame spans appear on a separate trace line) - rust-lang/rust#143975 (type_id_eq: check that the hash fully matches the type) - rust-lang/rust#143984 (Fix ice for feature-gated `cfg` attributes applied to the crate) r? `@ghost` `@rustbot` modify labels: rollup
2 parents bed032f + 055d8f5 commit abd1b4d

File tree

1 file changed

+51
-21
lines changed

1 file changed

+51
-21
lines changed

src/bin/log/tracing_chrome.rs

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
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.
5-
//! https://github.com/thoren-d/tracing-chrome/blob/7e2625ab4aeeef2f0ef9bde9d6258dd181c04472/src/lib.rs
3+
//! This file was initially taken from the following link:
4+
//! <https://github.com/thoren-d/tracing-chrome/blob/7e2625ab4aeeef2f0ef9bde9d6258dd181c04472/src/lib.rs>
5+
//!
6+
//! The precise changes that were made to the original file can be found in git history
7+
//! (`git log -- path/to/tracing_chrome.rs`), but in summary:
8+
//! - the file attributes were changed and `extern crate` was added at the top
9+
//! - if a tracing span has a field called "tracing_separate_thread", it will be given a separate
10+
//! span ID even in [TraceStyle::Threaded] mode, to make it appear on a separate line when viewing
11+
//! the trace in <https://ui.perfetto.dev>. This is the syntax to trigger this behavior:
12+
//! ```rust
13+
//! tracing::info_span!("my_span", tracing_separate_thread = tracing::field::Empty, /* ... */)
14+
//! ```
15+
//!
616
//! Depending on the tracing-chrome crate from crates.io is unfortunately not possible, since it
717
//! depends on `tracing_core` which conflicts with rustc_private's `tracing_core` (meaning it would
818
//! not be possible to use the [ChromeLayer] in a context that expects a [Layer] from
@@ -79,14 +89,26 @@ where
7989
}
8090

8191
/// Decides how traces will be recorded.
92+
/// Also see <https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.jh64i9l3vwa1>
8293
#[derive(Default)]
8394
pub enum TraceStyle {
84-
/// Traces will be recorded as a group of threads.
95+
/// Traces will be recorded as a group of threads, and all spans on the same thread will appear
96+
/// on a single trace line in <https://ui.perfetto.dev>.
8597
/// In this style, spans should be entered and exited on the same thread.
98+
///
99+
/// If a tracing span has a field called "tracing_separate_thread", it will be given a separate
100+
/// span ID even in this mode, to make it appear on a separate line when viewing the trace in
101+
/// <https://ui.perfetto.dev>. This is the syntax to trigger this behavior:
102+
/// ```rust
103+
/// tracing::info_span!("my_span", tracing_separate_thread = tracing::field::Empty, /* ... */)
104+
/// ```
105+
/// [tracing::field::Empty] is used so that other tracing layers (e.g. the logger) will ignore
106+
/// the "tracing_separate_thread" argument and not print out anything for it.
86107
#[default]
87108
Threaded,
88109

89-
/// Traces will recorded as a group of asynchronous operations.
110+
/// Traces will recorded as a group of asynchronous operations. All spans will be given separate
111+
/// span IDs and will appear on separate trace lines in <https://ui.perfetto.dev>.
90112
Async,
91113
}
92114

@@ -497,31 +519,39 @@ where
497519
}
498520
}
499521

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()
522+
fn get_root_id(&self, span: SpanRef<S>) -> Option<u64> {
523+
match self.trace_style {
524+
TraceStyle::Threaded => {
525+
if span.fields().field("tracing_separate_thread").is_some() {
526+
// assign an independent "id" to spans with argument "tracing_separate_thread",
527+
// so they appear a separate trace line in trace visualization tools, see
528+
// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.jh64i9l3vwa1
529+
Some(span.id().into_u64())
530+
} else {
531+
None
532+
}
533+
},
534+
TraceStyle::Async => Some(
535+
span.scope()
536+
.from_root()
537+
.take(1)
538+
.next()
539+
.unwrap_or(span)
540+
.id()
541+
.into_u64()
542+
),
543+
}
508544
}
509545

510546
fn enter_span(&self, span: SpanRef<S>, ts: f64) {
511547
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-
};
548+
let root_id = self.get_root_id(span);
516549
self.send_message(Message::Enter(ts, callsite, root_id));
517550
}
518551

519552
fn exit_span(&self, span: SpanRef<S>, ts: f64) {
520553
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-
};
554+
let root_id = self.get_root_id(span);
525555
self.send_message(Message::Exit(ts, callsite, root_id));
526556
}
527557

0 commit comments

Comments
 (0)