Crate reqray[][src]

Expand description

Log “request x-rays” for rust programs instrumented with tracing. This includes aggregated wall/own times as frequently found in flame graphs in a human-friendly text format. Example:

Dec 20 18:48:32.405  INFO Call summary of request@examples/nested.rs:47

                    ────────────┼──────────────┼──────────────┼───────────────────────
                          0 001 ┊      377.886 ┊        0.260 ┊ ┬ request
                          0 001 ┊      120.704 ┊       48.896 ┊ ├┬ nested
                          0 001 ┊        0.008 ┊        0.008 ┊ ┊├─ random
                          1 000 ┊       64.347 ┊       64.347 ┊ ┊╰─ repeated
                          0 002 ┊        0.118 ┊        0.118 ┊ ├─ repeated
                          0 001 ┊        3.818 ┊        0.049 ┊ ├┬ nest_deeply
                          0 001 ┊        3.762 ┊        0.053 ┊ ┊╰┬ nest_deeply
                          0 001 ┊        3.702 ┊        0.057 ┊ ┊ ╰┬ nest_deeply
                          0 001 ┊        3.637 ┊        0.056 ┊ ┊  ╰┬ nest_deeply
                          0 001 ┊        3.574 ┊        0.058 ┊ ┊   ╰┬ nest_deeply
                          0 001 ┊        3.503 ┊        0.061 ┊ ┊    ╰┬ nest_deeply
                          0 001 ┊        3.435 ┊        0.063 ┊ ┊     ╰┬ nest_deeply
                          0 001 ┊        3.365 ┊        0.066 ┊ ┊      ╰┬ nest_deeply
                          0 001 ┊        3.292 ┊        3.292 ┊ ┊       ╰─ nest_deeply
                          0 001 ┊      252.949 ┊       49.258 ┊ ╰┬ nested2
                          0 001 ┊        0.006 ┊        0.006 ┊  ├─ random
                          1 000 ┊       63.343 ┊       63.343 ┊  ├─ repeated
                          0 001 ┊      132.841 ┊       54.091 ┊  ╰┬ nested
                          0 001 ┊        0.007 ┊        0.007 ┊   ├─ random
                          1 000 ┊       70.875 ┊       70.875 ┊   ╰─ repeated

Under the hood, reqray provides a CallTreeCollector tracing Layer which, unsurprisingly, collects call trees. Once the root span (e.g. the top most span = the top most instrumented call) has been closed, the finished call tree is handed over to a FinishedCallTreeProcessor. LoggingCallTreeCollector implements FinishedCallTreeProcessor and logs each call tree in human-friendly way as shown above.

Let’s assume that you already have an explicit setup for tracing like this, then you simply need to add the highlighted line:

    use reqray::CallTreeCollector;
    use tracing_subscriber::{EnvFilter, util::SubscriberInitExt, fmt, prelude::*};

    let fmt_layer = fmt::layer()
        .with_target(false);
    let filter_layer = EnvFilter::try_from_default_env()
        .or_else(|_| EnvFilter::try_new("info"))
        .unwrap();

    tracing_subscriber::registry()
    // -----------------------------------------------
        .with(CallTreeCollector::default())
    // -----------------------------------------------
        .with(filter_layer)
        .with(fmt_layer)
        .init();

Instead of CallTreeCollector::default() you can chose a more explicit config using CallTreeCollectorBuilder and LoggingCallTreeCollectorBuilder.

use reqray::{CallTreeCollectorBuilder, display::LoggingCallTreeCollectorBuilder};
use tracing_subscriber::{EnvFilter, util::SubscriberInitExt, fmt, prelude::*};

// ...
let call_tree_collector = CallTreeCollectorBuilder::default()
    .max_call_depth(10)
    .build_with_collector(
        LoggingCallTreeCollectorBuilder::default()
            .left_margin(20)
            .build(),
    );

tracing_subscriber::registry()
    .with(call_tree_collector)
    // ...

Modules

Structs

A CallPathPool contains all CallPathTimings of a call tree indexed by CallPathPoolIds.

A CallPathTiming is an aggregation of all spans with the same call path. That means that their callsite::Identifier is the same and all the callsite::Identifiers of their ancestor spans are also the same.

A tracing::Subscriber which collects call trees and hands finished trees to a FinishedCallTreeProcessor.

Traits

A FinishedCallTreeProcessor uses the aggregated call tree for something useful.