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

feat: Add support for setting log format at startup #322

Merged
merged 1 commit into from
Nov 1, 2023
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
8 changes: 7 additions & 1 deletion CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This document contains the help content for the `unleash-edge` command-line prog
Default value: `3043`
* `--instance-id <INSTANCE_ID>` — Instance id. Used for metrics reporting

Default value: `01HAM0JMKDMZZP0HXNT8K1J6FW`
Default value: `01HE51VN5SQBAWR5Q93NAESEPW`
* `-a`, `--app-name <APP_NAME>` — App name. Used for metrics reporting

Default value: `unleash-edge`
Expand All @@ -58,6 +58,12 @@ This document contains the help content for the `unleash-edge` command-line prog
* `--edge-request-timeout <EDGE_REQUEST_TIMEOUT>` — Timeout for requests to Edge

Default value: `5`
* `-l`, `--log-format <LOG_FORMAT>` — Which log format should Edge use

Default value: `plain`

Possible values: `plain`, `json`, `pretty`




Expand Down
11 changes: 11 additions & 0 deletions server/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ pub struct ReadyCheckArgs {
pub ca_certificate_file: Option<PathBuf>,
}

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

#[derive(Parser, Debug, Clone)]
pub struct CliArgs {
#[clap(flatten)]
Expand Down Expand Up @@ -248,6 +255,10 @@ pub struct CliArgs {
/// Timeout for requests to Edge
#[clap(long, env, default_value_t = 5)]
pub edge_request_timeout: u64,

/// Which log format should Edge use
#[clap(short, long, env, global = true, value_enum, default_value_t = LogFormat::Plain)]
pub log_format: LogFormat,
}

#[derive(Args, Debug, Clone)]
Expand Down
1 change: 0 additions & 1 deletion server/src/http/feature_refresher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,6 @@ mod tests {
upstream_features_cache.insert(cache_key.clone(), empty_features);

feature_refresher.refresh_features().await;
println!("{:#?}", features_cache.get(&cache_key).unwrap().features);
// Since our response was empty, our theory is that there should be no features here now.
assert!(!features_cache
.get(&cache_key)
Expand Down
20 changes: 0 additions & 20 deletions server/src/http/unleash_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,24 +949,4 @@ mod tests {
);
assert!(client.is_ok());
}

/* #[tokio::test]
pub async fn can_make_ssl_request() {
let root_cert = "./testdata/tls/certs/cacert.pem";
let key = "./testdata/client_certs/client.key.pem";
let cert = "./testdata/client_certs/client.cert.pem";
let identity = ClientTls {
pkcs8_client_certificate_file: Some(cert.into()),
pkcs8_client_key_file: Some(key.into()),
pkcs12_identity_file: None,
pkcs12_passphrase: None,
upstream_certificate_file: Some(root_cert.into()),
};
let client = new_reqwest_client("test_pkcs8".into(), false, Some(identity));
assert!(client.is_ok());
let actual_client = client.unwrap();
let res = actual_client.get("https://localhost:4433").send().await;
println!("{res:?}");
assert!(res.is_ok());
}*/
}
2 changes: 1 addition & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async fn main() -> Result<(), anyhow::Error> {
let request_timeout = args.edge_request_timeout;
let trust_proxy = args.clone().trust_proxy;
let base_path = http_args.base_path.clone();
let (metrics_handler, request_metrics) = prom_metrics::instantiate(None);
let (metrics_handler, request_metrics) = prom_metrics::instantiate(None, &args.log_format);
let connect_via = ConnectVia {
app_name: args.clone().app_name,
instance_id: args.clone().instance_id,
Expand Down
27 changes: 21 additions & 6 deletions server/src/prom_metrics.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::cli::LogFormat;
use opentelemetry_sdk::metrics::MeterProvider;
#[cfg(target_os = "linux")]
use prometheus::process_collector::ProcessCollector;
Expand All @@ -9,20 +10,34 @@ use crate::metrics::actix_web_metrics::{
PrometheusMetricsHandler, RequestMetrics, RequestMetricsBuilder,
};

fn instantiate_tracing_and_logging() {
let logger = tracing_subscriber::fmt::layer();
fn instantiate_tracing_and_logging(log_format: &LogFormat) {
let env_filter = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("info"))
.unwrap();
let collector = Registry::default().with(logger).with(env_filter);
// Initialize tracing
tracing::subscriber::set_global_default(collector).unwrap();
match log_format {
LogFormat::Plain => {
let logger = tracing_subscriber::fmt::layer();
let collector = Registry::default().with(logger).with(env_filter);
tracing::subscriber::set_global_default(collector).unwrap();
}
LogFormat::Json => {
let logger = tracing_subscriber::fmt::layer().json();
let collector = Registry::default().with(logger).with(env_filter);
tracing::subscriber::set_global_default(collector).unwrap();
}
LogFormat::Pretty => {
let logger = tracing_subscriber::fmt::layer().pretty();
let collector = Registry::default().with(logger).with(env_filter);
tracing::subscriber::set_global_default(collector).unwrap();
}
};
}

pub fn instantiate(
registry: Option<prometheus::Registry>,
log_format: &LogFormat,
) -> (PrometheusMetricsHandler, RequestMetrics) {
instantiate_tracing_and_logging();
instantiate_tracing_and_logging(log_format);
let registry = registry.unwrap_or_else(instantiate_registry);
register_custom_metrics(&registry);
instantiate_prometheus_metrics_handler(registry)
Expand Down
5 changes: 1 addition & 4 deletions server/src/ready_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ pub async fn check_ready(ready_check_args: ReadyCheckArgs) -> Result<(), EdgeErr
))
})?;
match ready_check_result.status {
Status::Ready => {
println!("OK");
Ok(())
}
Status::Ready => Ok(()),
_ => Err(EdgeError::ReadyCheckError(format!(
"Ready check returned a different status than READY. It returned {:?}",
ready_check_result
Expand Down
Loading