tracing-prof 0.3.0

Experimental library for profiling tracing spans.
Documentation
#![allow(missing_docs)]

use tracing_prof::{
    ProfileConfig, ProfileLayer, TrackingAllocator,
    reporter::pprof::{
        PProfReporter, PProfReporterConfig,
        backend::pyroscope::{PyroscopeBackend, PyroscopeBackendConfig},
    },
    sampler::probabilistic::ProbabilisticSampler,
};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[global_allocator]
static ALLOCATOR: TrackingAllocator = TrackingAllocator::system();

fn do_unnecessary_cpu_intensive_work() {
    let mut sum = 0_usize;
    for i in 0..1_000_000 {
        sum += i;
    }
    println!("Sum: {sum}");
}

fn main() {
    let profile_layer = ProfileLayer::new_with_sampler(
        ProfileConfig::default().track_all(),
        PProfReporter::new(
            PProfReporterConfig::default().aggregate_samples(true),
            PyroscopeBackend::new(PyroscopeBackendConfig::new(
                "tracing-prof-test",
                "http://localhost:4040/ingest",
            )),
        ),
        ProbabilisticSampler::new(0.5),
    );

    let _handle = profile_layer.shutdown_handle();

    tracing_subscriber::registry()
        .with(tracing_subscriber::fmt::layer())
        .with(profile_layer)
        .with(
            tracing_subscriber::EnvFilter::builder()
                .with_default_directive(tracing::Level::INFO.into())
                .from_env_lossy(),
        )
        .init();

    let start = std::time::Instant::now();

    while start.elapsed() < std::time::Duration::from_secs(300) {
        let mut handles = Vec::new();
        for _ in 0..10 {
            handles.push(std::thread::spawn(foo));
        }
        for handle in handles {
            handle.join().unwrap();
        }

        foo();

        let v = bar();

        std::thread::sleep(std::time::Duration::from_millis(500));
        drop(v);
    }

    std::thread::sleep(std::time::Duration::from_secs(120));
}

#[tracing::instrument]
fn foo() {
    for _ in 0..100 {
        let _v: Vec<u8> = Vec::with_capacity(10_000_000);
        let _v2 = bar();
        do_unnecessary_cpu_intensive_work();
    }
}

#[tracing::instrument]
fn bar() -> Vec<u8> {
    let v: Vec<u8> = Vec::with_capacity(10_000_000);
    v
}