Skip to content

Commit

Permalink
Merge branch 'main' into fix-295
Browse files Browse the repository at this point in the history
  • Loading branch information
chirino committed May 2, 2024
1 parent f9baacd commit f7fc524
Show file tree
Hide file tree
Showing 9 changed files with 464 additions and 256 deletions.
250 changes: 201 additions & 49 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions limitador-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
edition = "2021"

[features]
infinispan = [ "limitador/infinispan_storage" ]
infinispan = ["limitador/infinispan_storage"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down Expand Up @@ -46,7 +46,8 @@ lazy_static = "1.4.0"
clap = "4.3"
sysinfo = "0.30.10"
openssl = { version = "0.10.57", features = ["vendored"] }
prometheus = "0.13.3"
metrics = "0.22.3"
metrics-exporter-prometheus = "0.14.0"


[build-dependencies]
Expand Down
31 changes: 25 additions & 6 deletions limitador-server/src/envoy_rls/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ mod tests {

use crate::envoy_rls::server::envoy::extensions::common::ratelimit::v3::rate_limit_descriptor::Entry;
use crate::envoy_rls::server::envoy::extensions::common::ratelimit::v3::RateLimitDescriptor;
use crate::prometheus_metrics::tests::TEST_PROMETHEUS_HANDLE;
use crate::Configuration;

use super::*;
Expand Down Expand Up @@ -311,7 +312,10 @@ mod tests {
let rate_limiter = MyRateLimiter::new(
Arc::new(Limiter::Blocking(limiter)),
RateLimitHeaders::DraftVersion03,
Arc::new(PrometheusMetrics::default()),
Arc::new(PrometheusMetrics::new_with_handle(
false,
TEST_PROMETHEUS_HANDLE.clone(),
)),
);

let req = RateLimitRequest {
Expand Down Expand Up @@ -370,7 +374,10 @@ mod tests {
let rate_limiter = MyRateLimiter::new(
Arc::new(Limiter::new(Configuration::default()).await.unwrap()),
RateLimitHeaders::DraftVersion03,
Arc::new(PrometheusMetrics::default()),
Arc::new(PrometheusMetrics::new_with_handle(
false,
TEST_PROMETHEUS_HANDLE.clone(),
)),
);

let req = RateLimitRequest {
Expand Down Expand Up @@ -401,7 +408,10 @@ mod tests {
let rate_limiter = MyRateLimiter::new(
Arc::new(Limiter::new(Configuration::default()).await.unwrap()),
RateLimitHeaders::DraftVersion03,
Arc::new(PrometheusMetrics::default()),
Arc::new(PrometheusMetrics::new_with_handle(
false,
TEST_PROMETHEUS_HANDLE.clone(),
)),
);

let req = RateLimitRequest {
Expand Down Expand Up @@ -444,7 +454,10 @@ mod tests {
let rate_limiter = MyRateLimiter::new(
Arc::new(Limiter::Blocking(limiter)),
RateLimitHeaders::DraftVersion03,
Arc::new(PrometheusMetrics::default()),
Arc::new(PrometheusMetrics::new_with_handle(
false,
TEST_PROMETHEUS_HANDLE.clone(),
)),
);

let req = RateLimitRequest {
Expand Down Expand Up @@ -503,7 +516,10 @@ mod tests {
let rate_limiter = MyRateLimiter::new(
Arc::new(Limiter::Blocking(limiter)),
RateLimitHeaders::DraftVersion03,
Arc::new(PrometheusMetrics::default()),
Arc::new(PrometheusMetrics::new_with_handle(
false,
TEST_PROMETHEUS_HANDLE.clone(),
)),
);

let req = RateLimitRequest {
Expand Down Expand Up @@ -569,7 +585,10 @@ mod tests {
let rate_limiter = MyRateLimiter::new(
Arc::new(Limiter::Blocking(limiter)),
RateLimitHeaders::DraftVersion03,
Arc::new(PrometheusMetrics::default()),
Arc::new(PrometheusMetrics::new_with_handle(
false,
TEST_PROMETHEUS_HANDLE.clone(),
)),
);

let req = RateLimitRequest {
Expand Down
17 changes: 13 additions & 4 deletions limitador-server/src/http_api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ pub async fn run_http_server(
#[cfg(test)]
mod tests {
use super::*;
use crate::prometheus_metrics::tests::TEST_PROMETHEUS_HANDLE;
use crate::Configuration;
use actix_web::{test, web};
use limitador::limit::Limit as LimitadorLimit;
Expand All @@ -258,7 +259,9 @@ mod tests {
async fn test_metrics() {
let rate_limiter: Arc<Limiter> =
Arc::new(Limiter::new(Configuration::default()).await.unwrap());
let prometheus_metrics: Arc<PrometheusMetrics> = Arc::new(PrometheusMetrics::default());
let prometheus_metrics: Arc<PrometheusMetrics> = Arc::new(
PrometheusMetrics::new_with_handle(false, TEST_PROMETHEUS_HANDLE.clone()),
);
let data = web::Data::new(RateLimitData::new(rate_limiter, prometheus_metrics));
let app = test::init_service(
App::new()
Expand All @@ -283,7 +286,9 @@ mod tests {

let limit = create_test_limit(&limiter, namespace, 10).await;
let rate_limiter: Arc<Limiter> = Arc::new(limiter);
let prometheus_metrics: Arc<PrometheusMetrics> = Arc::new(PrometheusMetrics::default());
let prometheus_metrics: Arc<PrometheusMetrics> = Arc::new(
PrometheusMetrics::new_with_handle(false, TEST_PROMETHEUS_HANDLE.clone()),
);
let data = web::Data::new(RateLimitData::new(rate_limiter, prometheus_metrics));
let app = test::init_service(
App::new()
Expand All @@ -310,7 +315,9 @@ mod tests {
let namespace = "test_namespace";
let _limit = create_test_limit(&limiter, namespace, 1).await;
let rate_limiter: Arc<Limiter> = Arc::new(limiter);
let prometheus_metrics: Arc<PrometheusMetrics> = Arc::new(PrometheusMetrics::default());
let prometheus_metrics: Arc<PrometheusMetrics> = Arc::new(
PrometheusMetrics::new_with_handle(false, TEST_PROMETHEUS_HANDLE.clone()),
);
let data = web::Data::new(RateLimitData::new(rate_limiter, prometheus_metrics));
let app = test::init_service(
App::new()
Expand Down Expand Up @@ -355,7 +362,9 @@ mod tests {
let _limit = create_test_limit(&limiter, namespace, 1).await;

let rate_limiter: Arc<Limiter> = Arc::new(limiter);
let prometheus_metrics: Arc<PrometheusMetrics> = Arc::new(PrometheusMetrics::default());
let prometheus_metrics: Arc<PrometheusMetrics> = Arc::new(
PrometheusMetrics::new_with_handle(false, TEST_PROMETHEUS_HANDLE.clone()),
);
let data = web::Data::new(RateLimitData::new(rate_limiter, prometheus_metrics));
let app = test::init_service(
App::new()
Expand Down
16 changes: 8 additions & 8 deletions limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::config::{
use crate::envoy_rls::server::{run_envoy_rls_server, RateLimitHeaders};
use crate::http_api::server::run_http_server;
use crate::metrics::MetricsLayer;
use ::metrics::histogram;
use clap::{value_parser, Arg, ArgAction, Command};
use const_format::formatcp;
use limitador::counter::Counter;
Expand Down Expand Up @@ -256,7 +257,7 @@ fn find_first_negative_limit(limits: &[Limit]) -> Option<usize> {

#[actix_rt::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (config, prometheus_metrics) = {
let config = {
let (config, version) = create_config();
println!("{LIMITADOR_HEADER} {version}");
let level = config.log_level.unwrap_or_else(|| {
Expand All @@ -270,14 +271,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::layer()
};

let limit_name_in_metrics = config.limit_name_in_labels;
let prometheus_metrics =
Arc::new(PrometheusMetrics::new_with_options(limit_name_in_metrics));
let metrics = prometheus_metrics.clone();

let metrics_layer = MetricsLayer::new().gather(
"should_rate_limit",
move |timings| metrics.counter_access(Duration::from(timings)),
|timings| histogram!("counter_latency").record(Duration::from(timings).as_secs_f64()),
vec!["datastore"],
);

Expand Down Expand Up @@ -311,9 +307,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

info!("Version: {}", version);
info!("Using config: {:?}", config);
(config, prometheus_metrics)
config
};

let prometheus_metrics = Arc::new(PrometheusMetrics::new_with_options(
config.limit_name_in_labels,
));

let limit_file = config.limits_file.clone();
let envoy_rls_address = config.rlp_address();
let http_api_address = config.http_address();
Expand Down
Loading

0 comments on commit f7fc524

Please sign in to comment.