commonware-runtime 2026.4.0

Execute asynchronous tasks with a configurable scheduler.
Documentation
//! Macros shared across runtime implementations.

/// Prepare metrics for a spawned task.
///
/// Returns a `(Label, MetricHandle)` pair for tracking spawned tasks.
///
/// The `Label` identifies the task in the metrics registry and the
/// `MetricHandle` immediately increments the `tasks_running` gauge for that
/// label. Call `MetricHandle::finish` once the task completes to decrement the
/// gauge.
#[cfg(not(any(
    commonware_stability_GAMMA,
    commonware_stability_DELTA,
    commonware_stability_EPSILON,
    commonware_stability_RESERVED
)))] // BETA
#[macro_export]
macro_rules! spawn_metrics {
    // Handle future tasks
    ($ctx:ident) => {
        $crate::spawn_metrics!(
            $crate::telemetry::metrics::task::Label::task(
                $ctx.name.clone(),
                $ctx.execution,
            ),
            @make $ctx
        )
    };

    // Increment the number of spawned tasks and return a metrics tracker that
    // keeps the running tasks gauge accurate
    ($label:expr, @make $ctx:ident) => {{
        let label = $label;
        let metrics = $ctx.metrics();
        metrics.tasks_spawned.get_or_create(&label).inc();
        let metric = $crate::utils::MetricHandle::new(
            metrics.tasks_running.get_or_create(&label).clone(),
        );
        (label, metric)
    }};
}