Skip to main content

tracing_prof/
config.rs

1//! Profiling configuration.
2
3use crate::IndexSet;
4
5/// Configuration for the profiling layer.
6///
7/// By default all metric collection is disabled.
8#[derive(Debug, Clone)]
9#[must_use]
10pub struct ProfileConfig {
11    pub(crate) track_allocations: bool,
12    pub(crate) track_heap: bool,
13    pub(crate) track_cpu_time: bool,
14    pub(crate) track_wall_time: bool,
15    pub(crate) max_samples: usize,
16    pub(crate) heap_snapshot_interval: std::time::Duration,
17    pub(crate) record_labels: IndexSet<String>,
18    pub(crate) max_closed_heap_spans: usize,
19}
20
21impl Default for ProfileConfig {
22    fn default() -> Self {
23        Self {
24            track_allocations: false,
25            track_heap: false,
26            track_cpu_time: false,
27            track_wall_time: false,
28            max_samples: 1000,
29            heap_snapshot_interval: std::time::Duration::from_secs(10),
30            record_labels: IndexSet::default(),
31            max_closed_heap_spans: 1000,
32        }
33    }
34}
35
36impl ProfileConfig {
37    /// Creates a new `ProfileConfig` with default settings.
38    pub fn new() -> Self {
39        Self::default()
40    }
41
42    /// Enable/disable tracking of allocations.
43    ///
44    /// In order to track allocations, the global allocator must be
45    /// set to [`TrackingAllocator`](crate::allocator::TrackingAllocator).
46    ///
47    /// By default, this is disabled.
48    pub fn track_allocations(mut self, enable: bool) -> Self {
49        self.track_allocations = enable;
50
51        self
52    }
53
54    /// Enable/disable tracking of heap usage.
55    ///
56    /// Implicitly enables tracking of allocations.
57    ///
58    /// By default, this is disabled.
59    pub fn track_heap(mut self, enable: bool) -> Self {
60        self.track_heap = enable;
61
62        if enable {
63            self.track_allocations = true;
64        }
65
66        self
67    }
68
69    /// Enable/disable tracking of CPU time.
70    ///
71    /// By default, this is disabled.
72    pub fn track_cpu(mut self, enable: bool) -> Self {
73        self.track_cpu_time = enable;
74        self
75    }
76
77    /// Enable/disable tracking of wall time.
78    ///
79    /// By default, this is disabled.
80    pub fn track_wall(mut self, enable: bool) -> Self {
81        self.track_wall_time = enable;
82        self
83    }
84
85    /// Enable all profiling features.
86    pub fn track_all(mut self) -> Self {
87        self.track_allocations = true;
88        self.track_heap = true;
89        self.track_cpu_time = true;
90        self.track_wall_time = true;
91        self
92    }
93
94    /// Use the given fields as labels for all spans.
95    ///
96    /// By default, no labels are recorded as this is an expensive operation.
97    pub fn record_labels(mut self, labels: &[&str]) -> Self {
98        self.record_labels = labels.iter().map(ToString::to_string).collect();
99        self
100    }
101
102    /// Set the heap snapshot interval for the reporter.
103    ///
104    /// Pyroscope will sum snapshot values if they are too frequent,
105    /// this is a (hopefully) temporary workaround to avoid that.
106    ///
107    /// Set this to 0 to send heap snapshots on every poll.
108    ///
109    /// By default, this is 10 seconds.
110    pub fn heap_snapshot_interval(mut self, interval: std::time::Duration) -> Self {
111        self.heap_snapshot_interval = interval;
112        self
113    }
114
115    /// Set the maximum number of samples to accumulate before flushing.
116    ///
117    /// Note that this is a soft limit, the reporter may flush more often
118    /// or less often depending on the load.
119    ///
120    /// By default, this is 1000.
121    pub fn max_samples(mut self, max_samples: usize) -> Self {
122        self.max_samples = max_samples;
123        self
124    }
125
126    /// Set the maximum number of closed spans to keep in the heap tracker.
127    /// If this is exceeded, closed spans will be dropped from heap tracking.
128    ///
129    /// Spans with the lowest amount of allocated bytes still in use will be dropped first.
130    ///
131    /// By default, this is 1000.
132    pub fn max_closed_heap_spans(mut self, max_closed_heap_spans: usize) -> Self {
133        self.max_closed_heap_spans = max_closed_heap_spans;
134        self
135    }
136}