tcrm_task/helper/tracing.rs
1use std::future::Future;
2
3#[cfg(feature = "tracing")]
4use tracing::{Instrument, Level};
5
6/// Conditionally instruments futures with tracing spans.
7///
8/// This trait provides a unified interface for adding tracing instrumentation
9/// to async operations. When the "tracing" feature is enabled, futures are
10/// wrapped with debug-level spans. When disabled, futures are returned unchanged.
11pub trait MaybeInstrument: Future + Sized {
12 /// Conditionally instruments the future with a tracing span.
13 ///
14 /// # Arguments
15 ///
16 /// * `name` - Static name for the tracing span.
17 ///
18 /// # Returns
19 ///
20 /// When tracing is enabled: An instrumented future with a debug-level span.
21 /// When tracing is disabled: The original future unchanged.
22 #[cfg(feature = "tracing")]
23 fn maybe_instrument(self, name: &'static str) -> impl Future<Output = Self::Output> {
24 let span = tracing::span!(Level::DEBUG, "async_op", name = name);
25 self.instrument(span)
26 }
27
28 /// No-op implementation when tracing is disabled.
29 ///
30 /// # Arguments
31 ///
32 /// * `_name` - Ignored static name parameter.
33 ///
34 /// # Returns
35 ///
36 /// The original future unchanged.
37 #[cfg(not(feature = "tracing"))]
38 #[must_use]
39 fn maybe_instrument(self, _name: &'static str) -> Self {
40 self
41 }
42}
43
44/// Blanket implementation for all Future types.
45impl<F: Future> MaybeInstrument for F {}