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 /// Number of spawned regular blocking tasks
20 pub(crate) regular_blocking_tasks_total: Counter,
21 /// Number of finished spawned regular blocking tasks
22 pub(crate) finished_regular_blocking_tasks_total: Counter,
23}
24
25impl TaskExecutorMetrics {
26 /// Increments the counter for spawned critical tasks.
27 pub(crate) fn inc_critical_tasks(&self) {
28 self.critical_tasks_total.increment(1);
29 }
30
31 /// Increments the counter for spawned regular tasks.
32 pub(crate) fn inc_regular_tasks(&self) {
33 self.regular_tasks_total.increment(1);
34 }
35
36 /// Increments the counter for spawned regular blocking tasks.
37 pub(crate) fn inc_regular_blocking_tasks(&self) {
38 self.regular_blocking_tasks_total.increment(1);
39 }
40}
41
42/// Helper type for increasing counters even if a task fails
43pub struct IncCounterOnDrop(Counter);
44
45impl fmt::Debug for IncCounterOnDrop {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 f.debug_tuple("IncCounterOnDrop").finish()
48 }
49}
50
51impl IncCounterOnDrop {
52 /// Creates a new instance of `IncCounterOnDrop` with the given counter.
53 pub const fn new(counter: Counter) -> Self {
54 Self(counter)
55 }
56}
57
58impl Drop for IncCounterOnDrop {
59 /// Increment the counter when the instance is dropped.
60 fn drop(&mut self) {
61 self.0.increment(1);
62 }
63}