Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement file-based logging in daemon and coordinator #549

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions binaries/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,24 @@ fn run() -> eyre::Result<()> {
let args = Args::parse();

#[cfg(feature = "tracing")]
match args.command {
Command::Daemon { quiet, .. } => {
set_up_tracing_opts("dora-daemon", !quiet)
match &args.command {
Command::Daemon {
quiet, machine_id, ..
} => {
let name = "dora-daemon";
let filename = machine_id
.as_ref()
.map(|id| format!("{name}-{id}"))
.unwrap_or(name.to_string());
set_up_tracing_opts(name, !quiet, Some(&filename))
.context("failed to set up tracing subscriber")?;
}
Command::Runtime => {
// Do not set the runtime in the cli.
}
Command::Coordinator { quiet, .. } => {
set_up_tracing_opts("dora-coordinator", !quiet)
let name = "dora-coordinator";
set_up_tracing_opts(name, !quiet, Some(name))
.context("failed to set up tracing subscriber")?;
}
_ => {
Expand Down
61 changes: 38 additions & 23 deletions libraries/extensions/telemetry/tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//! This module init a tracing propagator for Rust code that requires tracing, and is
//! able to serialize and deserialize context that has been sent via the middleware.

use std::path::Path;

use eyre::Context as EyreContext;
use tracing::metadata::LevelFilter;
use tracing_subscriber::{
Expand All @@ -14,37 +16,50 @@ use tracing_subscriber::Registry;
pub mod telemetry;

pub fn set_up_tracing(name: &str) -> eyre::Result<()> {
set_up_tracing_opts(name, true)
set_up_tracing_opts(name, true, None)
}

pub fn set_up_tracing_opts(name: &str, stdout: bool) -> eyre::Result<()> {
let stdout_filter = if stdout {
LevelFilter::TRACE
} else {
LevelFilter::OFF
};
// Filter log using `RUST_LOG`. More useful for CLI.
let env_filter = EnvFilter::from_default_env().or(LevelFilter::WARN);
let stdout_log = tracing_subscriber::fmt::layer()
.pretty()
.with_filter(stdout_filter)
.with_filter(env_filter);

let registry = Registry::default().with(stdout_log);
pub fn set_up_tracing_opts(name: &str, stdout: bool, filename: Option<&str>) -> eyre::Result<()> {
let mut layers = Vec::new();

if stdout {
// Filter log using `RUST_LOG`. More useful for CLI.
let env_filter = EnvFilter::from_default_env().or(LevelFilter::WARN);
let layer = tracing_subscriber::fmt::layer()
.compact()
.with_filter(env_filter);
layers.push(layer.boxed());
}

if let Some(filename) = filename {
let out_dir = Path::new("out");
std::fs::create_dir_all(out_dir).context("failed to create `out` directory")?;
let path = out_dir.join(filename).with_extension("txt");
let file = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)
.context("failed to create log file")?;
// Filter log using `RUST_LOG`. More useful for CLI.
let layer = tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_writer(file)
.with_filter(LevelFilter::INFO);
layers.push(layer.boxed());
}

if let Some(endpoint) = std::env::var_os("DORA_JAEGER_TRACING") {
let endpoint = endpoint
.to_str()
.wrap_err("Could not parse env variable: DORA_JAEGER_TRACING")?;
let tracer = crate::telemetry::init_jaeger_tracing(name, endpoint)
.wrap_err("Could not instantiate tracing")?;
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
let subscriber = registry.with(telemetry);
tracing::subscriber::set_global_default(subscriber).context(format!(
"failed to set tracing global subscriber for {name}"
))
} else {
tracing::subscriber::set_global_default(registry).context(format!(
"failed to set tracing global subscriber for {name}"
))
layers.push(telemetry.boxed());
}

let registry = Registry::default().with(layers);
tracing::subscriber::set_global_default(registry).context(format!(
"failed to set tracing global subscriber for {name}"
))
}
Loading