fix: use result cache env for metrics (#9281)

This commit is contained in:
Hengfei Yang
2025-11-25 14:14:46 +08:00
committed by GitHub
parent ec567b45ff
commit 2e28f9be47
4 changed files with 10 additions and 18 deletions
+1 -9
View File
@@ -1170,12 +1170,6 @@ pub struct Common {
help = "Strategy to use for result cache, default is both, possible value - both, overlap, duration"
)]
pub result_cache_selection_strategy: String,
#[env_config(
name = "ZO_METRICS_CACHE_ENABLED",
default = true,
help = "Enable result cache for PromQL metrics queries"
)]
pub metrics_cache_enabled: bool,
#[env_config(name = "ZO_SWAGGER_ENABLED", default = true)]
pub swagger_enabled: bool,
#[env_config(
@@ -2812,16 +2806,14 @@ fn check_disk_cache_config(cfg: &mut Config) -> Result<(), anyhow::Error> {
// disable disk cache for local disk storage
if cfg.common.is_local_storage
&& !cfg.common.result_cache_enabled
&& !cfg.common.metrics_cache_enabled
&& !cfg.common.feature_query_streaming_aggs
{
cfg.disk_cache.enabled = false;
}
// disable result cache and metrics cache if disk cache is disabled
// disable result cache if disk cache is disabled
if !cfg.disk_cache.enabled {
cfg.common.result_cache_enabled = false;
cfg.common.metrics_cache_enabled = false;
cfg.common.feature_query_streaming_aggs = false;
}
+1 -3
View File
@@ -179,9 +179,7 @@ async fn dispatch(
}
// check if the request need to be proxied by body
if (cfg.common.result_cache_enabled || cfg.common.metrics_cache_enabled)
&& is_querier_route_by_body(&path)
{
if cfg.common.result_cache_enabled && is_querier_route_by_body(&path) {
return proxy_querier_by_body(req, payload, client, new_url, start).await;
}
+2 -2
View File
@@ -55,7 +55,7 @@ static GLOBAL_CACHE: Lazy<Vec<RwLock<MetricsIndex>>> = Lazy::new(|| {
pub async fn init() -> Result<()> {
let cfg = get_config();
if !cfg.common.metrics_cache_enabled {
if !cfg.common.result_cache_enabled {
return Ok(());
}
@@ -352,7 +352,7 @@ pub async fn set(
/// load the cache item from the secondary storage
pub async fn load(cache_key: &str) -> Result<()> {
let cfg = get_config();
if !cfg.common.metrics_cache_enabled {
if !cfg.common.result_cache_enabled {
return Ok(());
}
let Some((key, start, end)) = parse_cache_item_key(cache_key) else {
+6 -4
View File
@@ -100,7 +100,7 @@ async fn search_in_cluster(
// cache disabled if result cache is disabled or use_cache is false or start == end or step == 0
let cache_disabled =
!cfg.common.metrics_cache_enabled || !req.use_cache || start == end || step == 0;
!cfg.common.result_cache_enabled || !req.use_cache || start == end || step == 0;
// adjust start and end time
let (start, end) = adjust_start_end(start, end, step);
@@ -144,11 +144,13 @@ async fn search_in_cluster(
match cache::get(query, start, end, step).await {
Ok(Some((new_start, values))) => {
let took = start_time.elapsed().as_millis() as i32;
let cache_ratio = (new_start - start) as f64 / (end - start) as f64;
config::metrics::QUERY_METRICS_CACHE_RATIO
.with_label_values(&[&req.org_id])
.observe((new_start - start) as f64 / (end - start) as f64);
.observe(cache_ratio);
log::info!(
"[trace_id {trace_id}] promql->search->cache: hit cache, took: {took} ms"
"[trace_id {trace_id}] promql->search->cache: hit cache, cache ratio: {:.2}%, took: {took} ms",
cache_ratio * 100.0,
);
(new_start, values)
}
@@ -348,7 +350,7 @@ async fn search_in_cluster(
.await;
// cache the result
if cfg.common.metrics_cache_enabled
if cfg.common.result_cache_enabled
&& let Some(matrix) = values.get_ref_matrix_values()
&& let Err(err) = cache::set(
trace_id,