Skip to content

Commit

Permalink
feat(hubble): add json logging (#1927)
Browse files Browse the repository at this point in the history
Adds the CLI option to set logging to json format, as well as updated
the service with the log-format parameter. Also made log-format an enum
in unionvisor to make it less error prone.
  • Loading branch information
KaiserKarel authored May 18, 2024
2 parents 4560f77 + a60d91a commit b58f1b9
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 20 deletions.
2 changes: 1 addition & 1 deletion hubble/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ time = { version = "0.3.30", features = ["serde"] }
tokio = { workspace = true, features = ["full"] }
tonic = { workspace = true, features = ["transport", "tls", "tls-roots", "tls-webpki-roots"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
tracing-subscriber = { workspace = true, features = ["env-filter", "json", "tracing-log"] }
unionlabs = { workspace = true, features = ["ethabi"] }
url = { version = "2.4.1", features = ["serde"] }
valuable = { version = "0.1.0", features = ["derive"] }
Expand Down
6 changes: 6 additions & 0 deletions hubble/hubble.nix
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
description = "RUST_LOG passed to hubble";
example = "hubble=debug";
};
log-format = mkOption {
type = types.enum [ "json" "plain" ];
default = "json";
example = "plain";
};
};

config = mkIf cfg.enable {
Expand All @@ -94,6 +99,7 @@
''
${pkgs.lib.getExe cfg.package} \
${datastore} \
--log-format ${cfg.log-format} \
--metrics-addr ${cfg.metrics-addr} \
--indexers '${indexersJson}'
'';
Expand Down
23 changes: 12 additions & 11 deletions hubble/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::{net::SocketAddr, str::FromStr};
use clap::Parser;
use url::Url;

use crate::logging::LogFormat;

/// Hubble is state machine observer.
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand All @@ -11,17 +13,6 @@ pub struct Args {
#[arg(short, long, env = "HUBBLE_HASURA_URL")]
pub url: Option<Url>,

/// The admin secret used to authenticate with hasura.
#[arg(
requires("url"),
group = "datastore",
required = true,
short = 's',
long,
env = "HUBBLE_HASURA_ADMIN_SECRET"
)]
pub hasura_admin_secret: Option<String>,

/// The database url used to connect with timescaledb.
#[arg(
group = "datastore",
Expand All @@ -43,6 +34,16 @@ pub struct Args {
/// Fetch the counterparty chain ids for all clients known to hubble.
#[arg(long)]
pub fetch_client_chain_ids: bool,

/// The log format for Hubble.
#[arg(
global = true,
short = 'f',
long,
env = "HUBBLE_LOG_FORMAT",
default_value = "json"
)]
pub log_format: LogFormat,
}

#[derive(Clone, Debug, serde::Deserialize)]
Expand Down
42 changes: 42 additions & 0 deletions hubble/src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::str::FromStr;

use thiserror::Error;
use tracing_subscriber::EnvFilter;

pub fn init(log_format: LogFormat) {
match log_format {
LogFormat::Plain => {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
}
LogFormat::Json => {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.json()
.init();
}
}
}

#[derive(Debug, Copy, Clone)]
pub enum LogFormat {
Plain,
Json,
}

#[derive(Debug, Error)]
#[error("unknown log format {0}")]
pub struct UnknownLogFormatError(String);

impl FromStr for LogFormat {
type Err = UnknownLogFormatError;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"plain" => Ok(Self::Plain),
"json" => Ok(Self::Json),
s => Err(UnknownLogFormatError(s.to_owned())),
}
}
}
11 changes: 4 additions & 7 deletions hubble/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ use clap::Parser;
use sqlx::postgres::PgPoolOptions;
use tokio::task::JoinSet;
use tracing::{error, info, warn};
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

mod chain_id_query;
mod cli;
mod eth;
mod healthz;
mod logging;
mod metrics;
mod postgres;
mod tm;

mod chain_id_query;

#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;

Expand All @@ -29,10 +28,8 @@ static GLOBAL: Jemalloc = Jemalloc;
async fn main() -> color_eyre::eyre::Result<()> {
color_eyre::install().unwrap();
let args = crate::cli::Args::parse();
tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_default_env())
.init();

crate::logging::init(args.log_format);
metrics::register_custom_metrics();

let db = PgPoolOptions::new()
Expand Down
2 changes: 1 addition & 1 deletion unionvisor/unionvisor.nix
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
default = self.packages.${pkgs.system}.bundle-testnet-8;
};
logFormat = mkOption {
type = types.str;
type = types.enum [ "json" "plain" ];
default = "json";
example = "plain";
};
Expand Down

0 comments on commit b58f1b9

Please sign in to comment.