reth_tasks/
metrics.rs

1//! Task Executor Metrics
2
3use core::fmt;
4
5use reth_metrics::{metrics::Counter, Metrics};
6
7/// Task Executor Metrics
8#[derive(Metrics, Clone)]
9#[metrics(scope = "executor.spawn")]
10pub struct TaskExecutorMetrics {
11    /// Number of spawned critical tasks
12    pub(crate) critical_tasks_total: Counter,
13    /// Number of finished spawned critical tasks
14    pub(crate) finished_critical_tasks_total: Counter,
15    /// Number of spawned regular tasks
16    pub(crate) regular_tasks_total: Counter,
17    /// Number of finished spawned regular tasks
18    pub(crate) finished_regular_tasks_total: Counter,
19}
20
21impl TaskExecutorMetrics {
22    /// Increments the counter for spawned critical tasks.
23    pub(crate) fn inc_critical_tasks(&self) {
24        self.critical_tasks_total.increment(1);
25    }
26
27    /// Increments the counter for spawned regular tasks.
28    pub(crate) fn inc_regular_tasks(&self) {
29        self.regular_tasks_total.increment(1);
30    }
31}
32
33/// Helper type for increasing counters even if a task fails
34pub struct IncCounterOnDrop(Counter);
35
36impl fmt::Debug for IncCounterOnDrop {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        f.debug_tuple("IncCounterOnDrop").finish()
39    }
40}
41
42impl IncCounterOnDrop {
43    /// Creates a new instance of `IncCounterOnDrop` with the given counter.
44    pub const fn new(counter: Counter) -> Self {
45        Self(counter)
46    }
47}
48
49impl Drop for IncCounterOnDrop {
50    /// Increment the counter when the instance is dropped.
51    fn drop(&mut self) {
52        self.0.increment(1);
53    }
54}