Skip to main content

reth_tasks/
metrics.rs

1//! Task Executor Metrics
2
3use core::fmt;
4
5use reth_metrics::{
6    metrics::{Counter, Histogram},
7    Metrics,
8};
9use std::time::Duration;
10
11/// Task Executor Metrics
12#[derive(Metrics, Clone)]
13#[metrics(scope = "executor.spawn")]
14pub struct TaskExecutorMetrics {
15    /// Number of spawned critical tasks
16    pub(crate) critical_tasks_total: Counter,
17    /// Number of finished spawned critical tasks
18    pub(crate) finished_critical_tasks_total: Counter,
19    /// Number of spawned regular tasks
20    pub(crate) regular_tasks_total: Counter,
21    /// Number of finished spawned regular tasks
22    pub(crate) finished_regular_tasks_total: Counter,
23    /// Number of spawned regular blocking tasks
24    pub(crate) regular_blocking_tasks_total: Counter,
25    /// Number of finished spawned regular blocking tasks
26    pub(crate) finished_regular_blocking_tasks_total: Counter,
27}
28
29impl TaskExecutorMetrics {
30    /// Increments the counter for spawned critical tasks.
31    pub(crate) fn inc_critical_tasks(&self) {
32        self.critical_tasks_total.increment(1);
33    }
34
35    /// Increments the counter for spawned regular tasks.
36    pub(crate) fn inc_regular_tasks(&self) {
37        self.regular_tasks_total.increment(1);
38    }
39
40    /// Increments the counter for spawned regular blocking tasks.
41    pub(crate) fn inc_regular_blocking_tasks(&self) {
42        self.regular_blocking_tasks_total.increment(1);
43    }
44}
45
46/// Helper type for increasing counters even if a task fails
47pub struct IncCounterOnDrop(Counter);
48
49impl fmt::Debug for IncCounterOnDrop {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        f.debug_tuple("IncCounterOnDrop").finish()
52    }
53}
54
55impl IncCounterOnDrop {
56    /// Creates a new instance of `IncCounterOnDrop` with the given counter.
57    pub const fn new(counter: Counter) -> Self {
58        Self(counter)
59    }
60}
61
62impl Drop for IncCounterOnDrop {
63    /// Increment the counter when the instance is dropped.
64    fn drop(&mut self) {
65        self.0.increment(1);
66    }
67}
68
69/// Metrics for a named worker managed by the [`crate::worker_map::WorkerMap`].
70#[derive(Metrics, Clone)]
71#[metrics(scope = "executor.named_worker")]
72pub(crate) struct WorkerThreadMetrics {
73    /// Time spent queued before a named worker starts executing the task.
74    queue_wait_duration_seconds: Histogram,
75    /// Time spent executing a task on a named worker.
76    task_duration_seconds: Histogram,
77}
78
79impl WorkerThreadMetrics {
80    /// Create metrics for the named worker.
81    pub(crate) fn new(worker: &'static str) -> Self {
82        Self::new_with_labels(&[("worker", worker)])
83    }
84
85    /// Record how long a task waited for this worker.
86    pub(crate) fn record_queue_wait(&self, duration: Duration) {
87        self.queue_wait_duration_seconds.record(duration.as_secs_f64());
88    }
89
90    /// Record how long a task executed on this worker.
91    pub(crate) fn record_task_duration(&self, duration: Duration) {
92        self.task_duration_seconds.record(duration.as_secs_f64());
93    }
94}
95
96/// Metrics for jobs submitted through [`crate::pool::WorkerPool`] wrapper methods.
97#[cfg(feature = "rayon")]
98#[derive(Metrics, Clone)]
99#[metrics(scope = "executor.worker_pool")]
100pub(crate) struct WorkerPoolMetrics {
101    /// Time spent queued before the worker pool starts executing the outer job.
102    job_queue_wait_duration_seconds: Histogram,
103    /// Time spent executing the outer job on the worker pool.
104    job_duration_seconds: Histogram,
105}
106
107#[cfg(feature = "rayon")]
108impl WorkerPoolMetrics {
109    /// Create metrics for the worker pool.
110    pub(crate) fn new(pool: &'static str) -> Self {
111        Self::new_with_labels(&[("pool", pool)])
112    }
113
114    /// Record how long an outer job waited for this pool.
115    pub(crate) fn record_job_queue_wait(&self, duration: Duration) {
116        self.job_queue_wait_duration_seconds.record(duration.as_secs_f64());
117    }
118
119    /// Record how long an outer job executed on this pool.
120    pub(crate) fn record_job_duration(&self, duration: Duration) {
121        self.job_duration_seconds.record(duration.as_secs_f64());
122    }
123}