Skip to content

Commit

Permalink
Merge pull request #2013 from input-output-hk/djo/1981/enhance_logs_i…
Browse files Browse the repository at this point in the history
…n_client

Enhance logs in client and client-cli
  • Loading branch information
Alenar authored Oct 17, 2024
2 parents 7f3f4e5 + 99bf8c3 commit a3bbf33
Show file tree
Hide file tree
Showing 23 changed files with 119 additions and 88 deletions.
6 changes: 2 additions & 4 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions mithril-client-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-client-cli"
version = "0.9.16"
version = "0.9.17"
description = "A Mithril Client"
authors = { workspace = true }
edition = { workspace = true }
Expand Down Expand Up @@ -44,7 +44,6 @@ slog = { version = "2.7.0", features = [
] }
slog-async = "2.8.0"
slog-bunyan = "2.5.0"
slog-scope = "4.4.0"
slog-term = "2.9.1"
thiserror = "1.0.64"
tokio = { version = "1.40.0", features = ["full"] }
Expand Down
29 changes: 23 additions & 6 deletions mithril-client-cli/src/commands/cardano_db/download.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, Context};
use chrono::Utc;
use clap::Parser;
use slog_scope::{debug, warn};
use slog::{debug, warn, Logger};
use std::{
collections::HashMap,
fs::File,
Expand Down Expand Up @@ -55,6 +55,7 @@ impl CardanoDbDownloadCommand {
let params = context.config_parameters()?.add_source(self)?;
let download_dir: &String = &params.require("download_dir")?;
let db_dir = Path::new(download_dir).join("db");
let logger = context.logger();

let progress_output_type = if self.is_json_output_enabled() {
ProgressOutputType::JsonReporter
Expand All @@ -65,7 +66,9 @@ impl CardanoDbDownloadCommand {
let client = client_builder(&params)?
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
progress_output_type,
logger.clone(),
)))
.with_logger(logger.clone())
.build()?;

let get_list_of_artifact_ids = || async {
Expand Down Expand Up @@ -99,6 +102,7 @@ impl CardanoDbDownloadCommand {
.await?;

Self::download_and_unpack_cardano_db(
logger,
3,
&progress_printer,
&client,
Expand All @@ -117,6 +121,7 @@ impl CardanoDbDownloadCommand {
Self::compute_cardano_db_message(4, &progress_printer, &certificate, &db_dir).await?;

Self::verify_cardano_db_signature(
logger,
5,
&progress_printer,
&certificate,
Expand Down Expand Up @@ -181,6 +186,7 @@ impl CardanoDbDownloadCommand {
}

async fn download_and_unpack_cardano_db(
logger: &Logger,
step_number: u16,
progress_printer: &ProgressPrinter,
client: &Client,
Expand All @@ -196,14 +202,17 @@ impl CardanoDbDownloadCommand {
// The cardano db download does not fail if the statistic call fails.
// It would be nice to implement tests to verify the behavior of `add_statistics`
if let Err(e) = client.snapshot().add_statistics(cardano_db).await {
warn!("Could not increment cardano db download statistics: {e:?}");
warn!(
logger, "Could not increment cardano db download statistics";
"error" => ?e
);
}

// Append 'clean' file to speedup node bootstrap
if let Err(error) = File::create(db_dir.join("clean")) {
warn!(
"Could not create clean shutdown marker file in directory {}: {error}",
db_dir.display()
logger, "Could not create clean shutdown marker file in directory '{}'", db_dir.display();
"error" => error.to_string()
);
};

Expand Down Expand Up @@ -233,6 +242,7 @@ impl CardanoDbDownloadCommand {
}

async fn verify_cardano_db_signature(
logger: &Logger,
step_number: u16,
progress_printer: &ProgressPrinter,
certificate: &MithrilCertificate,
Expand All @@ -242,10 +252,16 @@ impl CardanoDbDownloadCommand {
) -> MithrilResult<()> {
progress_printer.report_step(step_number, "Verifying the cardano db signature…")?;
if !certificate.match_message(message) {
debug!("Digest verification failed, removing unpacked files & directory.");
debug!(
logger,
"Digest verification failed, removing unpacked files & directory."
);

if let Err(error) = std::fs::remove_dir_all(db_dir) {
warn!("Error while removing unpacked files & directory: {error}.");
warn!(
logger, "Error while removing unpacked files & directory";
"error" => error.to_string()
);
}

return Err(anyhow!(
Expand Down Expand Up @@ -389,6 +405,7 @@ mod tests {
);

let result = CardanoDbDownloadCommand::verify_cardano_db_signature(
&Logger::root(slog::Discard, slog::o!()),
1,
&progress_printer,
&certificate,
Expand Down
4 changes: 3 additions & 1 deletion mithril-client-cli/src/commands/cardano_db/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ impl CardanoDbListCommand {
/// Main command execution
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
let params = context.config_parameters()?;
let client = client_builder_with_fallback_genesis_key(&params)?.build()?;
let client = client_builder_with_fallback_genesis_key(&params)?
.with_logger(context.logger().clone())
.build()?;
let items = client.snapshot().list().await?;

if self.is_json_output_enabled() {
Expand Down
4 changes: 3 additions & 1 deletion mithril-client-cli/src/commands/cardano_db/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ impl CardanoDbShowCommand {
/// Cardano DB Show command
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
let params = context.config_parameters()?;
let client = client_builder_with_fallback_genesis_key(&params)?.build()?;
let client = client_builder_with_fallback_genesis_key(&params)?
.with_logger(context.logger().clone())
.build()?;

let get_list_of_artifact_ids = || async {
let cardano_dbs = client.snapshot().list().await.with_context(|| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl CardanoStakeDistributionDownloadCommand {
let params = context.config_parameters()?.add_source(self)?;
let download_dir = params.get_or("download_dir", ".");
let download_dir = Path::new(&download_dir);
let logger = context.logger();

let progress_output_type = if self.is_json_output_enabled() {
ProgressOutputType::JsonReporter
Expand All @@ -59,7 +60,9 @@ impl CardanoStakeDistributionDownloadCommand {
let client = client_builder(&params)?
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
progress_output_type,
logger.clone(),
)))
.with_logger(logger.clone())
.build()?;

progress_printer.report_step(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ impl CardanoStakeDistributionListCommand {
/// Main command execution
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
let params = context.config_parameters()?;
let client = client_builder_with_fallback_genesis_key(&params)?.build()?;
let client = client_builder_with_fallback_genesis_key(&params)?
.with_logger(context.logger().clone())
.build()?;
let lines = client.cardano_stake_distribution().list().await?;

if self.is_json_output_enabled() {
Expand Down
10 changes: 5 additions & 5 deletions mithril-client-cli/src/commands/cardano_transaction/certify.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, Context};
use clap::Parser;
use cli_table::{print_stdout, Cell, Table};
use slog_scope::debug;
use slog::debug;
use std::{collections::HashMap, sync::Arc};

use mithril_client::{
Expand Down Expand Up @@ -40,6 +40,7 @@ impl CardanoTransactionsCertifyCommand {
/// Cardano transaction certify command
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
let params = context.config_parameters()?.add_source(self)?;
let logger = context.logger();

let progress_output_type = if self.is_json_output_enabled() {
ProgressOutputType::JsonReporter
Expand All @@ -50,7 +51,9 @@ impl CardanoTransactionsCertifyCommand {
let client = client_builder(&params)?
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
progress_output_type,
logger.clone(),
)))
.with_logger(logger.clone())
.build()?;

progress_printer.report_step(1, "Fetching a proof for the given transactions…")?;
Expand All @@ -64,10 +67,7 @@ impl CardanoTransactionsCertifyCommand {
self.transactions_hashes
)
})?;
debug!(
"Got Proof from aggregator, proof: {:?}",
cardano_transaction_proof
);
debug!(logger, "Got Proof from aggregator"; "proof" => ?cardano_transaction_proof);

let verified_transactions =
Self::verify_proof_validity(2, &progress_printer, &cardano_transaction_proof)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ impl CardanoTransactionSnapshotListCommand {
/// Main command execution
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
let params = context.config_parameters()?;
let client = client_builder_with_fallback_genesis_key(&params)?.build()?;
let client = client_builder_with_fallback_genesis_key(&params)?
.with_logger(context.logger().clone())
.build()?;
let lines = client.cardano_transaction().list_snapshots().await?;

if self.is_json_output_enabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ impl CardanoTransactionsSnapshotShowCommand {
/// Cardano transaction snapshot Show command
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
let params = context.config_parameters()?;
let client = client_builder_with_fallback_genesis_key(&params)?.build()?;
let client = client_builder_with_fallback_genesis_key(&params)?
.with_logger(context.logger().clone())
.build()?;

let get_list_of_artifact_ids = || async {
let transactions_sets = client.cardano_transaction().list_snapshots().await.with_context(|| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl MithrilStakeDistributionDownloadCommand {
let params = context.config_parameters()?.add_source(self)?;
let download_dir = params.get_or("download_dir", ".");
let download_dir = Path::new(&download_dir);
let logger = context.logger();

let progress_output_type = if self.is_json_output_enabled() {
ProgressOutputType::JsonReporter
Expand All @@ -60,7 +61,9 @@ impl MithrilStakeDistributionDownloadCommand {
let client = client_builder(&params)?
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
progress_output_type,
logger.clone(),
)))
.with_logger(logger.clone())
.build()?;

let get_list_of_artifact_ids = || async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ impl MithrilStakeDistributionListCommand {
/// Main command execution
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
let params = context.config_parameters()?;
let client = client_builder_with_fallback_genesis_key(&params)?.build()?;
let client = client_builder_with_fallback_genesis_key(&params)?
.with_logger(context.logger().clone())
.build()?;
let lines = client.mithril_stake_distribution().list().await?;

if self.is_json_output_enabled() {
Expand Down
7 changes: 2 additions & 5 deletions mithril-client-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub use deprecation::{DeprecatedCommand, Deprecation};

use clap::Args;
use mithril_client::{ClientBuilder, MithrilResult};
use slog_scope::logger;

use crate::configuration::ConfigParameters;

Expand All @@ -29,8 +28,7 @@ pub(crate) fn client_builder(params: &ConfigParameters) -> MithrilResult<ClientB
let builder = ClientBuilder::aggregator(
&params.require("aggregator_endpoint")?,
&params.require("genesis_verification_key")?,
)
.with_logger(logger());
);

Ok(builder)
}
Expand All @@ -50,8 +48,7 @@ pub(crate) fn client_builder_with_fallback_genesis_key(
"genesis_verification_key",
fallback_genesis_verification_key,
),
)
.with_logger(logger());
);

Ok(builder)
}
16 changes: 8 additions & 8 deletions mithril-client-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
use anyhow::{anyhow, Context};
use clap::{CommandFactory, Parser, Subcommand};
use config::{builder::DefaultState, ConfigBuilder, Map, Source, Value, ValueKind};
use slog::{Drain, Fuse, Level, Logger};
use slog_async::Async;
use slog_scope::debug;
use slog::{debug, Drain, Fuse, Level, Logger};
use slog_term::Decorator;
use std::io::Write;
use std::sync::Arc;
Expand Down Expand Up @@ -86,9 +84,9 @@ pub struct Args {

impl Args {
pub async fn execute(&self, root_logger: Logger) -> MithrilResult<()> {
debug!("Run Mode: {}", self.run_mode);
debug!(root_logger, "Run Mode: {}", self.run_mode);
let filename = format!("{}/{}.json", self.config_directory.display(), self.run_mode);
debug!("Reading configuration file '{}'.", filename);
debug!(root_logger, "Reading configuration file '{filename}'.");
let config: ConfigBuilder<DefaultState> = config::Config::builder()
.add_source(config::File::with_name(&filename).required(false))
.add_source(self.clone())
Expand Down Expand Up @@ -116,7 +114,7 @@ impl Args {
}
}

fn wrap_drain<D: Decorator + Send + 'static>(&self, decorator: D) -> Fuse<Async> {
fn wrap_drain<D: Decorator + Send + 'static>(&self, decorator: D) -> Fuse<slog_async::Async> {
let drain = slog_term::CompactFormat::new(decorator).build().fuse();
let drain = slog::LevelFilter::new(drain, self.log_level()).fuse();

Expand All @@ -128,7 +126,10 @@ impl Args {
let writer = log_output_type.get_writer()?;

let drain = if self.log_format_json {
let drain = slog_bunyan::new(writer).set_pretty(false).build().fuse();
let drain = slog_bunyan::with_name("mithril-client", writer)
.set_pretty(false)
.build()
.fuse();
let drain = slog::LevelFilter::new(drain, self.log_level()).fuse();

slog_async::Async::new(drain).build().fuse()
Expand Down Expand Up @@ -240,7 +241,6 @@ async fn main() -> MithrilResult<()> {
)
});
let logger = args.build_logger()?;
let _guard = slog_scope::set_global_logger(logger.clone());

#[cfg(feature = "bundle_openssl")]
openssl_probe::init_ssl_cert_env_vars();
Expand Down
Loading

0 comments on commit a3bbf33

Please sign in to comment.