Skip to content

Commit

Permalink
Merge pull request #250 from Kuadrant/tracing
Browse files Browse the repository at this point in the history
Add metrics on traffic going in the server and out of the crate layer (e.g. Redis)
  • Loading branch information
alexsnaps authored Feb 20, 2024
2 parents 094b4a7 + a77f7c7 commit 40fc8c7
Show file tree
Hide file tree
Showing 16 changed files with 436 additions and 109 deletions.
134 changes: 102 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion limitador-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ prost = "0.12"
prost-types = "0.12"
serde_yaml = "0.9"
log = "0.4"
env_logger = "0.10.0"
tracing = "0.1.40"
tracing-log = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
url = "2"
actix-web = "4.1"
actix-rt = "2"
Expand Down
2 changes: 1 addition & 1 deletion limitador-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use crate::envoy_rls::server::RateLimitHeaders;
use limitador::storage;
use log::LevelFilter;
use tracing::level_filters::LevelFilter;

#[derive(Debug)]
pub struct Configuration {
Expand Down
1 change: 1 addition & 0 deletions limitador-server/src/envoy_rls/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl MyRateLimiter {

#[tonic::async_trait]
impl RateLimitService for MyRateLimiter {
#[tracing::instrument(skip_all)]
async fn should_rate_limit(
&self,
request: Request<RateLimitRequest>,
Expand Down
6 changes: 6 additions & 0 deletions limitador-server/src/http_api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async fn status() -> web::Json<()> {
Json(())
}

#[tracing::instrument(skip(data))]
#[api_v2_operation]
async fn metrics(data: web::Data<Arc<Limiter>>) -> String {
match data.get_ref().as_ref() {
Expand All @@ -53,6 +54,7 @@ async fn metrics(data: web::Data<Arc<Limiter>>) -> String {
}

#[api_v2_operation]
#[tracing::instrument(skip(data))]
async fn get_limits(
data: web::Data<Arc<Limiter>>,
namespace: web::Path<String>,
Expand All @@ -66,6 +68,7 @@ async fn get_limits(
Ok(Json(resp_limits))
}

#[tracing::instrument(skip(data))]
#[api_v2_operation]
async fn get_counters(
data: web::Data<Arc<Limiter>>,
Expand All @@ -89,6 +92,7 @@ async fn get_counters(
}
}

#[tracing::instrument(skip(state))]
#[api_v2_operation]
async fn check(
state: web::Data<Arc<Limiter>>,
Expand Down Expand Up @@ -117,6 +121,7 @@ async fn check(
}
}

#[tracing::instrument(skip(data))]
#[api_v2_operation]
async fn report(
data: web::Data<Arc<Limiter>>,
Expand All @@ -139,6 +144,7 @@ async fn report(
}
}

#[tracing::instrument(skip(data))]
#[api_v2_operation]
async fn check_and_report(
data: web::Data<Arc<Limiter>>,
Expand Down
28 changes: 16 additions & 12 deletions limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::envoy_rls::server::{run_envoy_rls_server, RateLimitHeaders};
use crate::http_api::server::run_http_server;
use clap::{value_parser, Arg, ArgAction, Command};
use const_format::formatcp;
use env_logger::Builder;
use limitador::counter::Counter;
use limitador::errors::LimitadorError;
use limitador::limit::Limit;
Expand All @@ -30,7 +29,6 @@ use limitador::storage::{AsyncCounterStorage, AsyncStorage, Storage};
use limitador::{
storage, AsyncRateLimiter, AsyncRateLimiterBuilder, RateLimiter, RateLimiterBuilder,
};
use log::LevelFilter;
use notify::event::{ModifyKind, RenameMode};
use notify::{Error, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::env::VarError;
Expand All @@ -42,6 +40,8 @@ use std::{env, process};
use sysinfo::{RefreshKind, System, SystemExt};
use thiserror::Error;
use tokio::runtime::Handle;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::fmt::format::FmtSpan;

mod envoy_rls;
mod http_api;
Expand Down Expand Up @@ -285,13 +285,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = {
let (config, version) = create_config();
println!("{LIMITADOR_HEADER} {version}");
let mut builder = Builder::new();
if let Some(level) = config.log_level {
builder.filter(None, level);
let level = config.log_level.unwrap_or_else(|| {
tracing_subscriber::filter::EnvFilter::from_default_env()
.max_level_hint()
.unwrap_or(LevelFilter::ERROR)
});
let builder = if level >= LevelFilter::DEBUG {
tracing_subscriber::fmt().with_span_events(FmtSpan::CLOSE)
} else {
builder.parse_default_env();
}
builder.init();
tracing_subscriber::fmt()
};
builder.with_max_level(level).init();

info!("Version: {}", version);
info!("Using config: {:?}", config);
Expand Down Expand Up @@ -759,10 +763,10 @@ fn create_config() -> (Configuration, &'static str) {

config.log_level = match matches.get_count("v") {
0 => None,
1 => Some(LevelFilter::Warn),
2 => Some(LevelFilter::Info),
3 => Some(LevelFilter::Debug),
4 => Some(LevelFilter::Trace),
1 => Some(LevelFilter::WARN),
2 => Some(LevelFilter::INFO),
3 => Some(LevelFilter::DEBUG),
4 => Some(LevelFilter::TRACE),
_ => unreachable!("Verbosity should at most be 4!"),
};

Expand Down
1 change: 1 addition & 0 deletions limitador/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async-trait = "0.1"
cfg-if = "1"
prometheus = "0.13"
lazy_static = "1"
tracing = "0.1.40"

# Optional dependencies
rocksdb = { version = "0.21.0", optional = true, features = ["multi-threaded-cf"] }
Expand Down
Loading

0 comments on commit 40fc8c7

Please sign in to comment.