Metrics for cache

This commit is contained in:
Ara Sadoyan
2026-10-23 19:05:01 +02:00
parent 72756a00dd
commit e9143e8f99
4 changed files with 21 additions and 2 deletions
-1
View File
@@ -107,7 +107,6 @@ impl Storage for RedisCache {
async fn lookup(&'static self, key: &CacheKey, _trace: &SpanHandle) -> Result<Option<(CacheMeta, HitHandler)>> { async fn lookup(&'static self, key: &CacheKey, _trace: &SpanHandle) -> Result<Option<(CacheMeta, HitHandler)>> {
let base = Self::make_key(key); let base = Self::make_key(key);
// Pre-allocate exact capacities to prevent reallocations
let mut meta_key = String::with_capacity(base.len() + 5); let mut meta_key = String::with_capacity(base.len() + 5);
meta_key.push_str(&base); meta_key.push_str(&base);
meta_key.push_str(":meta"); meta_key.push_str(":meta");
+1
View File
@@ -352,6 +352,7 @@ impl ProxyHttp for LB {
code: session.response_written().map(|resp| resp.status), code: session.response_written().map(|resp| resp.status),
latency: ctx.start_time.elapsed(), latency: ctx.start_time.elapsed(),
version: session.req_header().version, version: session.req_header().version,
cache: session.cache.phase(),
upstream: ctx.hostname.take().unwrap_or_else(|| LOCALHOST.clone()), upstream: ctx.hostname.take().unwrap_or_else(|| LOCALHOST.clone()),
}; };
calc_metrics(m); calc_metrics(m);
-1
View File
@@ -274,7 +274,6 @@ pub async fn access_log(response_code: u16, summary: &str, session: &Session) {
.unwrap_or(IpAddr::V4(Ipv4Addr::LOCALHOST)); .unwrap_or(IpAddr::V4(Ipv4Addr::LOCALHOST));
let user_agent = session.req_header().headers.get("user-agent").and_then(|v| v.to_str().ok()).unwrap_or("-"); let user_agent = session.req_header().headers.get("user-agent").and_then(|v| v.to_str().ok()).unwrap_or("-");
let msg = LogMessage { let msg = LogMessage {
response_code, response_code,
summary: summary.to_owned(), summary: summary.to_owned(),
+20
View File
@@ -1,5 +1,6 @@
use crate::utils::lazylock::EVICTION; use crate::utils::lazylock::EVICTION;
use crate::utils::tools::Die; use crate::utils::tools::Die;
use pingora_cache::CachePhase;
use pingora_cache::eviction::EvictionManager; use pingora_cache::eviction::EvictionManager;
use pingora_http::Method; use pingora_http::Method;
use pingora_http::StatusCode; use pingora_http::StatusCode;
@@ -11,6 +12,7 @@ use std::time::Duration;
pub struct MetricTypes { pub struct MetricTypes {
pub method: Method, pub method: Method,
pub cache: CachePhase,
pub upstream: Arc<str>, pub upstream: Arc<str>,
pub code: Option<StatusCode>, pub code: Option<StatusCode>,
pub latency: Duration, pub latency: Duration,
@@ -53,6 +55,9 @@ pub static REQUESTS_BY_METHOD: LazyLock<IntCounterVec> = LazyLock::new(|| {
register_int_counter_vec!("aralez_requests_by_method_total", "Number of requests by HTTP method", &["method"]).die("Failed to register REQUESTS_BY_METHOD metric") register_int_counter_vec!("aralez_requests_by_method_total", "Number of requests by HTTP method", &["method"]).die("Failed to register REQUESTS_BY_METHOD metric")
}); });
pub static CACHE_PHASE: LazyLock<IntCounterVec> =
LazyLock::new(|| register_int_counter_vec!("aralez_cache_status", "Phases of cached responses", &["phase"]).die("Failed to register CACHE_PHASE metric"));
pub static REQUESTS_BY_UPSTREAM: LazyLock<IntCounterVec> = LazyLock::new(|| { pub static REQUESTS_BY_UPSTREAM: LazyLock<IntCounterVec> = LazyLock::new(|| {
register_int_counter_vec!("aralez_requests_by_upstream", "Number of requests by UPSTREAM server", &["upstream"]).die("Failed to register REQUESTS_BY_UPSTREAM metric") register_int_counter_vec!("aralez_requests_by_upstream", "Number of requests by UPSTREAM server", &["upstream"]).die("Failed to register REQUESTS_BY_UPSTREAM metric")
}); });
@@ -66,6 +71,12 @@ pub static HTTP_30_COUNT: LazyLock<IntCounter> = LazyLock::new(|| REQUESTS_BY_VE
pub static HTTP_10_COUNT: LazyLock<IntCounter> = LazyLock::new(|| REQUESTS_BY_VERSION.with_label_values(&["HTTP/1.0"])); pub static HTTP_10_COUNT: LazyLock<IntCounter> = LazyLock::new(|| REQUESTS_BY_VERSION.with_label_values(&["HTTP/1.0"]));
pub static HTTP_UNK_COUNT: LazyLock<IntCounter> = LazyLock::new(|| REQUESTS_BY_VERSION.with_label_values(&["Unknown"])); pub static HTTP_UNK_COUNT: LazyLock<IntCounter> = LazyLock::new(|| REQUESTS_BY_VERSION.with_label_values(&["Unknown"]));
pub static CACHE_HIT: LazyLock<IntCounter> = LazyLock::new(|| CACHE_PHASE.with_label_values(&["hit"]));
pub static CACHE_MISS: LazyLock<IntCounter> = LazyLock::new(|| CACHE_PHASE.with_label_values(&["miss"]));
pub static CACHE_EXPIRED: LazyLock<IntCounter> = LazyLock::new(|| CACHE_PHASE.with_label_values(&["expired"]));
pub static CACHE_REVALIDATED: LazyLock<IntCounter> = LazyLock::new(|| CACHE_PHASE.with_label_values(&["revalidated"]));
pub static CACHE_DISABLED: LazyLock<IntCounter> = LazyLock::new(|| CACHE_PHASE.with_label_values(&["disabled"]));
pub static METHOD_GET: LazyLock<IntCounter> = LazyLock::new(|| REQUESTS_BY_METHOD.with_label_values(&["GET"])); pub static METHOD_GET: LazyLock<IntCounter> = LazyLock::new(|| REQUESTS_BY_METHOD.with_label_values(&["GET"]));
pub static METHOD_POST: LazyLock<IntCounter> = LazyLock::new(|| REQUESTS_BY_METHOD.with_label_values(&["POST"])); pub static METHOD_POST: LazyLock<IntCounter> = LazyLock::new(|| REQUESTS_BY_METHOD.with_label_values(&["POST"]));
pub static METHOD_PUT: LazyLock<IntCounter> = LazyLock::new(|| REQUESTS_BY_METHOD.with_label_values(&["PUT"])); pub static METHOD_PUT: LazyLock<IntCounter> = LazyLock::new(|| REQUESTS_BY_METHOD.with_label_values(&["PUT"]));
@@ -128,6 +139,15 @@ pub fn calc_metrics(metric_types: &MetricTypes) {
_ => RESPONSE_CODES.with_label_values(&[code.as_str()]).inc(), // Fallback for rare status codes _ => RESPONSE_CODES.with_label_values(&[code.as_str()]).inc(), // Fallback for rare status codes
} }
match metric_types.cache.as_str() {
"disabled" => CACHE_DISABLED.inc(),
"hit" => CACHE_HIT.inc(),
"miss" => CACHE_MISS.inc(),
"expired" => CACHE_EXPIRED.inc(),
"revalidated" => CACHE_REVALIDATED.inc(),
_ => CACHE_PHASE.with_label_values(&[metric_types.cache.as_str()]).inc(),
}
// CACHE_PHASE.with_label_values(&[metric_types.cache.as_str()]).inc();
REQUESTS_BY_UPSTREAM.with_label_values(&[metric_types.upstream.as_ref()]).inc(); REQUESTS_BY_UPSTREAM.with_label_values(&[metric_types.upstream.as_ref()]).inc();
RESPONSE_LATENCY.observe(metric_types.latency.as_secs_f64()); RESPONSE_LATENCY.observe(metric_types.latency.as_secs_f64());
} }