Skip to content

Commit e98c2e4

Browse files
committed
feat(relay): wire loki tracing init and graceful shutdown
- Build the relay TracingConfig in try_into so --log-color and --loki-addresses / --loki-service flags reach the tracing layer. - Move pluto_tracing::init into relay::run, spawn the returned Loki BackgroundTask, and bound shutdown with a 3s flush window so buffered logs reach Loki on SIGTERM. - Tolerate TryInitError in run so multiple in-process tests can share an already-installed global subscriber.
1 parent 5fba40e commit e98c2e4

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

crates/cli/src/commands/relay.rs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::{
2-
commands::common::{ConsoleColor, LICENSE, build_console_tracing_config, parse_relay_addr},
2+
commands::common::{ConsoleColor, LICENSE, parse_relay_addr},
33
error::CliError,
44
};
55
use libp2p::multiaddr::Protocol;
66
use pluto_p2p::k1;
7-
use std::path::PathBuf;
7+
use std::{collections::HashMap, path::PathBuf};
88
use tokio_util::sync::CancellationToken;
99
use tracing::{error, info};
1010

@@ -60,7 +60,28 @@ impl TryInto<pluto_relay_server::config::Config> for RelayArgs {
6060
}
6161
};
6262

63-
let log_config = build_console_tracing_config(self.log.level.clone(), &self.log.color);
63+
let log_config = {
64+
let mut builder = pluto_tracing::TracingConfig::builder().with_default_console();
65+
66+
builder = match &self.log.color {
67+
ConsoleColor::Auto => builder.console_with_ansi(std::env::var("NO_COLOR").is_err()),
68+
ConsoleColor::Force => builder.console_with_ansi(true),
69+
ConsoleColor::Disable => builder.console_with_ansi(false),
70+
};
71+
72+
if let Some(loki_url) = self.loki.loki_addresses.first() {
73+
let mut labels = HashMap::new();
74+
labels.insert("service".to_string(), self.loki.loki_service.clone());
75+
76+
builder = builder.loki(pluto_tracing::LokiConfig {
77+
loki_url: loki_url.clone(),
78+
labels,
79+
extra_fields: HashMap::new(),
80+
});
81+
}
82+
83+
builder.override_env_filter(self.log.level.clone()).build()
84+
};
6485

6586
let builder = pluto_relay_server::config::Config::builder()
6687
.data_dir(self.data_dir.data_dir)
@@ -265,6 +286,18 @@ pub async fn run(
265286
config: pluto_relay_server::config::Config,
266287
ct: CancellationToken,
267288
) -> Result<(), CliError> {
289+
let loki_task = match pluto_tracing::init(&config.log_config) {
290+
Ok(Some(task)) => Some(tokio::spawn(task)),
291+
Ok(None) => None,
292+
Err(pluto_tracing::init::Error::InitError(_)) => {
293+
// A global tracing subscriber is already installed (e.g. when
294+
// running multiple tests in the same process). Continue with the
295+
// existing subscriber instead of failing.
296+
None
297+
}
298+
Err(err) => return Err(err.into()),
299+
};
300+
268301
info!("{LICENSE}");
269302
info!(config = ?config);
270303

@@ -291,10 +324,21 @@ pub async fn run(
291324
e => e,
292325
}?;
293326

294-
pluto_relay_server::p2p::run_relay_p2p_node(&config, key, ct)
327+
let result = pluto_relay_server::p2p::run_relay_p2p_node(&config, key, ct)
295328
.await
296329
.map(|_| ())
297-
.map_err(Into::into)
330+
.map_err(Into::into);
331+
332+
// Give the Loki worker a short window to flush buffered logs before the
333+
// runtime aborts it. The task holds a `Layer` clone through the global
334+
// subscriber and never completes on its own, so the wait is bounded.
335+
if let Some(handle) = loki_task {
336+
let abort_handle = handle.abort_handle();
337+
let _ = tokio::time::timeout(std::time::Duration::from_secs(3), handle).await;
338+
abort_handle.abort();
339+
}
340+
341+
result
298342
}
299343

300344
#[cfg(test)]

crates/cli/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ async fn run() -> std::result::Result<(), CliError> {
8383
}
8484
Commands::Relay(args) => {
8585
let config: pluto_relay_server::config::Config = (*args).clone().try_into()?;
86-
pluto_tracing::init(&config.log_config).expect("Failed to initialize tracing");
8786
commands::relay::run(config, ct).await
8887
}
8988
Commands::Alpha(args) => match args.command {

0 commit comments

Comments
 (0)