Skip to content
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function_name = "0.3.0"
bitflags = { version = "2",features = ["serde"]}
prometheus = { version = "0.14.0", features = ["process"] }
futures = "0.3.32"
tokio = { version = "1.51.0", features = ["sync"] }

# Improves comp time ?
[profile.dev]
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
if let Ok(ssesh) = Box::pin(db::prelude::new_scylla_session(&format!("{scylla_inet}:9042"))).await {
let mcache = db::prelude::new_moka_cache(1_000);
let session = actix_web::web::Data::new(security::structures::ScyllaSession {
lock: std::sync::Mutex::new(ssesh)
lock: tokio::sync::Mutex::new(ssesh)
});


let cache = actix_web::web::Data::new(security::structures::MokaCache {
lock: std::sync::Mutex::new(mcache)
lock: tokio::sync::Mutex::new(mcache)
});

let rl_config = RateLimitConfig::default().max_requests(API_RATELIMIT_COUNT).window_secs(API_RATELIMIT_WINDOW_SECONDS);
Expand Down Expand Up @@ -124,4 +124,4 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
}
Ok(())
}

70 changes: 38 additions & 32 deletions src/metrics/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use actix_web::{web::Data, Error};
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
use futures::future::{ok, Ready};
use prometheus::{HistogramVec, HistogramOpts, IntCounterVec, IntCounter, Opts, Registry};
use actix_web::{Error, web::Data};
use futures::future::{Ready, ok};
use prometheus::{HistogramOpts, HistogramVec, IntCounter, IntCounterVec, Opts, Registry};
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Instant;
use std::pin::Pin;

#[derive(Clone)]
pub struct MetricsCollector {
Expand All @@ -19,27 +19,28 @@ impl MetricsCollector {
pub fn new(registry: &Registry) -> Result<Self, prometheus::Error> {
let request_counter = IntCounterVec::new(
Opts::new("http_requests_total", "Total number of HTTP requests"),
&["method", "endpoint", "status"]
&["method", "endpoint", "status"],
)?;

let response_time_histogram = HistogramVec::new(
HistogramOpts::new("http_request_duration_seconds", "HTTP request duration in seconds"),
&["method", "endpoint"]
HistogramOpts::new(
"http_request_duration_seconds",
"HTTP request duration in seconds",
),
&["method", "endpoint"],
)?;

let request_size = IntCounterVec::new(
Opts::new("http_request_size_bytes", "HTTP request size in bytes"),
&["method", "endpoint"]
&["method", "endpoint"],
)?;

let total_cache_hit_count = IntCounter::new(
"total_cache_hit_count", "How many cache hits happened."
)?;
let total_cache_hit_count =
IntCounter::new("total_cache_hit_count", "How many cache hits happened.")?;

let total_cache_miss_count =
IntCounter::new("total_cache_miss_count", "How many cache misses happened.")?;

let total_cache_miss_count = IntCounter::new(
"total_cache_miss_count", "How many cache misses happened."
)?;

registry.register(Box::new(request_counter.clone()))?;
registry.register(Box::new(total_cache_hit_count.clone()))?;
registry.register(Box::new(total_cache_miss_count.clone()))?;
Expand Down Expand Up @@ -113,7 +114,6 @@ pub struct MetricsMiddlewareService<S> {
collector: Data<MetricsCollector>,
}


/// To actually be able to interact with services asynchronously
/// we need to implement this `actix_web` trait:
///
Expand All @@ -138,14 +138,16 @@ where
fn call(&self, req: ServiceRequest) -> Self::Future {
let start_time = Instant::now();
let collector = self.collector.clone();

let method: String = req.method().to_string();
let endpoint = MetricsCollector::get_endpoint_pattern(&req).clone();

if let Some(content_length) = req.headers().get("content-length")
&& let Ok(size_str) = content_length.to_str()
&& let Ok(size) = size_str.parse::<u64>() {
collector.request_size
&& let Ok(size_str) = content_length.to_str()
&& let Ok(size) = size_str.parse::<u64>()
{
collector
.request_size
.with_label_values(&[&method, &endpoint])
.inc_by(size);
}
Expand Down Expand Up @@ -177,29 +179,33 @@ where
// counter for requests total, etc.)
let result = fut.await;
let duration = start_time.elapsed().as_secs_f64();

match result {
Ok(response) => {
collector.response_time_histogram
collector
.response_time_histogram
.with_label_values(&[&method, &endpoint])
.observe(duration);

let status = response.status().as_u16().to_string();
collector.request_counter
collector
.request_counter
.with_label_values(&[&method, &endpoint, &status])
.inc();

Ok(response)
},
}
Err(e) => {
collector.response_time_histogram
collector
.response_time_histogram
.with_label_values(&[&method, &endpoint])
.observe(duration);

collector.request_counter

collector
.request_counter
.with_label_values(&[&method, &endpoint, &"error".to_string()])
.inc();

Err(e)
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/security/structures.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use tokio;

pub struct ScyllaSession {
pub lock: std::sync::Mutex<scylla::client::session::Session>
pub lock: tokio::sync::Mutex<scylla::client::session::Session>,
}

pub struct MokaCache {
pub lock: std::sync::Mutex<moka::future::Cache<String, String>>
pub lock: tokio::sync::Mutex<moka::future::Cache<String, String>>,
}
2 changes: 1 addition & 1 deletion test-env-compose/Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ FROM workspace AS linter
RUN --mount=type=cache,target=/usr/local/cargo/registry \
<<EOF
if [ "$RUN_LINT" = "true" ]; then
cargo clippy --release -- -D warnings -A clippy::await_holding_lock
cargo clippy --release -- -D warnings
fi
EOF

Expand Down