140 lines
4.6 KiB
Rust
140 lines
4.6 KiB
Rust
use http::{Method, StatusCode};
|
|
use serde_json::{json, Value};
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
pub(super) enum ResponseKind {
|
|
Static,
|
|
Php,
|
|
}
|
|
|
|
pub(crate) const PHP_FAILURE_NAMES: [&str; 9] = [
|
|
"capacity",
|
|
"connect_timeout",
|
|
"connect_error",
|
|
"request_timeout",
|
|
"request_io",
|
|
"response_timeout",
|
|
"response_io",
|
|
"invalid_response",
|
|
"body_io",
|
|
];
|
|
|
|
#[derive(Debug, Default)]
|
|
pub(crate) struct PhpMetrics {
|
|
pub(crate) requests: AtomicU64,
|
|
pub(crate) status_2xx: AtomicU64,
|
|
pub(crate) status_3xx: AtomicU64,
|
|
pub(crate) status_4xx: AtomicU64,
|
|
pub(crate) status_5xx: AtomicU64,
|
|
pub(crate) response_headers_us: AtomicU64,
|
|
pub(crate) response_bytes: AtomicU64,
|
|
pub(crate) in_flight_headers: AtomicU64,
|
|
pub(crate) static_requests: AtomicU64,
|
|
pub(crate) php_requests: AtomicU64,
|
|
pub(crate) other_requests: AtomicU64,
|
|
pub(crate) methods: [AtomicU64; 5],
|
|
pub(crate) selected_status: [AtomicU64; 8],
|
|
pub(crate) failures: [AtomicU64; 9],
|
|
pub(crate) compression: [AtomicU64; 4],
|
|
}
|
|
|
|
pub(crate) struct HeadersInFlight<'a>(&'a AtomicU64);
|
|
impl<'a> HeadersInFlight<'a> {
|
|
pub(crate) fn new(counter: &'a AtomicU64) -> Self {
|
|
counter.fetch_add(1, Ordering::Relaxed);
|
|
Self(counter)
|
|
}
|
|
}
|
|
impl Drop for HeadersInFlight<'_> {
|
|
fn drop(&mut self) {
|
|
self.0.fetch_sub(1, Ordering::Relaxed);
|
|
}
|
|
}
|
|
|
|
impl PhpMetrics {
|
|
pub(crate) fn record(&self, status: StatusCode, elapsed_us: u64) {
|
|
self.requests.fetch_add(1, Ordering::Relaxed);
|
|
let counter = match status.as_u16() / 100 {
|
|
2 => Some(&self.status_2xx),
|
|
3 => Some(&self.status_3xx),
|
|
4 => Some(&self.status_4xx),
|
|
5 => Some(&self.status_5xx),
|
|
_ => None,
|
|
};
|
|
if let Some(counter) = counter {
|
|
counter.fetch_add(1, Ordering::Relaxed);
|
|
}
|
|
self.response_headers_us
|
|
.fetch_add(elapsed_us, Ordering::Relaxed);
|
|
}
|
|
|
|
pub(super) fn record_web(
|
|
&self,
|
|
status: StatusCode,
|
|
elapsed_us: u64,
|
|
method: &Method,
|
|
kind: Option<ResponseKind>,
|
|
) {
|
|
self.record(status, elapsed_us);
|
|
let method_index = if method == Method::GET {
|
|
0
|
|
} else if method == Method::POST {
|
|
1
|
|
} else if method == Method::PUT {
|
|
2
|
|
} else if method == Method::DELETE {
|
|
3
|
|
} else {
|
|
4
|
|
};
|
|
self.methods[method_index].fetch_add(1, Ordering::Relaxed);
|
|
match kind {
|
|
Some(ResponseKind::Static) => &self.static_requests,
|
|
Some(ResponseKind::Php) => &self.php_requests,
|
|
None => &self.other_requests,
|
|
}
|
|
.fetch_add(1, Ordering::Relaxed);
|
|
let status_index = [200, 206, 304, 403, 404, 500, 502, 503]
|
|
.iter()
|
|
.position(|code| *code == status.as_u16());
|
|
if let Some(index) = status_index {
|
|
self.selected_status[index].fetch_add(1, Ordering::Relaxed);
|
|
}
|
|
}
|
|
|
|
pub(crate) fn failure(&self, index: usize) {
|
|
if let Some(counter) = self.failures.get(index) {
|
|
counter.fetch_add(1, Ordering::Relaxed);
|
|
}
|
|
}
|
|
|
|
pub(crate) fn record_encoding(&self, encoding: crate::compression::AppliedEncoding) {
|
|
let index = match encoding {
|
|
crate::compression::AppliedEncoding::Gzip => 0,
|
|
crate::compression::AppliedEncoding::Brotli => 1,
|
|
crate::compression::AppliedEncoding::Zstd => 2,
|
|
crate::compression::AppliedEncoding::Deflate => 3,
|
|
};
|
|
self.compression[index].fetch_add(1, Ordering::Relaxed);
|
|
}
|
|
|
|
pub(crate) fn view(&self) -> Value {
|
|
let n = |counter: &AtomicU64| counter.load(Ordering::Relaxed);
|
|
json!({
|
|
"requests": n(&self.requests),
|
|
"status_2xx": n(&self.status_2xx), "status_3xx": n(&self.status_3xx),
|
|
"status_4xx": n(&self.status_4xx), "status_5xx": n(&self.status_5xx),
|
|
"response_headers_us": n(&self.response_headers_us),
|
|
"response_bytes": n(&self.response_bytes),
|
|
"active_headers": n(&self.in_flight_headers),
|
|
"static_requests": n(&self.static_requests),
|
|
"php_requests": n(&self.php_requests), "other_requests": n(&self.other_requests),
|
|
"methods": self.methods.iter().map(n).collect::<Vec<_>>(),
|
|
"selected_status": self.selected_status.iter().map(n).collect::<Vec<_>>(),
|
|
"failures": self.failures.iter().map(n).collect::<Vec<_>>(),
|
|
"compression": self.compression.iter().map(n).collect::<Vec<_>>()
|
|
})
|
|
}
|
|
}
|