Skip to content

Commit

Permalink
refactor: add clap to common and use one LogFormat in project
Browse files Browse the repository at this point in the history
  • Loading branch information
dav1do committed May 22, 2024
1 parent 20160d4 commit 5fad7d8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 47 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tokio-console = ["telemetry", "dep:console-subscriber"]
[dependencies]
anyhow.workspace = true
console-subscriber = { workspace = true, optional = true }
clap.workspace = true # technically optional to telemetry but required by the rest of the workspace right now
gethostname = "0.4.2"
opentelemetry-otlp = { workspace = true, optional = true }
opentelemetry = { workspace = true, optional = true }
Expand Down
11 changes: 10 additions & 1 deletion common/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ use tracing_subscriber::{
};

#[derive(
Clone, Copy, Debug, PartialEq, Eq, Hash, Default, serde::Deserialize, serde::Serialize,
Clone,
Copy,
Debug,
PartialEq,
Eq,
Hash,
Default,
clap::ValueEnum,
serde::Deserialize,
serde::Serialize,
)]
/// The format to use for logging
#[serde(rename_all = "camelCase")]
Expand Down
25 changes: 2 additions & 23 deletions operator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Operator is a long lived process that auotmates creating and managing Ceramic networks.
#![deny(missing_docs)]
use anyhow::Result;
use clap::{arg, command, Parser, Subcommand, ValueEnum};
use clap::{arg, command, Parser, Subcommand};
use keramik_common::telemetry;
use keramik_operator::set_network_log_format;
use opentelemetry::global::{shutdown_meter_provider, shutdown_tracer_provider};
Expand All @@ -20,28 +20,7 @@ struct Cli {
prom_bind: String,

#[arg(long, env = "OPERATOR_LOG_FORMAT")]
log_format: Option<LogFormat>,
}

#[derive(ValueEnum, Debug, Clone)]
/// The format of the logs
pub enum LogFormat {
/// Compact single line logs
SingleLine,
/// Pretty multi-line logs
MultiLine,
/// JSON logs
Json,
}

impl From<LogFormat> for telemetry::LogFormat {
fn from(format: LogFormat) -> telemetry::LogFormat {
match format {
LogFormat::SingleLine => telemetry::LogFormat::SingleLine,
LogFormat::MultiLine => telemetry::LogFormat::MultiLine,
LogFormat::Json => telemetry::LogFormat::Json,
}
}
log_format: Option<telemetry::LogFormat>,
}

/// Available Subcommands
Expand Down
25 changes: 2 additions & 23 deletions runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,7 @@ struct Cli {

/// Specify the format of log events.
#[arg(long, default_value = "multi-line", env = "RUNNER_LOG_FORMAT")]
log_format: LogFormat,
}

#[derive(clap::ValueEnum, Debug, Clone)]
/// The format of the logs
pub enum LogFormat {
/// Compact single line logs
SingleLine,
/// Pretty multi-line logs
MultiLine,
/// JSON logs
Json,
}

impl From<LogFormat> for telemetry::LogFormat {
fn from(format: LogFormat) -> telemetry::LogFormat {
match format {
LogFormat::SingleLine => telemetry::LogFormat::SingleLine,
LogFormat::MultiLine => telemetry::LogFormat::MultiLine,
LogFormat::Json => telemetry::LogFormat::Json,
}
}
log_format: telemetry::LogFormat,
}

/// Available Subcommands
Expand Down Expand Up @@ -91,7 +70,7 @@ pub enum CommandResult {
#[tokio::main]
async fn main() -> Result<()> {
let args = Cli::parse();
telemetry::init_tracing(Some(args.otlp_endpoint.clone()), args.log_format.into()).await?;
telemetry::init_tracing(Some(args.otlp_endpoint.clone()), args.log_format).await?;
let metrics_controller = telemetry::init_metrics_otlp(args.otlp_endpoint.clone()).await?;
info!("starting runner");

Expand Down

0 comments on commit 5fad7d8

Please sign in to comment.