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

Add proper tracing to registry resource #555

Merged
merged 4 commits into from
Sep 20, 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
197 changes: 197 additions & 0 deletions registry/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ schemars = "0.8.0"
serde = "1.0.130"
serde_json = "1.0.68"
thiserror = "1.0.30"
tracing = { version = "0.1.37" }
tracing-subscriber = { version = "0.3.17", features = ["ansi", "env-filter", "json"] }
utfx = "0.1.0"

[build-dependencies]
Expand Down
46 changes: 35 additions & 11 deletions registry/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use clap::Parser;
use registry_helper::RegistryHelper;
use schemars::schema_for;
use std::process::exit;

use tracing::{debug, error};
use tracing_subscriber::{filter::LevelFilter, prelude::__tracing_subscriber_SubscriberExt, EnvFilter, Layer};
use crate::config::Registry;

mod args;
Expand All @@ -28,27 +29,30 @@ fn main() {
#[cfg(debug_assertions)]
check_debug();

enable_tracing();

let args = Arguments::parse();
match args.subcommand {
args::SubCommand::Query { key_path, value_name, recurse } => {
eprintln!("Get key_path: {key_path}, value_name: {value_name:?}, recurse: {recurse}");
debug!("Get key_path: {key_path}, value_name: {value_name:?}, recurse: {recurse}");
},
args::SubCommand::Set { key_path, value } => {
eprintln!("Set key_path: {key_path}, value: {value}");
debug!("Set key_path: {key_path}, value: {value}");
},
args::SubCommand::Remove { key_path, value_name, recurse } => {
eprintln!("Remove key_path: {key_path}, value_name: {value_name:?}, recurse: {recurse}");
debug!("Remove key_path: {key_path}, value_name: {value_name:?}, recurse: {recurse}");
},
args::SubCommand::Find { key_path, find, recurse, keys_only, values_only } => {
eprintln!("Find key_path: {key_path}, find: {find}, recurse: {recurse:?}, keys_only: {keys_only:?}, values_only: {values_only:?}");
debug!("Find key_path: {key_path}, find: {find}, recurse: {recurse:?}, keys_only: {keys_only:?}, values_only: {values_only:?}");
},
args::SubCommand::Config { subcommand } => {
match subcommand {
args::ConfigSubCommand::Get{input} => {
debug!("Get input: {input}");
let reg_helper = match RegistryHelper::new(&input) {
Ok(reg_helper) => reg_helper,
Err(err) => {
eprintln!("Error: {err}");
error!("{err}");
exit(EXIT_INVALID_INPUT);
}
};
Expand All @@ -58,16 +62,17 @@ fn main() {
println!("{json}");
},
Err(err) => {
eprintln!("Error: {err}");
error!("{err}");
exit(EXIT_REGISTRY_ERROR);
}
}
},
args::ConfigSubCommand::Set{input, what_if} => {
debug!("Set input: {input}, what_if: {what_if}");
let mut reg_helper = match RegistryHelper::new(&input) {
Ok(reg_helper) => reg_helper,
Err(err) => {
eprintln!("Error: {err}");
error!("{err}");
exit(EXIT_INVALID_INPUT);
}
};
Expand All @@ -82,23 +87,24 @@ fn main() {
}
},
Err(err) => {
eprintln!("Error: {err}");
error!("{err}");
exit(EXIT_REGISTRY_ERROR);
}
}
},
args::ConfigSubCommand::Delete{input} => {
debug!("Delete input: {input}");
let reg_helper = match RegistryHelper::new(&input) {
Ok(reg_helper) => reg_helper,
Err(err) => {
eprintln!("Error: {err}");
error!("{err}");
exit(EXIT_INVALID_INPUT);
}
};
match reg_helper.remove() {
Ok(()) => {},
Err(err) => {
eprintln!("Error: {err}");
error!("{err}");
exit(EXIT_REGISTRY_ERROR);
}
}
Expand All @@ -115,6 +121,24 @@ fn main() {
exit(EXIT_SUCCESS);
}

pub fn enable_tracing() {
// default filter to trace level
let filter = EnvFilter::builder().with_default_directive(LevelFilter::TRACE.into()).parse("").unwrap_or_default();
let layer = tracing_subscriber::fmt::Layer::default().with_writer(std::io::stderr);
let fmt = layer
.with_ansi(false)
.with_level(true)
.with_line_number(true)
.json()
.boxed();

let subscriber = tracing_subscriber::Registry::default().with(fmt).with(filter);

if tracing::subscriber::set_global_default(subscriber).is_err() {
eprintln!("Unable to set global default tracing subscriber. Tracing is diabled.");
}
}

#[cfg(debug_assertions)]
fn check_debug() {
if env::var("DEBUG_REGISTRY").is_ok() {
Expand Down
Loading
Loading