diff --git a/limitador-server/src/main.rs b/limitador-server/src/main.rs index 3ca26690..e1bcc5f9 100644 --- a/limitador-server/src/main.rs +++ b/limitador-server/src/main.rs @@ -43,7 +43,6 @@ use std::path::Path; use std::sync::Arc; use std::time::Duration; use std::{env, process}; -use tracing_subscriber::Layer; #[cfg(feature = "distributed_storage")] use clap::parser::ValuesRef; @@ -52,9 +51,10 @@ use sysinfo::{MemoryRefreshKind, RefreshKind, System}; use thiserror::Error; use tokio::runtime::Handle; use tracing::level_filters::LevelFilter; +use tracing::Subscriber; use tracing_subscriber::fmt::format::FmtSpan; -use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::util::SubscriberInitExt; +use tracing_subscriber::{layer::SubscriberExt, Layer}; mod envoy_rls; mod http_api; @@ -760,15 +760,7 @@ fn configure_tracing_subscriber(config: &Configuration) { .unwrap_or(LevelFilter::ERROR) }); - let fmt_layer = tracing_subscriber::fmt::layer() - .with_span_events(if level >= LevelFilter::DEBUG { - FmtSpan::CLOSE - } else { - FmtSpan::NONE - }) - .with_filter(level); - - let metrics_layer = MetricsLayer::new() + let metrics_layer = MetricsLayer::default() .gather( "should_rate_limit", PrometheusMetrics::record_datastore_latency, @@ -794,22 +786,41 @@ fn configure_tracing_subscriber(config: &Configuration) { .with_trace_config(trace::config().with_resource(Resource::new(vec![ KeyValue::new("service.name", "limitador"), ]))) - .install_batch(opentelemetry_sdk::runtime::Tokio)?; + .install_batch(opentelemetry_sdk::runtime::Tokio) + .expect("error installing tokio tracing exporter"); let telemetry_layer = tracing_opentelemetry::layer().with_tracer(tracer); // Init tracing subscriber with telemetry tracing_subscriber::registry() .with(metrics_layer) - .with(fmt_layer) + .with(fmt_layer(level)) .with(level.max(LevelFilter::INFO)) .with(telemetry_layer) - .init(); + .init() } else { - // Init tracing subscriber without telemetry - tracing_subscriber::registry() - .with(metrics_layer) - .with(fmt_layer) - .init(); - }; + // If running in memory initialize without metrics, otherwise include the metrics layer + match config.storage { + StorageConfiguration::InMemory(_) => { + tracing_subscriber::registry().with(fmt_layer(level)).init() + } + _ => tracing_subscriber::registry() + .with(metrics_layer) + .with(fmt_layer(level)) + .init(), + } + } +} + +fn fmt_layer(level: LevelFilter) -> impl Layer +where + S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>, +{ + tracing_subscriber::fmt::layer() + .with_span_events(if level >= LevelFilter::DEBUG { + FmtSpan::CLOSE + } else { + FmtSpan::NONE + }) + .with_filter(level) }