Skip to main content

commonware_runtime/
macros.rs

1//! Macros shared across runtime implementations.
2
3/// Prepare metrics for a spawned task.
4///
5/// Returns a `(Label, MetricHandle)` pair for tracking spawned tasks.
6///
7/// The `Label` identifies the task in the metrics registry and the
8/// `MetricHandle` immediately increments the `tasks_running` gauge for that
9/// label. Call `MetricHandle::finish` once the task completes to decrement the
10/// gauge.
11#[cfg(not(any(
12    commonware_stability_GAMMA,
13    commonware_stability_DELTA,
14    commonware_stability_EPSILON,
15    commonware_stability_RESERVED
16)))] // BETA
17#[macro_export]
18macro_rules! spawn_metrics {
19    // Handle future tasks
20    ($ctx:ident) => {
21        $crate::spawn_metrics!(
22            $crate::telemetry::metrics::task::Label::task(
23                $ctx.name.clone(),
24                $ctx.execution,
25            ),
26            @make $ctx
27        )
28    };
29
30    // Increment the number of spawned tasks and return a metrics tracker that
31    // keeps the running tasks gauge accurate
32    ($label:expr, @make $ctx:ident) => {{
33        let label = $label;
34        let metrics = $ctx.metrics();
35        metrics.tasks_spawned.get_or_create(&label).inc();
36        let metric = $crate::utils::MetricHandle::new(
37            metrics.tasks_running.get_or_create(&label).clone(),
38        );
39        (label, metric)
40    }};
41}