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

Refactor levels for metrics, logging, and tracing #348

Merged
merged 2 commits into from
Jun 3, 2024
Merged
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
23 changes: 16 additions & 7 deletions limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ 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;
Expand Down Expand Up @@ -215,16 +216,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = {
let (config, version) = create_config();
println!("{LIMITADOR_HEADER} {version}");

let level = config.log_level.unwrap_or_else(|| {
tracing_subscriber::filter::EnvFilter::from_default_env()
.max_level_hint()
.unwrap_or(LevelFilter::ERROR)
});
let fmt_layer = if level >= LevelFilter::DEBUG {
tracing_subscriber::fmt::layer().with_span_events(FmtSpan::CLOSE)
} else {
tracing_subscriber::fmt::layer()
};

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()
.gather(
Expand All @@ -240,6 +245,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

if !config.tracing_endpoint.is_empty() {
global::set_text_map_propagator(TraceContextPropagator::new());

let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
Expand All @@ -251,16 +257,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
KeyValue::new("service.name", "limitador"),
])))
.install_batch(opentelemetry_sdk::runtime::Tokio)?;

let telemetry_layer = tracing_opentelemetry::layer().with_tracer(tracer);

// Init tracing subscriber with telemetry
tracing_subscriber::registry()
.with(level)
.with(metrics_layer)
.with(fmt_layer)
.with(level.max(LevelFilter::INFO))
.with(telemetry_layer)
.init();
} else {
// Init tracing subscriber without telemetry
tracing_subscriber::registry()
.with(level)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth mentioning that removing this global level filter means all spans hit the metrics layer regardless of the configured verbosity now

.with(metrics_layer)
.with(fmt_layer)
.init();
Expand Down
Loading