Skip to content

Commit 4315a74

Browse files
committed
Use i64 for tracing chrome "id"
Perfetto gives an error if an id does not fit in an 64-bit signed integer in 2's complement.
1 parent 7b6ef64 commit 4315a74

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/bin/log/tracing_chrome.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! ```rust
1313
//! tracing::info_span!("my_span", tracing_separate_thread = tracing::field::Empty, /* ... */)
1414
//! ```
15+
//! - use i64 instead of u64 for the "id" in [ChromeLayer::get_root_id] to be compatible with Perfetto
1516
//!
1617
//! Depending on the tracing-chrome crate from crates.io is unfortunately not possible, since it
1718
//! depends on `tracing_core` which conflicts with rustc_private's `tracing_core` (meaning it would
@@ -285,9 +286,9 @@ struct Callsite {
285286
}
286287

287288
enum Message {
288-
Enter(f64, Callsite, Option<u64>),
289+
Enter(f64, Callsite, Option<i64>),
289290
Event(f64, Callsite),
290-
Exit(f64, Callsite, Option<u64>),
291+
Exit(f64, Callsite, Option<i64>),
291292
NewThread(usize, String),
292293
Flush,
293294
Drop,
@@ -519,14 +520,17 @@ where
519520
}
520521
}
521522

522-
fn get_root_id(&self, span: SpanRef<S>) -> Option<u64> {
523+
fn get_root_id(&self, span: SpanRef<S>) -> Option<i64> {
524+
// Returns `Option<i64>` instead of `Option<u64>` because apparently Perfetto gives an
525+
// error if an id does not fit in a 64-bit signed integer in 2's complement. We cast the
526+
// span id from `u64` to `i64` with wraparound, since negative values are fine.
523527
match self.trace_style {
524528
TraceStyle::Threaded => {
525529
if span.fields().field("tracing_separate_thread").is_some() {
526530
// assign an independent "id" to spans with argument "tracing_separate_thread",
527531
// so they appear a separate trace line in trace visualization tools, see
528532
// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.jh64i9l3vwa1
529-
Some(span.id().into_u64())
533+
Some(span.id().into_u64().cast_signed()) // the comment above explains the cast
530534
} else {
531535
None
532536
}
@@ -539,6 +543,7 @@ where
539543
.unwrap_or(span)
540544
.id()
541545
.into_u64()
546+
.cast_signed() // the comment above explains the cast
542547
),
543548
}
544549
}

0 commit comments

Comments
 (0)