|
1 | 1 | // SPDX-License-Identifier: MIT
|
2 | 2 | // 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_line", it will be given a separate span |
| 10 | +//! ID even in [TraceStyle::Threaded] mode, to make it appear on a separate line when viewing the |
| 11 | +//! trace in <https://ui.perfetto.dev>. This is the syntax to trigger this behavior: |
| 12 | +//! ```rust |
| 13 | +//! tracing::info_span!("my_span", tracing_separate_line = tracing::field::Empty, /* ... */) |
| 14 | +//! ``` |
| 15 | +//! |
6 | 16 | //! Depending on the tracing-chrome crate from crates.io is unfortunately not possible, since it
|
7 | 17 | //! depends on `tracing_core` which conflicts with rustc_private's `tracing_core` (meaning it would
|
8 | 18 | //! not be possible to use the [ChromeLayer] in a context that expects a [Layer] from
|
@@ -79,14 +89,26 @@ where
|
79 | 89 | }
|
80 | 90 |
|
81 | 91 | /// Decides how traces will be recorded.
|
| 92 | +/// Also see <https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.jh64i9l3vwa1> |
82 | 93 | #[derive(Default)]
|
83 | 94 | 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>. |
85 | 97 | /// 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_line", 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_line = 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_line" argument and not print out anything for it. |
86 | 107 | #[default]
|
87 | 108 | Threaded,
|
88 | 109 |
|
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>. |
90 | 112 | Async,
|
91 | 113 | }
|
92 | 114 |
|
@@ -497,31 +519,39 @@ where
|
497 | 519 | }
|
498 | 520 | }
|
499 | 521 |
|
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_line").is_some() { |
| 526 | + // put spans with argument "tracing_separate_line" on a separate trace line |
| 527 | + // by setting their "id", 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 | + } |
508 | 544 | }
|
509 | 545 |
|
510 | 546 | fn enter_span(&self, span: SpanRef<S>, ts: f64) {
|
511 | 547 | 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); |
516 | 549 | self.send_message(Message::Enter(ts, callsite, root_id));
|
517 | 550 | }
|
518 | 551 |
|
519 | 552 | fn exit_span(&self, span: SpanRef<S>, ts: f64) {
|
520 | 553 | 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); |
525 | 555 | self.send_message(Message::Exit(ts, callsite, root_id));
|
526 | 556 | }
|
527 | 557 |
|
|
0 commit comments