1#![deny(clippy::pedantic)]
2#![allow(clippy::missing_errors_doc)]
3#![allow(clippy::missing_panics_doc)]
4
5use 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_WALLETV2: &str = "fm::module::walletv2";
47pub const LOG_MODULE_LN: &str = "fm::module::ln";
48pub const LOG_MODULE_LNV2: &str = "fm::module::lnv2";
49pub const LOG_CLIENT_REACTOR: &str = "fm::client::reactor";
50pub const LOG_CLIENT_NET: &str = "fm::client::net";
51pub const LOG_CLIENT_NET_API: &str = "fm::client::net::api";
52pub const LOG_CLIENT_BACKUP: &str = "fm::client::backup";
53pub const LOG_CLIENT_RECOVERY: &str = "fm::client::recovery";
54pub const LOG_CLIENT_RECOVERY_MINT: &str = "fm::client::recovery::mint";
55pub const LOG_CLIENT_MODULE_MINT: &str = "fm::client::module::mint";
56pub const LOG_CLIENT_MODULE_META: &str = "fm::client::module::meta";
57pub const LOG_CLIENT_MODULE_LN: &str = "fm::client::module::ln";
58pub const LOG_CLIENT_MODULE_LNV2: &str = "fm::client::module::lnv2";
59pub const LOG_CLIENT_MODULE_WALLET: &str = "fm::client::module::wallet";
60pub const LOG_CLIENT_MODULE_WALLETV2: &str = "fm::client::module::walletv2";
61pub const LOG_CLIENT_MODULE_GW: &str = "fm::client::module::gw";
62pub const LOG_CLIENT_MODULE_GWV2: &str = "fm::client::module::gwv2";
63pub const LOG_GATEWAY: &str = "fm::gw";
64pub const LOG_GATEWAY_UI: &str = "fm::gw::ui";
65pub const LOG_LIGHTNING: &str = "fm::gw::lightning";
66pub const LOG_BITCOIND_ESPLORA: &str = "fm::bitcoind::esplora";
67pub const LOG_BITCOIND_CORE: &str = "fm::bitcoind::bitcoincore";
68pub const LOG_BITCOIND: &str = "fm::bitcoind";
69pub const LOG_BITCOIN: &str = "fm::bitcoin";
70
71#[cfg(feature = "telemetry")]
73static TRACER_PROVIDER: std::sync::OnceLock<opentelemetry_sdk::trace::SdkTracerProvider> =
74 std::sync::OnceLock::new();
75
76#[derive(Default)]
78pub struct TracingSetup {
79 base_level: Option<String>,
80 extra_directives: Option<String>,
81 #[cfg(feature = "telemetry")]
82 tokio_console_bind: Option<std::net::SocketAddr>,
83 #[cfg(feature = "telemetry")]
84 with_jaeger: bool,
85 with_file: Option<File>,
86}
87
88impl TracingSetup {
89 #[cfg(feature = "telemetry")]
91 pub fn tokio_console_bind(&mut self, address: Option<std::net::SocketAddr>) -> &mut Self {
92 self.tokio_console_bind = address;
93 self
94 }
95
96 #[cfg(feature = "telemetry")]
110 pub fn with_jaeger(&mut self, enabled: bool) -> &mut Self {
111 self.with_jaeger = enabled;
112 self
113 }
114
115 pub fn with_file(&mut self, file: Option<File>) -> &mut Self {
116 self.with_file = file;
117 self
118 }
119
120 pub fn with_base_level(&mut self, level: impl Into<String>) -> &mut Self {
124 self.base_level = Some(level.into());
125 self
126 }
127
128 pub fn with_directive(&mut self, directive: &str) -> &mut Self {
130 if let Some(old) = self.extra_directives.as_mut() {
131 *old = format!("{old},{directive}");
132 } else {
133 self.extra_directives = Some(directive.to_owned());
134 }
135 self
136 }
137
138 pub fn init(&mut self) -> anyhow::Result<()> {
140 use tracing_subscriber::fmt::writer::{BoxMakeWriter, Tee};
141
142 let var = env::var(tracing_subscriber::EnvFilter::DEFAULT_ENV).unwrap_or_default();
143 let filter_layer = EnvFilter::builder().parse(format!(
144 "{},{},{},{},{},{},{},{},{}",
148 self.base_level.as_deref().unwrap_or("info"),
149 "jsonrpsee_core::client::async_client=off",
150 "hyper=off",
151 "h2=off",
152 "jsonrpsee_server=warn,jsonrpsee_server::transport=off",
153 "AlephBFT-=error",
154 "iroh=error",
155 var,
156 self.extra_directives.as_deref().unwrap_or(""),
157 ))?;
158
159 let fmt_writer = match self.with_file.take() {
160 Some(file) => BoxMakeWriter::new(Tee::new(io::stderr, file)),
161 _ => BoxMakeWriter::new(io::stderr),
162 };
163
164 let fmt_layer = tracing_subscriber::fmt::layer()
165 .with_thread_names(false) .with_writer(fmt_writer)
167 .with_filter(filter_layer);
168
169 let console_opt = || -> Option<Box<dyn Layer<_> + Send + Sync + 'static>> {
170 #[cfg(feature = "telemetry")]
171 if let Some(l) = self.tokio_console_bind {
172 let tracer = console_subscriber::ConsoleLayer::builder()
173 .retention(std::time::Duration::from_mins(1))
174 .server_addr(l)
175 .spawn()
176 .with_filter(EnvFilter::new("tokio=trace,runtime=trace"));
178 return Some(tracer.boxed());
179 }
180 None
181 };
182
183 let telemetry_layer_opt = || -> Option<Box<dyn Layer<_> + Send + Sync + 'static>> {
184 #[cfg(feature = "telemetry")]
185 if self.with_jaeger {
186 use opentelemetry::trace::TracerProvider as _;
187
188 let exporter = match opentelemetry_otlp::SpanExporter::builder()
193 .with_tonic()
194 .build()
195 {
196 Ok(exporter) => exporter,
197 Err(e) => {
198 eprintln!(
199 "Failed to create OTLP span exporter, continuing without telemetry: {e}"
200 );
201 return None;
202 }
203 };
204
205 let tracer_provider = opentelemetry_sdk::trace::SdkTracerProvider::builder()
207 .with_batch_exporter(exporter)
208 .with_resource(
209 opentelemetry_sdk::Resource::builder()
210 .with_service_name("fedimint")
211 .build(),
212 )
213 .build();
214
215 let _ = TRACER_PROVIDER.set(tracer_provider.clone());
217 opentelemetry::global::set_tracer_provider(tracer_provider.clone());
218
219 let tracer = tracer_provider.tracer("fedimint");
220
221 return Some(tracing_opentelemetry::layer().with_tracer(tracer).boxed());
222 }
223 None
224 };
225
226 tracing_subscriber::registry()
227 .with(fmt_layer)
228 .with(console_opt())
229 .with(telemetry_layer_opt())
230 .try_init()?;
231 Ok(())
232 }
233}
234
235pub fn shutdown() {
236 #[cfg(feature = "telemetry")]
237 if let Some(provider) = TRACER_PROVIDER.get()
238 && let Err(e) = provider.shutdown()
239 {
240 eprintln!("Error shutting down tracer provider: {e}");
241 }
242}