@@ -31,6 +31,7 @@ use bitcoin::hashes::{Hash, HashEngine, HmacEngine};
3131use bitcoin::secp256k1::Secp256k1;
3232use bitcoin::secp256k1::{PublicKey, SecretKey};
3333use bitcoin::{secp256k1, Sequence, SignedAmount};
34+ use tracing::Span;
3435
3536use crate::blinded_path::message::{
3637 AsyncPaymentsContext, BlindedMessagePath, MessageForwardNode, OffersContext,
@@ -1889,6 +1890,36 @@ where
18891890 }
18901891}
18911892
1893+ thread_local! {
1894+ static CURRENT_INSTANCE: RefCell<Option<String>> = RefCell::new(None);
1895+ static CURRENT_SPAN: RefCell<Option<Span>> = RefCell::new(None);
1896+ }
1897+
1898+ fn enter_instance_span(name: String) -> tracing::span::Span {
1899+ let mut span_to_enter: Option<Span> = None;
1900+
1901+ CURRENT_INSTANCE.with(|current_name| {
1902+ let mut current_name = current_name.borrow_mut();
1903+
1904+ if current_name.as_deref() != Some(&name) {
1905+ // Start a new span and store it
1906+ let span = tracing::info_span!("node", name);
1907+ CURRENT_SPAN.with(|s| *s.borrow_mut() = Some(span.clone())); // clone is cheap
1908+ *current_name = Some(name);
1909+ span_to_enter = Some(span);
1910+ } else {
1911+ // Reuse existing span
1912+ CURRENT_SPAN.with(|s| {
1913+ if let Some(span) = s.borrow().as_ref() {
1914+ span_to_enter = Some(span.clone());
1915+ }
1916+ });
1917+ }
1918+ });
1919+
1920+ span_to_enter.unwrap()
1921+ }
1922+
18921923/// A lightning node's channel state machine and payment management logic, which facilitates
18931924/// sending, forwarding, and receiving payments through lightning channels.
18941925///
0 commit comments