Skip to content

Commit

Permalink
Merge pull request #11 from avnik/avnik/code-improvement
Browse files Browse the repository at this point in the history
Logging and minor code improvements
  • Loading branch information
mbssrc authored Aug 30, 2024
2 parents bd238ec + 46a389f commit 4e5a6d9
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 22 deletions.
58 changes: 55 additions & 3 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ tonic-types = {version="0.11.0"}
tonic-reflection = {version="0.11.0"}
tower = {version = "0.4"}
tracing = "0.1"
tracing-subscriber = {version = "0.3"}
tracing-subscriber = {version = "0.3", features = ["env-filter", "tracing-log", "time", "local-time"]}
tracing-journald = {version =" 0.2.0"}
serde = { version = "1.0.202", features = ["derive"]}
serde_json = "1.0.120"
x509-parser = { version = "0.16" }
Expand Down
5 changes: 5 additions & 0 deletions nixos/modules/admin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ in
{
options.givc.admin = {
enable = mkEnableOption "Enable givc-admin module.";
debug = mkEnableOption "Enable givc-admin debug logging.";

name = mkOption {
description = "Host name (without domain).";
Expand Down Expand Up @@ -134,6 +135,10 @@ in
"CA_CERT" = "${cfg.tls.caCertPath}";
"HOST_CERT" = "${cfg.tls.certPath}";
"HOST_KEY" = "${cfg.tls.keyPath}";
}
// attrsets.optionalAttrs cfg.debug {
"RUST_BACKTRACE" = "1";
"GIVC_LOG" = "debug";
};
};
networking.firewall.allowedTCPPorts =
Expand Down
1 change: 1 addition & 0 deletions nixos/tests/admin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ in
];
givc.admin = {
enable = true;
debug = true;
name = "admin-vm";
addr = addrs.adminvm;
port = "9001";
Expand Down
14 changes: 7 additions & 7 deletions src/admin/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Registry {
self.send_event(event)
}

pub fn deregister(&self, name: &String) -> anyhow::Result<()> {
pub fn deregister(&self, name: &str) -> anyhow::Result<()> {
let mut state = self.map.lock().unwrap();
match state.remove(name) {
Some(entry) => {
Expand All @@ -49,19 +49,19 @@ impl Registry {
}
}

pub fn by_name(&self, name: &String) -> anyhow::Result<RegistryEntry> {
pub fn by_name(&self, name: &str) -> anyhow::Result<RegistryEntry> {
let state = self.map.lock().unwrap();
state
.get(name)
.cloned()
.ok_or_else(|| anyhow!("Service {name} not registered"))
}

pub fn find_names(&self, name: &String) -> anyhow::Result<Vec<String>> {
pub fn find_names(&self, name: &str) -> anyhow::Result<Vec<String>> {
let state = self.map.lock().unwrap();
let list: Vec<String> = state
.keys()
.filter(|x| x.starts_with(name.as_str()))
.filter(|x| x.starts_with(name))
.cloned()
.collect();
if list.len() == 0 {
Expand All @@ -85,12 +85,12 @@ impl Registry {
}
}

pub fn contains(&self, name: &String) -> bool {
pub fn contains(&self, name: &str) -> bool {
let state = self.map.lock().unwrap();
state.contains_key(name)
}

pub fn create_unique_entry_name(&self, name: &String) -> String {
pub fn create_unique_entry_name(&self, name: &str) -> String {
let state = self.map.lock().unwrap();
let mut counter = 0;
loop {
Expand All @@ -107,7 +107,7 @@ impl Registry {
state.values().filter(|x| x.watch).cloned().collect()
}

pub fn update_state(&self, name: &String, status: UnitStatus) -> anyhow::Result<()> {
pub fn update_state(&self, name: &str, status: UnitStatus) -> anyhow::Result<()> {
let mut state = self.map.lock().unwrap();
state
.get_mut(name)
Expand Down
7 changes: 3 additions & 4 deletions src/admin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use tonic::{Code, Response, Status};
use tracing::{error, info};
use tracing::{debug, error, info};

pub use pb::admin_service_server::AdminServiceServer;

Expand Down Expand Up @@ -177,7 +177,7 @@ impl AdminServiceImpl {
async fn monitor_routine(&self) -> anyhow::Result<()> {
let watch_list = self.registry.watch_list();
for entry in watch_list {
info!("Monitoring {}...", &entry.name);
debug!("Monitoring {}...", &entry.name);
match self.get_remote_status(&entry).await {
Err(err) => {
error!(
Expand All @@ -199,7 +199,7 @@ impl AdminServiceImpl {
)
};

info!("Status of {} is {:#?} (updated)", &entry.name, status);
debug!("Status of {} is {:#?} (updated)", &entry.name, status);
// We have immutable copy of entry here, but need update _in registry_ copy
self.registry.update_state(&entry.name, status)?;

Expand All @@ -219,7 +219,6 @@ impl AdminServiceImpl {
watch.tick().await; // First tick fires instantly
loop {
watch.tick().await;
info!("Monitoring...");
if let Err(err) = self.monitor_routine().await {
error!("Error during watch: {}", err);
}
Expand Down
6 changes: 3 additions & 3 deletions src/bin/givc-admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use givc_common::pb::reflection::ADMIN_DESCRIPTOR;
use std::net::SocketAddr;
use std::path::PathBuf;
use tonic::transport::Server;
use tracing::info;
use tracing::debug;

#[derive(Debug, Parser)] // requires `derive` feature
#[command(name = "givc-admin")]
Expand Down Expand Up @@ -39,10 +39,10 @@ struct Cli {

#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
givc::trace_init();
givc::trace_init()?;

let cli = Cli::parse();
info!("CLI is {:#?}", cli);
debug!("CLI is {:#?}", cli);

let addr = SocketAddr::new(cli.addr.parse().unwrap(), cli.port);

Expand Down
2 changes: 1 addition & 1 deletion src/bin/givc-agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct Cli {

#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
givc::trace_init();
givc::trace_init()?;

let cli = Cli::parse();
info!("CLI is {:#?}", cli);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/givc-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async fn test_subcommands(test: Test, admin: AdminClient) -> anyhow::Result<()>

#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
givc::trace_init();
givc::trace_init()?;

let cli = Cli::parse();
info!("CLI is {:#?}", cli);
Expand Down
38 changes: 36 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@ pub mod pb {
pub use givc_client::endpoint;
pub use givc_common::types;

pub fn trace_init() {
tracing_subscriber::fmt::init();
pub fn trace_init() -> anyhow::Result<()> {
use std::env;
use tracing::Level;
use tracing_subscriber::{filter::LevelFilter, layer::SubscriberExt, EnvFilter, Layer};

let env_filter =
EnvFilter::try_from_env("GIVC_LOG").unwrap_or_else(|_| EnvFilter::from("info"));
let is_debug_log_level = env_filter
.max_level_hint()
.map_or_else(|| false, |level| level >= Level::DEBUG);

let stdout = tracing_subscriber::fmt::layer()
.with_target(is_debug_log_level)
.with_file(is_debug_log_level)
.with_line_number(is_debug_log_level)
.with_thread_ids(is_debug_log_level);

let stdout = if is_debug_log_level {
stdout.pretty().boxed()
} else {
stdout.boxed()
};

// enable journald logging only on release to avoid log spam on dev machines
let journald = match env::var("INVOCATION_ID") {
Err(_) => None,
Ok(_) => tracing_journald::layer().ok(),
};

let subscriber = tracing_subscriber::registry()
.with(journald.with_filter(LevelFilter::INFO))
.with(stdout.with_filter(env_filter));

tracing::subscriber::set_global_default(subscriber)
.expect("tracing shouldn't already have been set up");
Ok(())
}

0 comments on commit 4e5a6d9

Please sign in to comment.