tracing-prof 0.3.0

Experimental library for profiling tracing spans.
Documentation
//! Profiling configuration.

use crate::IndexSet;

/// Configuration for the profiling layer.
///
/// By default all metric collection is disabled.
#[derive(Debug, Clone)]
#[must_use]
pub struct ProfileConfig {
    pub(crate) track_allocations: bool,
    pub(crate) track_heap: bool,
    pub(crate) track_cpu_time: bool,
    pub(crate) track_wall_time: bool,
    pub(crate) max_samples: usize,
    pub(crate) heap_snapshot_interval: std::time::Duration,
    pub(crate) record_labels: IndexSet<String>,
    pub(crate) max_closed_heap_spans: usize,
}

impl Default for ProfileConfig {
    fn default() -> Self {
        Self {
            track_allocations: false,
            track_heap: false,
            track_cpu_time: false,
            track_wall_time: false,
            max_samples: 1000,
            heap_snapshot_interval: std::time::Duration::from_secs(10),
            record_labels: IndexSet::default(),
            max_closed_heap_spans: 1000,
        }
    }
}

impl ProfileConfig {
    /// Creates a new `ProfileConfig` with default settings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable/disable tracking of allocations.
    ///
    /// In order to track allocations, the global allocator must be
    /// set to [`TrackingAllocator`](crate::allocator::TrackingAllocator).
    ///
    /// By default, this is disabled.
    pub fn track_allocations(mut self, enable: bool) -> Self {
        self.track_allocations = enable;

        self
    }

    /// Enable/disable tracking of heap usage.
    ///
    /// Implicitly enables tracking of allocations.
    ///
    /// By default, this is disabled.
    pub fn track_heap(mut self, enable: bool) -> Self {
        self.track_heap = enable;

        if enable {
            self.track_allocations = true;
        }

        self
    }

    /// Enable/disable tracking of CPU time.
    ///
    /// By default, this is disabled.
    pub fn track_cpu(mut self, enable: bool) -> Self {
        self.track_cpu_time = enable;
        self
    }

    /// Enable/disable tracking of wall time.
    ///
    /// By default, this is disabled.
    pub fn track_wall(mut self, enable: bool) -> Self {
        self.track_wall_time = enable;
        self
    }

    /// Enable all profiling features.
    pub fn track_all(mut self) -> Self {
        self.track_allocations = true;
        self.track_heap = true;
        self.track_cpu_time = true;
        self.track_wall_time = true;
        self
    }

    /// Use the given fields as labels for all spans.
    ///
    /// By default, no labels are recorded as this is an expensive operation.
    pub fn record_labels(mut self, labels: &[&str]) -> Self {
        self.record_labels = labels.iter().map(ToString::to_string).collect();
        self
    }

    /// Set the heap snapshot interval for the reporter.
    ///
    /// Pyroscope will sum snapshot values if they are too frequent,
    /// this is a (hopefully) temporary workaround to avoid that.
    ///
    /// Set this to 0 to send heap snapshots on every poll.
    ///
    /// By default, this is 10 seconds.
    pub fn heap_snapshot_interval(mut self, interval: std::time::Duration) -> Self {
        self.heap_snapshot_interval = interval;
        self
    }

    /// Set the maximum number of samples to accumulate before flushing.
    ///
    /// Note that this is a soft limit, the reporter may flush more often
    /// or less often depending on the load.
    ///
    /// By default, this is 1000.
    pub fn max_samples(mut self, max_samples: usize) -> Self {
        self.max_samples = max_samples;
        self
    }

    /// Set the maximum number of closed spans to keep in the heap tracker.
    /// If this is exceeded, closed spans will be dropped from heap tracking.
    ///
    /// Spans with the lowest amount of allocated bytes still in use will be dropped first.
    ///
    /// By default, this is 1000.
    pub fn max_closed_heap_spans(mut self, max_closed_heap_spans: usize) -> Self {
        self.max_closed_heap_spans = max_closed_heap_spans;
        self
    }
}