1use std::collections::HashMap;
35use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, Ordering};
36use std::sync::{Mutex, OnceLock};
37use std::time::Instant;
38
39use crate::application::Application;
40use crate::middleware::Middleware;
41use crate::request::Request;
42use crate::response::Response;
43use crate::server::ConnectionInfo;
44
45pub mod exposition;
46pub mod http;
47#[cfg(not(target_arch = "wasm32"))]
48pub mod llm;
49pub mod tls;
50
51pub static SERVER_READY: AtomicBool = AtomicBool::new(false);
58
59pub static REQUESTS_TOTAL: AtomicU64 = AtomicU64::new(0);
61
62pub static ERRORS_TOTAL: AtomicU64 = AtomicU64::new(0);
64
65pub static ACTIVE_CONNECTIONS: AtomicI64 = AtomicI64::new(0);
67
68pub static REQUESTS_IN_FLIGHT: AtomicI64 = AtomicI64::new(0);
71
72pub struct InFlight(());
74
75impl InFlight {
76 pub fn start() -> Self {
77 REQUESTS_IN_FLIGHT.fetch_add(1, Ordering::SeqCst);
78 InFlight(())
79 }
80}
81
82impl Drop for InFlight {
83 fn drop(&mut self) {
84 REQUESTS_IN_FLIGHT.fetch_sub(1, Ordering::SeqCst);
85 }
86}
87
88pub static THREAD_POOL_QUEUED: AtomicI64 = AtomicI64::new(0);
90
91thread_local! {
92 static UNCOUNTED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
93}
94
95#[doc(hidden)]
100pub fn set_thread_uncounted(uncounted: bool) {
101 UNCOUNTED.with(|u| u.set(uncounted));
102}
103
104#[doc(hidden)]
106pub fn thread_uncounted() -> bool {
107 UNCOUNTED.with(|u| u.get())
108}
109
110pub fn record_request() {
111 if thread_uncounted() {
112 return;
113 }
114 REQUESTS_TOTAL.fetch_add(1, Ordering::Relaxed);
115}
116
117pub fn record_error() {
118 if thread_uncounted() {
119 return;
120 }
121 ERRORS_TOTAL.fetch_add(1, Ordering::Relaxed);
122}
123
124pub fn connection_open() {
125 ACTIVE_CONNECTIONS.fetch_add(1, Ordering::Relaxed);
126}
127
128pub fn connection_close() {
129 ACTIVE_CONNECTIONS.fetch_sub(1, Ordering::Relaxed);
130}
131
132const BUCKET_BOUNDS: [f64; 11] = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0];
137
138struct HistogramEntry {
139 buckets: [u64; 11],
141 sum: f64,
142 count: u64,
143}
144
145impl HistogramEntry {
146 fn new() -> Self {
147 HistogramEntry { buckets: [0; 11], sum: 0.0, count: 0 }
148 }
149
150 fn observe(&mut self, secs: f64) {
151 for (i, &upper) in BUCKET_BOUNDS.iter().enumerate() {
152 if secs <= upper {
153 self.buckets[i] += 1;
154 }
155 }
156 self.sum += secs;
157 self.count += 1;
158 }
159}
160
161struct RouteEntry {
162 counts: HashMap<i16, u64>,
163 latency: HistogramEntry,
164}
165
166impl RouteEntry {
167 fn new() -> Self {
168 RouteEntry { counts: HashMap::new(), latency: HistogramEntry::new() }
169 }
170}
171
172struct RouteStore {
173 entries: HashMap<(String, String), RouteEntry>,
175}
176
177static ROUTE_STORE: OnceLock<Mutex<RouteStore>> = OnceLock::new();
178
179fn route_store() -> &'static Mutex<RouteStore> {
180 ROUTE_STORE.get_or_init(|| Mutex::new(RouteStore { entries: HashMap::new() }))
181}
182
183pub fn record_route(method: &str, path: &str, status: i16, elapsed_secs: f64) {
188 let key = (method.to_string(), path.to_string());
189 let mut guard = route_store().lock().unwrap();
190 let entry = guard.entries.entry(key).or_insert_with(RouteEntry::new);
191 *entry.counts.entry(status).or_insert(0) += 1;
192 entry.latency.observe(elapsed_secs);
193}
194
195fn strip_query(uri: &str) -> &str {
197 match uri.find('?') {
198 Some(i) => &uri[..i],
199 None => uri,
200 }
201}
202
203pub struct MetricsLayer;
219
220impl Middleware for MetricsLayer {
221 fn accepts_streamed_body(&self, _request: &Request, _connection: &ConnectionInfo) -> bool {
223 true
224 }
225
226
227 fn handle(
228 &self,
229 request: &Request,
230 connection: &ConnectionInfo,
231 next: &dyn Application,
232 ) -> Result<Response, String> {
233 let start = Instant::now();
234 let result = next.execute(request, connection);
235 let elapsed = start.elapsed().as_secs_f64();
236
237 let path = strip_query(&request.request_uri).to_string();
238 let status = match &result {
239 Ok(r) => r.status_code,
240 Err(_) => 500,
241 };
242 record_route(&request.method, &path, status, elapsed);
243
244 result
245 }
246
247 fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool {
249 true
250 }
251}
252
253#[cfg(test)]
256mod tests;
257
258pub fn prometheus_text() -> String {
260 let mut out = prometheus_text_without_http();
261 let http_text = http::prometheus_text();
262 if !http_text.is_empty() {
263 out.push('\n');
264 out.push_str(&http_text);
265 }
266 out
267}
268
269fn prometheus_text_without_http() -> String {
272 let requests = REQUESTS_TOTAL.load(Ordering::Relaxed);
273 let errors = ERRORS_TOTAL.load(Ordering::Relaxed);
274 let active = ACTIVE_CONNECTIONS.load(Ordering::Relaxed);
275
276 let mut out = format!(
277 "# HELP rws_requests_total Total HTTP requests handled\n\
278 # TYPE rws_requests_total counter\n\
279 rws_requests_total {}\n\n\
280 # HELP rws_errors_total HTTP requests that returned an application error\n\
281 # TYPE rws_errors_total counter\n\
282 rws_errors_total {}\n\n\
283 # HELP rws_active_connections Currently open connections\n\
284 # TYPE rws_active_connections gauge\n\
285 rws_active_connections {}\n",
286 requests, errors, active
287 );
288
289 let mut sections: Vec<(u16, fn() -> String)> = vec![
292 (section::ROUTES, route_prometheus_text),
293 (section::DISK, crate::disk_guard::prometheus_text),
294 (section::EARLY_HINTS, crate::early_hints::prometheus_text),
295 (section::TLS, tls::prometheus_text),
296 (section::CIRCUIT_BREAKER, circuit_breaker_prometheus_text),
297 ];
298 #[cfg(not(target_arch = "wasm32"))]
299 sections.push((section::LLM, llm::prometheus_text));
300 sections.extend(registered_sections());
301 sections.sort_by_key(|(order, _)| *order);
302 for (_, text) in sections {
303 let text = text();
304 if !text.is_empty() {
305 out.push('\n');
306 out.push_str(&text);
307 }
308 }
309
310 out
311}
312
313#[doc(hidden)]
316pub mod section {
317 pub const ROUTES: u16 = 10;
318 pub const UPSTREAMS: u16 = 20;
319 pub const WAF: u16 = 30;
320 pub const MIRROR: u16 = 40;
321 pub const DISK: u16 = 50;
322 pub const EARLY_HINTS: u16 = 60;
323 pub const WASM_PLUGINS: u16 = 70;
324 pub const LLM: u16 = 80;
325 pub const TLS: u16 = 90;
326 pub const CIRCUIT_BREAKER: u16 = 100;
327 pub const CONTROLLERS: u16 = 110;
329}
330
331static SECTIONS: std::sync::Mutex<Vec<(u16, fn() -> String)>> = std::sync::Mutex::new(Vec::new());
332
333#[doc(hidden)]
337pub fn register_section(order: u16, text: fn() -> String) {
338 let mut sections = SECTIONS.lock().unwrap_or_else(|e| e.into_inner());
339 if !sections.iter().any(|(o, _)| *o == order) {
340 sections.push((order, text));
341 }
342}
343
344fn registered_sections() -> Vec<(u16, fn() -> String)> {
345 SECTIONS.lock().unwrap_or_else(|e| e.into_inner()).clone()
346}
347
348#[cfg(target_arch = "wasm32")]
361fn circuit_breaker_prometheus_text() -> String {
362 String::new()
363}
364
365#[cfg(not(target_arch = "wasm32"))]
366fn circuit_breaker_prometheus_text() -> String {
367 let mut states = crate::circuit_breaker::global().lock().unwrap().all_states();
368 if states.is_empty() {
369 return String::new();
370 }
371 states.sort_by(|a, b| a.0.cmp(&b.0));
372
373 let mut out = String::new();
374 out.push_str("# HELP rws_circuit_breaker_state Circuit breaker state per backend (0=closed, 1=half_open, 2=open)\n");
375 out.push_str("# TYPE rws_circuit_breaker_state gauge\n");
376 for (backend, state) in states {
377 let value = match state {
378 crate::circuit_breaker::BreakerState::Closed => 0,
379 crate::circuit_breaker::BreakerState::HalfOpen => 1,
380 crate::circuit_breaker::BreakerState::Open => 2,
381 };
382 out.push_str(&format!("rws_circuit_breaker_state{{backend=\"{}\"}} {}\n", backend, value));
383 }
384 out
385}
386
387fn route_prometheus_text() -> String {
388 let guard = route_store().lock().unwrap();
389 if guard.entries.is_empty() {
390 return String::new();
391 }
392
393 let mut keys: Vec<&(String, String)> = guard.entries.keys().collect();
395 keys.sort();
396
397 let mut out = String::new();
398
399 out.push_str("# HELP rws_route_requests_total Total requests handled per route\n");
401 out.push_str("# TYPE rws_route_requests_total counter\n");
402 for key in &keys {
403 let entry = &guard.entries[*key];
404 let (method, path) = key;
405 let mut statuses: Vec<i16> = entry.counts.keys().cloned().collect();
406 statuses.sort();
407 for status in statuses {
408 let count = entry.counts[&status];
409 out.push_str(&format!(
410 "rws_route_requests_total{{method=\"{}\",path=\"{}\",status=\"{}\"}} {}\n",
411 method, path, status, count,
412 ));
413 }
414 }
415
416 out.push('\n');
418 out.push_str("# HELP rws_route_duration_seconds Request duration in seconds per route\n");
419 out.push_str("# TYPE rws_route_duration_seconds histogram\n");
420 for key in &keys {
421 let entry = &guard.entries[*key];
422 let (method, path) = key;
423 let lat = &entry.latency;
424
425 for (i, &upper) in BUCKET_BOUNDS.iter().enumerate() {
426 out.push_str(&format!(
427 "rws_route_duration_seconds_bucket{{method=\"{}\",path=\"{}\",le=\"{}\"}} {}\n",
428 method, path, upper, lat.buckets[i],
429 ));
430 }
431 out.push_str(&format!(
432 "rws_route_duration_seconds_bucket{{method=\"{}\",path=\"{}\",le=\"+Inf\"}} {}\n",
433 method, path, lat.count,
434 ));
435 out.push_str(&format!(
436 "rws_route_duration_seconds_sum{{method=\"{}\",path=\"{}\"}} {:.9}\n",
437 method, path, lat.sum,
438 ));
439 out.push_str(&format!(
440 "rws_route_duration_seconds_count{{method=\"{}\",path=\"{}\"}} {}\n",
441 method, path, lat.count,
442 ));
443 }
444
445 out
446}