use std::time::Duration;
#[cfg(feature = "metrics")]
use crate::context::CacheContext;
#[cfg(not(feature = "metrics"))]
use crate::context::CacheContext;
#[cfg(feature = "metrics")]
use lazy_static::lazy_static;
#[cfg(feature = "metrics")]
lazy_static! {
pub static ref CACHE_HIT_COUNTER: &'static str = {
metrics::describe_counter!(
"hitbox_cache_hit_total",
"Total number of cache hit events."
);
"hitbox_cache_hit_total"
};
pub static ref CACHE_MISS_COUNTER: &'static str = {
metrics::describe_counter!(
"hitbox_cache_miss_total",
"Total number of cache miss events."
);
"hitbox_cache_miss_total"
};
pub static ref CACHE_STALE_COUNTER: &'static str = {
metrics::describe_counter!(
"hitbox_cache_stale_total",
"Total number of cache stale events."
);
"hitbox_cache_stale_total"
};
pub static ref CACHE_REQUEST_DURATION: &'static str = {
metrics::describe_histogram!(
"hitbox_request_duration_seconds",
metrics::Unit::Seconds,
"Duration of cache requests in seconds."
);
"hitbox_request_duration_seconds"
};
pub static ref CACHE_UPSTREAM_HANDLING_HISTOGRAM: &'static str = {
metrics::describe_histogram!(
"hitbox_upstream_duration_seconds",
metrics::Unit::Seconds,
"Duration of upstream requests in seconds."
);
"hitbox_upstream_duration_seconds"
};
pub static ref OFFLOAD_TASKS_SPAWNED: &'static str = {
metrics::describe_counter!(
"hitbox_offload_tasks_spawned_total",
"Total number of offload tasks spawned."
);
"hitbox_offload_tasks_spawned_total"
};
pub static ref OFFLOAD_TASKS_COMPLETED: &'static str = {
metrics::describe_counter!(
"hitbox_offload_tasks_completed_total",
"Total number of offload tasks completed successfully."
);
"hitbox_offload_tasks_completed_total"
};
pub static ref OFFLOAD_TASKS_TIMEOUT: &'static str = {
metrics::describe_counter!(
"hitbox_offload_tasks_timeout_total",
"Total number of offload tasks that timed out."
);
"hitbox_offload_tasks_timeout_total"
};
pub static ref OFFLOAD_TASKS_DEDUPLICATED: &'static str = {
metrics::describe_counter!(
"hitbox_offload_tasks_deduplicated_total",
"Total number of offload tasks deduplicated (skipped because already in flight)."
);
"hitbox_offload_tasks_deduplicated_total"
};
pub static ref OFFLOAD_TASKS_ACTIVE: &'static str = {
metrics::describe_gauge!(
"hitbox_offload_tasks_active",
"Number of currently active offload tasks."
);
"hitbox_offload_tasks_active"
};
pub static ref OFFLOAD_TASK_DURATION: &'static str = {
metrics::describe_histogram!(
"hitbox_offload_task_duration_seconds",
metrics::Unit::Seconds,
"Duration of offload tasks in seconds."
);
"hitbox_offload_task_duration_seconds"
};
pub static ref OFFLOAD_REVALIDATION_COMPLETED: &'static str = {
metrics::describe_counter!(
"hitbox_offload_revalidation_completed_total",
"Total number of offload revalidation tasks completed."
);
"hitbox_offload_revalidation_completed_total"
};
}
#[cfg(feature = "metrics")]
#[inline]
pub fn record_upstream_duration(duration: Duration) {
metrics::histogram!(*CACHE_UPSTREAM_HANDLING_HISTOGRAM).record(duration.as_secs_f64());
}
#[cfg(not(feature = "metrics"))]
#[inline]
pub fn record_upstream_duration(_duration: Duration) {}
#[cfg(feature = "metrics")]
#[inline]
pub fn record_context_metrics(ctx: &CacheContext, duration: Duration, revalidate: bool) {
let status = ctx.status.as_str();
let backend = ctx.source.as_str();
metrics::histogram!(
*CACHE_REQUEST_DURATION,
"status" => status,
"backend" => backend.to_string()
)
.record(duration.as_secs_f64());
let counter = match ctx.status {
crate::context::CacheStatus::Hit => *CACHE_HIT_COUNTER,
crate::context::CacheStatus::Miss => *CACHE_MISS_COUNTER,
crate::context::CacheStatus::Stale => *CACHE_STALE_COUNTER,
};
metrics::counter!(counter, "backend" => backend.to_string()).increment(1);
if revalidate {
metrics::counter!(*OFFLOAD_REVALIDATION_COMPLETED).increment(1);
}
}
#[cfg(not(feature = "metrics"))]
#[inline]
pub fn record_context_metrics(_ctx: &CacheContext, _duration: Duration, _revalidate: bool) {}