Skip to content

Commit

Permalink
Disable metrics layer if running in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-cattermole committed Jun 7, 2024
1 parent 2237d53 commit c66fc01
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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<S>(level: LevelFilter) -> impl Layer<S>
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)
}

0 comments on commit c66fc01

Please sign in to comment.