| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use axum::{ |
| 8 |
extract::{MatchedPath, Request, State}, |
| 9 |
middleware::Next, |
| 10 |
response::{IntoResponse, Response}, |
| 11 |
}; |
| 12 |
use metrics::{counter, gauge, histogram}; |
| 13 |
use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle}; |
| 14 |
use std::time::Instant; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
pub fn init() -> PrometheusHandle { |
| 19 |
PrometheusBuilder::new() |
| 20 |
.install_recorder() |
| 21 |
.expect("failed to install Prometheus recorder") |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
#[allow( |
| 26 |
clippy::unused_async, |
| 27 |
reason = "axum handler: a sync fn returning impl IntoResponse does not implement the Handler trait" |
| 28 |
)] |
| 29 |
pub async fn render(State(handle): State<PrometheusHandle>) -> impl IntoResponse { |
| 30 |
handle.render() |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
pub async fn cache_control_middleware(request: Request, next: Next) -> Response { |
| 47 |
use axum::http::header::{CACHE_CONTROL, SET_COOKIE}; |
| 48 |
|
| 49 |
let path = request.uri().path().to_string(); |
| 50 |
let public = is_public_page(&path); |
| 51 |
|
| 52 |
|
| 53 |
let session = if public { |
| 54 |
request |
| 55 |
.extensions() |
| 56 |
.get::<tower_sessions::Session>() |
| 57 |
.cloned() |
| 58 |
} else { |
| 59 |
None |
| 60 |
}; |
| 61 |
|
| 62 |
let mut response = next.run(request).await; |
| 63 |
|
| 64 |
|
| 65 |
if response.headers().contains_key(CACHE_CONTROL) { |
| 66 |
return response; |
| 67 |
} |
| 68 |
|
| 69 |
let authed = match session { |
| 70 |
Some(ref s) => crate::auth::session_user(s).await.is_some(), |
| 71 |
None => false, |
| 72 |
}; |
| 73 |
let sets_cookie = response.headers().contains_key(SET_COOKIE); |
| 74 |
let value = cache_control_value(&path, public, sets_cookie, authed); |
| 75 |
|
| 76 |
response |
| 77 |
.headers_mut() |
| 78 |
.insert(CACHE_CONTROL, axum::http::HeaderValue::from_static(value)); |
| 79 |
|
| 80 |
|
| 81 |
if path.starts_with("/api/") { |
| 82 |
response.headers_mut().insert( |
| 83 |
axum::http::HeaderName::from_static("mnw-version"), |
| 84 |
axum::http::HeaderValue::from_static("2026-04-23"), |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
response |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
fn cache_control_value(path: &str, public: bool, sets_cookie: bool, authed: bool) -> &'static str { |
| 99 |
if public { |
| 100 |
if sets_cookie || authed { |
| 101 |
|
| 102 |
"private, no-cache" |
| 103 |
} else { |
| 104 |
|
| 105 |
"public, max-age=0, s-maxage=60, stale-while-revalidate=300" |
| 106 |
} |
| 107 |
} else if path.starts_with("/api/") |
| 108 |
|| path.starts_with("/stripe/") |
| 109 |
|| path.starts_with("/postmark/") |
| 110 |
{ |
| 111 |
"no-store" |
| 112 |
} else { |
| 113 |
|
| 114 |
"private, no-cache" |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
fn is_public_page(path: &str) -> bool { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
matches!(path, "/" | "/discover" | "/pricing" | "/source" | "/economics") |
| 124 |
|| path.starts_with("/p/") |
| 125 |
|| path.starts_with("/i/") |
| 126 |
|| path.starts_with("/u/") |
| 127 |
|| path.starts_with("/c/") |
| 128 |
|| path.starts_with("/docs") |
| 129 |
|| path.starts_with("/discover/") |
| 130 |
|| path.starts_with("/source/") |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|| path.starts_with("/feed/") |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
pub async fn metrics_middleware(request: Request, next: Next) -> Response { |
| 147 |
let method = request.method().to_string(); |
| 148 |
let path = request |
| 149 |
.extensions() |
| 150 |
.get::<MatchedPath>() |
| 151 |
.map_or_else(|| "<unmatched>".to_string(), |p| p.as_str().to_string()); |
| 152 |
|
| 153 |
let start = Instant::now(); |
| 154 |
let response = next.run(request).await; |
| 155 |
let duration = start.elapsed().as_secs_f64(); |
| 156 |
|
| 157 |
let status = status_class(response.status().as_u16()); |
| 158 |
|
| 159 |
let labels = [ |
| 160 |
("method", method), |
| 161 |
("path", path), |
| 162 |
("status", status.to_string()), |
| 163 |
]; |
| 164 |
|
| 165 |
counter!("http_requests_total", &labels).increment(1); |
| 166 |
histogram!("http_request_duration_seconds", &labels).record(duration); |
| 167 |
|
| 168 |
response |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
fn status_class(code: u16) -> &'static str { |
| 173 |
match code { |
| 174 |
200..=299 => "2xx", |
| 175 |
300..=399 => "3xx", |
| 176 |
400..=499 => "4xx", |
| 177 |
500..=599 => "5xx", |
| 178 |
_ => "other", |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
pub fn record_db_pool_stats(pool: &sqlx::PgPool) { |
| 185 |
let size = pool.size() as f64; |
| 186 |
let idle = pool.num_idle() as f64; |
| 187 |
gauge!("db_pool_connections_max").set(size); |
| 188 |
gauge!("db_pool_connections_idle").set(idle); |
| 189 |
gauge!("db_pool_connections_active").set(size - idle); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
#[tracing::instrument(skip_all)] |
| 200 |
pub async fn record_pg_stat_activity(pool: &sqlx::PgPool) -> Option<(i64, i64)> { |
| 201 |
let row: Result<(i64, i64), _> = sqlx::query_as( |
| 202 |
"SELECT \ |
| 203 |
(SELECT count(*) FROM pg_stat_activity \ |
| 204 |
WHERE state IS NOT NULL AND backend_type = 'client backend')::bigint, \ |
| 205 |
current_setting('max_connections')::bigint", |
| 206 |
) |
| 207 |
.fetch_one(pool) |
| 208 |
.await; |
| 209 |
|
| 210 |
match row { |
| 211 |
Ok((active, max_conn)) if max_conn > 0 => { |
| 212 |
gauge!("pg_stat_activity_active_backends").set(active as f64); |
| 213 |
gauge!("pg_stat_activity_max_connections").set(max_conn as f64); |
| 214 |
gauge!("pg_stat_activity_utilization_ratio").set(active as f64 / max_conn as f64); |
| 215 |
Some((active, max_conn)) |
| 216 |
} |
| 217 |
Ok(_) => None, |
| 218 |
Err(e) => { |
| 219 |
tracing::debug!(error = ?e, "pg_stat_activity gauge update failed"); |
| 220 |
None |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
#[tracing::instrument(skip_all)] |
| 237 |
pub async fn record_storage_fill_stats(pool: &sqlx::PgPool) { |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
use crate::db::CreatorTier; |
| 244 |
let tp = crate::tier_prices::TierPrices::global(); |
| 245 |
let basic = tp.max_storage_bytes_for(CreatorTier::Basic); |
| 246 |
let small_files = tp.max_storage_bytes_for(CreatorTier::SmallFiles); |
| 247 |
let big_files = tp.max_storage_bytes_for(CreatorTier::BigFiles); |
| 248 |
let everything = tp.max_storage_bytes_for(CreatorTier::Everything); |
| 249 |
|
| 250 |
let row: Result<(i64, i64), _> = sqlx::query_as( |
| 251 |
r" |
| 252 |
WITH tier_caps(tier, cap_bytes) AS ( |
| 253 |
VALUES |
| 254 |
('basic'::text, $1::bigint), |
| 255 |
('small_files'::text, $2::bigint), |
| 256 |
('big_files'::text, $3::bigint), |
| 257 |
('everything'::text, $4::bigint) |
| 258 |
) |
| 259 |
SELECT |
| 260 |
COALESCE(SUM(u.storage_used_bytes), 0)::bigint AS used, |
| 261 |
COALESCE(SUM(tc.cap_bytes), 0)::bigint AS cap |
| 262 |
FROM users u |
| 263 |
JOIN creator_subscriptions cs |
| 264 |
ON cs.user_id = u.id AND cs.status = 'active' |
| 265 |
JOIN tier_caps tc ON tc.tier = cs.tier |
| 266 |
", |
| 267 |
) |
| 268 |
.bind(basic) |
| 269 |
.bind(small_files) |
| 270 |
.bind(big_files) |
| 271 |
.bind(everything) |
| 272 |
.fetch_one(pool) |
| 273 |
.await; |
| 274 |
|
| 275 |
match row { |
| 276 |
Ok((used, cap)) => { |
| 277 |
gauge!("creator_storage_used_bytes_total").set(used as f64); |
| 278 |
gauge!("creator_storage_cap_bytes_total").set(cap as f64); |
| 279 |
let ratio = if cap > 0 { |
| 280 |
used as f64 / cap as f64 |
| 281 |
} else { |
| 282 |
0.0 |
| 283 |
}; |
| 284 |
gauge!("creator_storage_fill_ratio").set(ratio); |
| 285 |
} |
| 286 |
Err(e) => { |
| 287 |
tracing::debug!(error = ?e, "storage fill stats query failed"); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
pub fn record_domain_cache_size(size: usize) { |
| 295 |
gauge!("domain_cache_entries").set(size as f64); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
#[tracing::instrument(skip_all)] |
| 302 |
pub async fn record_custom_pages_stats(pool: &sqlx::PgPool) { |
| 303 |
let users: Result<(i64,), _> = |
| 304 |
sqlx::query_as("SELECT count(*) FROM users WHERE custom_html <> '' OR custom_css <> ''") |
| 305 |
.fetch_one(pool) |
| 306 |
.await; |
| 307 |
let projects: Result<(i64,), _> = |
| 308 |
sqlx::query_as("SELECT count(*) FROM projects WHERE custom_html <> '' OR custom_css <> ''") |
| 309 |
.fetch_one(pool) |
| 310 |
.await; |
| 311 |
match (users, projects) { |
| 312 |
(Ok((u,)), Ok((p,))) => { |
| 313 |
gauge!("custom_pages_active", "kind" => "profile").set(u as f64); |
| 314 |
gauge!("custom_pages_active", "kind" => "project").set(p as f64); |
| 315 |
} |
| 316 |
_ => tracing::debug!("custom-pages stats query failed"), |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
pub fn record_sanitizer_rejection(kind: &'static str) { |
| 324 |
counter!("custom_pages_sanitizer_rejections_total", "kind" => kind).increment(1); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
pub fn record_clamav_degraded_hold() { |
| 333 |
counter!("clamav_degraded_hold_total").increment(1); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
pub fn record_scan_verdict(verdict: &'static str) { |
| 342 |
counter!("scan_verdicts_total", "verdict" => verdict).increment(1); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
pub fn record_scan_duration(seconds: f64) { |
| 349 |
histogram!("scan_duration_seconds").record(seconds); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
pub async fn record_scan_queue_stats(pool: &sqlx::PgPool) { |
| 357 |
let pending = crate::db::scan_jobs::queued_count(pool).await.unwrap_or(0); |
| 358 |
let running = crate::db::scan_jobs::running_count(pool).await.unwrap_or(0); |
| 359 |
let stuck = crate::db::scan_jobs::stuck_count(pool, 300) |
| 360 |
.await |
| 361 |
.unwrap_or(0); |
| 362 |
gauge!("scan_queue_jobs", "state" => "pending").set(pending as f64); |
| 363 |
gauge!("scan_queue_jobs", "state" => "running").set(running as f64); |
| 364 |
gauge!("scan_queue_jobs", "state" => "stuck").set(stuck as f64); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
pub async fn idempotency_middleware( |
| 376 |
State(state): State<crate::AppState>, |
| 377 |
request: Request, |
| 378 |
next: Next, |
| 379 |
) -> Response { |
| 380 |
use axum::http::StatusCode; |
| 381 |
|
| 382 |
|
| 383 |
if !matches!( |
| 384 |
*request.method(), |
| 385 |
axum::http::Method::POST | axum::http::Method::PUT |
| 386 |
) { |
| 387 |
return next.run(request).await; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
let idem_key = request |
| 392 |
.headers() |
| 393 |
.get("idempotency-key") |
| 394 |
.and_then(|v| v.to_str().ok()) |
| 395 |
.map(std::string::ToString::to_string); |
| 396 |
|
| 397 |
let idem_key = match idem_key { |
| 398 |
Some(k) if !k.is_empty() && k.len() <= 256 => k, |
| 399 |
_ => return next.run(request).await, |
| 400 |
}; |
| 401 |
|
| 402 |
|
| 403 |
let session = request |
| 404 |
.extensions() |
| 405 |
.get::<tower_sessions::Session>() |
| 406 |
.cloned(); |
| 407 |
let user_id: Option<crate::db::UserId> = if let Some(ref session) = session { |
| 408 |
session |
| 409 |
.get::<crate::auth::SessionUser>("user") |
| 410 |
.await |
| 411 |
.ok() |
| 412 |
.flatten() |
| 413 |
.map(|u| u.id) |
| 414 |
} else { |
| 415 |
None |
| 416 |
}; |
| 417 |
|
| 418 |
let Some(user_id) = user_id else { |
| 419 |
|
| 420 |
return next.run(request).await; |
| 421 |
}; |
| 422 |
|
| 423 |
let method = request.method().to_string(); |
| 424 |
let path = request.uri().path().to_string(); |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
type NegKey = (String, crate::db::UserId); |
| 434 |
static NEG_CACHE: std::sync::OnceLock<dashmap::DashMap<NegKey, std::time::Instant>> = |
| 435 |
std::sync::OnceLock::new(); |
| 436 |
const NEG_TTL_SECS: u64 = 60; |
| 437 |
let neg_cache = NEG_CACHE.get_or_init(dashmap::DashMap::new); |
| 438 |
let neg_key = (idem_key.clone(), user_id); |
| 439 |
let recently_negative = neg_cache |
| 440 |
.get(&neg_key) |
| 441 |
.is_some_and(|e| e.elapsed().as_secs() < NEG_TTL_SECS); |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
const NEG_CACHE_MAX_ENTRIES: usize = 8192; |
| 449 |
static GC_TICK: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |
| 450 |
let tick = GC_TICK.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 451 |
if tick.is_multiple_of(1024) || neg_cache.len() > NEG_CACHE_MAX_ENTRIES { |
| 452 |
neg_cache.retain(|_, t| t.elapsed().as_secs() < NEG_TTL_SECS); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
if !recently_negative |
| 457 |
&& let Ok(Some(cached)) = crate::db::idempotency::get_cached_response( |
| 458 |
&state.db, &idem_key, user_id, &method, &path, |
| 459 |
) |
| 460 |
.await |
| 461 |
{ |
| 462 |
tracing::debug!(key = %idem_key, "returning cached idempotency response"); |
| 463 |
let status = StatusCode::from_u16(cached.status_code as u16).unwrap_or(StatusCode::OK); |
| 464 |
return (status, cached.response_body).into_response(); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
neg_cache.insert(neg_key.clone(), std::time::Instant::now()); |
| 470 |
|
| 471 |
|
| 472 |
let response = next.run(request).await; |
| 473 |
|
| 474 |
|
| 475 |
let status_code = response.status().as_u16(); |
| 476 |
|
| 477 |
|
| 478 |
if status_code < 400 { |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
let content_length = response |
| 484 |
.headers() |
| 485 |
.get(axum::http::header::CONTENT_LENGTH) |
| 486 |
.and_then(|v| v.to_str().ok()) |
| 487 |
.and_then(|v| v.parse::<usize>().ok()); |
| 488 |
let Some(len) = content_length else { |
| 489 |
tracing::debug!( |
| 490 |
key = %idem_key, method = %method, path = %path, |
| 491 |
"no content-length on response; skipping idempotency cache (body left intact)" |
| 492 |
); |
| 493 |
return response; |
| 494 |
}; |
| 495 |
if len > 1024 * 1024 { |
| 496 |
tracing::info!( |
| 497 |
key = %idem_key, method = %method, path = %path, len, |
| 498 |
"response body exceeds 1MB; skipping idempotency cache" |
| 499 |
); |
| 500 |
return response; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
let (parts, body) = response.into_parts(); |
| 507 |
let body_bytes = match axum::body::to_bytes(body, 1024 * 1024).await { |
| 508 |
Ok(b) => b, |
| 509 |
Err(e) => { |
| 510 |
tracing::error!( |
| 511 |
key = %idem_key, method = %method, path = %path, error = ?e, |
| 512 |
"response body exceeded 1MB despite content-length <= 1MB; failing closed" |
| 513 |
); |
| 514 |
return axum::response::Response::builder() |
| 515 |
.status(StatusCode::INTERNAL_SERVER_ERROR) |
| 516 |
.body(axum::body::Body::from("internal error")) |
| 517 |
.unwrap_or_else(|_| StatusCode::INTERNAL_SERVER_ERROR.into_response()); |
| 518 |
} |
| 519 |
}; |
| 520 |
|
| 521 |
if let Ok(body_str) = std::str::from_utf8(&body_bytes) { |
| 522 |
let body_owned = body_str.to_owned(); |
| 523 |
let db = state.db.clone(); |
| 524 |
let key = idem_key.clone(); |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
neg_cache.remove(&neg_key); |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
state.bg.spawn("idempotency_store", async move { |
| 535 |
if let Err(e) = crate::db::idempotency::store_response( |
| 536 |
&db, |
| 537 |
&key, |
| 538 |
user_id, |
| 539 |
&method, |
| 540 |
&path, |
| 541 |
status_code, |
| 542 |
&body_owned, |
| 543 |
) |
| 544 |
.await |
| 545 |
{ |
| 546 |
tracing::warn!(key = %key, error = ?e, "failed to store idempotency key"); |
| 547 |
} |
| 548 |
}); |
| 549 |
} |
| 550 |
|
| 551 |
axum::response::Response::from_parts(parts, axum::body::Body::from(body_bytes)) |
| 552 |
} else { |
| 553 |
response |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
pub struct MetricsSnapshot { |
| 559 |
pub total_requests: u64, |
| 560 |
pub total_5xx: u64, |
| 561 |
pub total_errors: u64, |
| 562 |
|
| 563 |
pub top_routes: Vec<(String, String, String, u64)>, |
| 564 |
|
| 565 |
pub error_breakdown: Vec<(String, u64)>, |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
pub fn snapshot(handle: &PrometheusHandle) -> MetricsSnapshot { |
| 572 |
let text = handle.render(); |
| 573 |
let mut total_requests: u64 = 0; |
| 574 |
let mut total_5xx: u64 = 0; |
| 575 |
let mut routes: Vec<(String, String, String, u64)> = Vec::new(); |
| 576 |
let mut errors: Vec<(String, u64)> = Vec::new(); |
| 577 |
|
| 578 |
for line in text.lines() { |
| 579 |
if line.starts_with('#') || line.is_empty() { |
| 580 |
continue; |
| 581 |
} |
| 582 |
|
| 583 |
if let Some(rest) = line.strip_prefix("http_requests_total{") { |
| 584 |
if let Some((labels, value)) = rest.rsplit_once("} ") { |
| 585 |
let count: u64 = value.parse().unwrap_or(0); |
| 586 |
let method = extract_label(labels, "method"); |
| 587 |
let path = extract_label(labels, "path"); |
| 588 |
let status = extract_label(labels, "status"); |
| 589 |
total_requests += count; |
| 590 |
if status == "5xx" { |
| 591 |
total_5xx += count; |
| 592 |
} |
| 593 |
routes.push((method, path, status, count)); |
| 594 |
} |
| 595 |
} else if let Some(rest) = line.strip_prefix("http_errors_total{") |
| 596 |
&& let Some((labels, value)) = rest.rsplit_once("} ") |
| 597 |
{ |
| 598 |
let count: u64 = value.parse().unwrap_or(0); |
| 599 |
let kind = extract_label(labels, "kind"); |
| 600 |
errors.push((kind, count)); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
routes.sort_by_key(|r| std::cmp::Reverse(r.3)); |
| 605 |
routes.truncate(20); |
| 606 |
errors.sort_by_key(|e| std::cmp::Reverse(e.1)); |
| 607 |
|
| 608 |
let total_errors = errors.iter().map(|(_, c)| c).sum(); |
| 609 |
|
| 610 |
MetricsSnapshot { |
| 611 |
total_requests, |
| 612 |
total_5xx, |
| 613 |
total_errors, |
| 614 |
top_routes: routes, |
| 615 |
error_breakdown: errors, |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
fn extract_label(labels: &str, key: &str) -> String { |
| 621 |
let prefix = format!("{key}=\""); |
| 622 |
labels |
| 623 |
.split(',') |
| 624 |
.find_map(|part| { |
| 625 |
let part = part.trim(); |
| 626 |
part.strip_prefix(&prefix) |
| 627 |
.and_then(|rest| rest.strip_suffix('"')) |
| 628 |
.map(std::string::ToString::to_string) |
| 629 |
}) |
| 630 |
.unwrap_or_default() |
| 631 |
} |
| 632 |
|
| 633 |
#[cfg(test)] |
| 634 |
mod tests { |
| 635 |
use super::*; |
| 636 |
|
| 637 |
#[test] |
| 638 |
fn status_class_mapping() { |
| 639 |
assert_eq!(status_class(200), "2xx"); |
| 640 |
assert_eq!(status_class(201), "2xx"); |
| 641 |
assert_eq!(status_class(301), "3xx"); |
| 642 |
assert_eq!(status_class(404), "4xx"); |
| 643 |
assert_eq!(status_class(500), "5xx"); |
| 644 |
assert_eq!(status_class(100), "other"); |
| 645 |
} |
| 646 |
|
| 647 |
#[test] |
| 648 |
fn public_page_anonymous_is_cdn_cacheable() { |
| 649 |
let v = cache_control_value("/u/alice", true, false, false); |
| 650 |
assert!( |
| 651 |
v.starts_with("public"), |
| 652 |
"anonymous public page should be CDN-cacheable: {v}" |
| 653 |
); |
| 654 |
} |
| 655 |
|
| 656 |
#[test] |
| 657 |
fn public_page_authed_viewer_is_private() { |
| 658 |
|
| 659 |
|
| 660 |
assert_eq!( |
| 661 |
cache_control_value("/i/thing", true, false, true), |
| 662 |
"private, no-cache" |
| 663 |
); |
| 664 |
assert_eq!( |
| 665 |
cache_control_value("/p/proj", true, false, true), |
| 666 |
"private, no-cache" |
| 667 |
); |
| 668 |
} |
| 669 |
|
| 670 |
#[test] |
| 671 |
fn public_page_that_sets_a_cookie_is_private() { |
| 672 |
|
| 673 |
|
| 674 |
assert_eq!( |
| 675 |
cache_control_value("/u/alice", true, true, false), |
| 676 |
"private, no-cache" |
| 677 |
); |
| 678 |
} |
| 679 |
|
| 680 |
#[test] |
| 681 |
fn api_routes_are_no_store() { |
| 682 |
assert_eq!( |
| 683 |
cache_control_value("/api/items", false, false, false), |
| 684 |
"no-store" |
| 685 |
); |
| 686 |
assert_eq!( |
| 687 |
cache_control_value("/stripe/webhook", false, false, false), |
| 688 |
"no-store" |
| 689 |
); |
| 690 |
assert_eq!( |
| 691 |
cache_control_value("/postmark/inbound", false, false, false), |
| 692 |
"no-store" |
| 693 |
); |
| 694 |
} |
| 695 |
|
| 696 |
#[test] |
| 697 |
fn private_routes_default_to_no_cache() { |
| 698 |
assert_eq!( |
| 699 |
cache_control_value("/dashboard", false, false, true), |
| 700 |
"private, no-cache" |
| 701 |
); |
| 702 |
assert_eq!( |
| 703 |
cache_control_value("/settings", false, false, false), |
| 704 |
"private, no-cache" |
| 705 |
); |
| 706 |
} |
| 707 |
} |
| 708 |
|