fedimint_logging/
lib.rs

1#![deny(clippy::pedantic)]
2#![allow(clippy::missing_errors_doc)]
3#![allow(clippy::missing_panics_doc)]
4
5//! Constants for categorizing the logging type
6//!
7//! To help stabilize logging targets, avoid typos and improve consistency,
8//! it's preferable for logging statements use static target constants,
9//! that we define in this module.
10//!
11//! Core + server side components should use global namespace,
12//! while client should generally be prefixed with `client::`.
13//! This makes it easier to filter interesting calls when
14//! running e.g. `devimint`, that will run both server and client
15//! side.
16
17use std::fs::File;
18use std::{env, io};
19
20use tracing_subscriber::layer::SubscriberExt;
21use tracing_subscriber::util::SubscriberInitExt;
22use tracing_subscriber::{EnvFilter, Layer};
23
24pub const LOG_CONSENSUS: &str = "fm::consensus";
25pub const LOG_CORE: &str = "fm::core";
26pub const LOG_SERVER: &str = "fm::server";
27pub const LOG_DB: &str = "fm::db";
28pub const LOG_DEVIMINT: &str = "fm::devimint";
29pub const LOG_NET: &str = "fm::net";
30pub const LOG_NET_IROH: &str = "fm::net::iroh";
31pub const LOG_NET_WS: &str = "fm::net::ws";
32pub const LOG_NET_API: &str = "fm::net::api";
33pub const LOG_NET_PEER_DKG: &str = "fm::net::peer::dkg";
34pub const LOG_NET_PEER: &str = "fm::net::peer";
35pub const LOG_NET_AUTH: &str = "fm::net::auth";
36pub const LOG_TASK: &str = "fm::task";
37pub const LOG_RUNTIME: &str = "fm::runtime";
38pub const LOG_TEST: &str = "fm::test";
39pub const LOG_TIMING: &str = "fm::timing";
40pub const LOG_CLIENT: &str = "fm::client";
41pub const LOG_CLIENT_DB: &str = "fm::client::db";
42pub const LOG_CLIENT_EVENT_LOG: &str = "fm::client::event-log";
43pub const LOG_MODULE_MINT: &str = "fm::module::mint";
44pub const LOG_MODULE_META: &str = "fm::module::meta";
45pub const LOG_MODULE_WALLET: &str = "fm::module::wallet";
46pub const LOG_MODULE_LN: &str = "fm::module::ln";
47pub const LOG_MODULE_LNV2: &str = "fm::module::lnv2";
48pub const LOG_CLIENT_REACTOR: &str = "fm::client::reactor";
49pub const LOG_CLIENT_NET: &str = "fm::client::net";
50pub const LOG_CLIENT_NET_API: &str = "fm::client::net::api";
51pub const LOG_CLIENT_BACKUP: &str = "fm::client::backup";
52pub const LOG_CLIENT_RECOVERY: &str = "fm::client::recovery";
53pub const LOG_CLIENT_RECOVERY_MINT: &str = "fm::client::recovery::mint";
54pub const LOG_CLIENT_MODULE_MINT: &str = "fm::client::module::mint";
55pub const LOG_CLIENT_MODULE_META: &str = "fm::client::module::meta";
56pub const LOG_CLIENT_MODULE_LN: &str = "fm::client::module::ln";
57pub const LOG_CLIENT_MODULE_LNV2: &str = "fm::client::module::lnv2";
58pub const LOG_CLIENT_MODULE_WALLET: &str = "fm::client::module::wallet";
59pub const LOG_CLIENT_MODULE_GW: &str = "fm::client::module::gw";
60pub const LOG_CLIENT_MODULE_GWV2: &str = "fm::client::module::gwv2";
61pub const LOG_GATEWAY: &str = "fm::gw";
62pub const LOG_GATEWAY_UI: &str = "fm::gw::ui";
63pub const LOG_LIGHTNING: &str = "fm::gw::lightning";
64pub const LOG_BITCOIND_ESPLORA: &str = "fm::bitcoind::esplora";
65pub const LOG_BITCOIND_CORE: &str = "fm::bitcoind::bitcoincore";
66pub const LOG_BITCOIND: &str = "fm::bitcoind";
67pub const LOG_BITCOIN: &str = "fm::bitcoin";
68
69/// Global tracer provider for proper shutdown
70#[cfg(feature = "telemetry")]
71static TRACER_PROVIDER: std::sync::OnceLock<opentelemetry_sdk::trace::SdkTracerProvider> =
72    std::sync::OnceLock::new();
73
74/// Consolidates the setup of server tracing into a helper
75#[derive(Default)]
76pub struct TracingSetup {
77    base_level: Option<String>,
78    extra_directives: Option<String>,
79    #[cfg(feature = "telemetry")]
80    tokio_console_bind: Option<std::net::SocketAddr>,
81    #[cfg(feature = "telemetry")]
82    with_jaeger: bool,
83    with_file: Option<File>,
84}
85
86impl TracingSetup {
87    /// Setup a console server for tokio logging <https://docs.rs/console-subscriber>
88    #[cfg(feature = "telemetry")]
89    pub fn tokio_console_bind(&mut self, address: Option<std::net::SocketAddr>) -> &mut Self {
90        self.tokio_console_bind = address;
91        self
92    }
93
94    /// Setup telemetry export via OTLP (OpenTelemetry Protocol).
95    ///
96    /// This uses the OTLP exporter which is compatible with Jaeger (since
97    /// v1.35), the OpenTelemetry Collector, and many other observability
98    /// backends.
99    ///
100    /// Configure the endpoint with `OTEL_EXPORTER_OTLP_ENDPOINT` environment
101    /// variable (defaults to `http://localhost:4317` for gRPC).
102    ///
103    /// To use with Jaeger:
104    /// ```bash
105    /// docker run -d -p4317:4317 -p16686:16686 jaegertracing/all-in-one:latest
106    /// ```
107    #[cfg(feature = "telemetry")]
108    pub fn with_jaeger(&mut self, enabled: bool) -> &mut Self {
109        self.with_jaeger = enabled;
110        self
111    }
112
113    pub fn with_file(&mut self, file: Option<File>) -> &mut Self {
114        self.with_file = file;
115        self
116    }
117
118    /// Sets the log level applied to most modules. Some overly chatty modules
119    /// are muted even if this is set to a lower log level, use the `RUST_LOG`
120    /// environment variable to override.
121    pub fn with_base_level(&mut self, level: impl Into<String>) -> &mut Self {
122        self.base_level = Some(level.into());
123        self
124    }
125
126    /// Add a filter directive.
127    pub fn with_directive(&mut self, directive: &str) -> &mut Self {
128        if let Some(old) = self.extra_directives.as_mut() {
129            *old = format!("{old},{directive}");
130        } else {
131            self.extra_directives = Some(directive.to_owned());
132        }
133        self
134    }
135
136    /// Initialize the logging, must be called for tracing to begin
137    pub fn init(&mut self) -> anyhow::Result<()> {
138        use tracing_subscriber::fmt::writer::{BoxMakeWriter, Tee};
139
140        let var = env::var(tracing_subscriber::EnvFilter::DEFAULT_ENV).unwrap_or_default();
141        let filter_layer = EnvFilter::builder().parse(format!(
142            // We prefix everything with a default general log level and
143            // good per-module specific default. User provided RUST_LOG
144            // can override one or both
145            "{},{},{},{},{},{},{},{},{}",
146            self.base_level.as_deref().unwrap_or("info"),
147            "jsonrpsee_core::client::async_client=off",
148            "hyper=off",
149            "h2=off",
150            "jsonrpsee_server=warn,jsonrpsee_server::transport=off",
151            "AlephBFT-=error",
152            "iroh=error",
153            var,
154            self.extra_directives.as_deref().unwrap_or(""),
155        ))?;
156
157        let fmt_writer = match self.with_file.take() {
158            Some(file) => BoxMakeWriter::new(Tee::new(io::stderr, file)),
159            _ => BoxMakeWriter::new(io::stderr),
160        };
161
162        let fmt_layer = tracing_subscriber::fmt::layer()
163            .with_thread_names(false) // can be enabled for debugging
164            .with_writer(fmt_writer)
165            .with_filter(filter_layer);
166
167        let console_opt = || -> Option<Box<dyn Layer<_> + Send + Sync + 'static>> {
168            #[cfg(feature = "telemetry")]
169            if let Some(l) = self.tokio_console_bind {
170                let tracer = console_subscriber::ConsoleLayer::builder()
171                    .retention(std::time::Duration::from_secs(60))
172                    .server_addr(l)
173                    .spawn()
174                    // tokio-console cares only about these layers, so we filter separately for it
175                    .with_filter(EnvFilter::new("tokio=trace,runtime=trace"));
176                return Some(tracer.boxed());
177            }
178            None
179        };
180
181        let telemetry_layer_opt = || -> Option<Box<dyn Layer<_> + Send + Sync + 'static>> {
182            #[cfg(feature = "telemetry")]
183            if self.with_jaeger {
184                use opentelemetry::trace::TracerProvider as _;
185
186                // Create OTLP exporter using gRPC (tonic)
187                // Jaeger now supports OTLP natively, so we use OTLP instead of the deprecated
188                // Jaeger exporter. Configure with OTEL_EXPORTER_OTLP_ENDPOINT env var
189                // (defaults to http://localhost:4317)
190                let exporter = match opentelemetry_otlp::SpanExporter::builder()
191                    .with_tonic()
192                    .build()
193                {
194                    Ok(exporter) => exporter,
195                    Err(e) => {
196                        eprintln!(
197                            "Failed to create OTLP span exporter, continuing without telemetry: {e}"
198                        );
199                        return None;
200                    }
201                };
202
203                // Build the tracer provider with the OTLP exporter
204                let tracer_provider = opentelemetry_sdk::trace::SdkTracerProvider::builder()
205                    .with_batch_exporter(exporter)
206                    .with_resource(
207                        opentelemetry_sdk::Resource::builder()
208                            .with_service_name("fedimint")
209                            .build(),
210                    )
211                    .build();
212
213                // Store provider for shutdown and set as global
214                let _ = TRACER_PROVIDER.set(tracer_provider.clone());
215                opentelemetry::global::set_tracer_provider(tracer_provider.clone());
216
217                let tracer = tracer_provider.tracer("fedimint");
218
219                return Some(tracing_opentelemetry::layer().with_tracer(tracer).boxed());
220            }
221            None
222        };
223
224        tracing_subscriber::registry()
225            .with(fmt_layer)
226            .with(console_opt())
227            .with(telemetry_layer_opt())
228            .try_init()?;
229        Ok(())
230    }
231}
232
233pub fn shutdown() {
234    #[cfg(feature = "telemetry")]
235    if let Some(provider) = TRACER_PROVIDER.get()
236        && let Err(e) = provider.shutdown()
237    {
238        eprintln!("Error shutting down tracer provider: {e}");
239    }
240}