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#[macro_export]
12macro_rules! spawn_metrics {
13    // Handle future tasks
14    ($ctx:ident) => {
15        $crate::spawn_metrics!(
16            $crate::telemetry::metrics::task::Label::task(
17                $ctx.name.clone(),
18                $ctx.execution,
19            ),
20            @make $ctx
21        )
22    };
23
24    // Increment the number of spawned tasks and return a metrics tracker that
25    // keeps the running tasks gauge accurate
26    ($label:expr, @make $ctx:ident) => {{
27        let label = $label;
28        let metrics = $ctx.metrics();
29        metrics.tasks_spawned.get_or_create(&label).inc();
30        let metric = $crate::utils::MetricHandle::new(
31            metrics.tasks_running.get_or_create(&label).clone(),
32        );
33        (label, metric)
34    }};
35}