Skip to content

Commit

Permalink
unified cache metrics (#5002)
Browse files Browse the repository at this point in the history
* unified cache metrics

* lint, use IntCounter

* CR comments

---------

Co-authored-by: Paul Masurel <[email protected]>
  • Loading branch information
Surya361 and fulmicoton authored Jun 12, 2024
1 parent de67160 commit c228fee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
19 changes: 19 additions & 0 deletions quickwit/quickwit-common/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ pub fn new_counter(
counter
}

pub fn new_counter_with_labels(
name: &str,
help: &str,
subsystem: &str,
const_labels: &[(&str, &str)],
) -> IntCounter {
let owned_const_labels: HashMap<String, String> = const_labels
.iter()
.map(|(label_name, label_value)| (label_name.to_string(), label_value.to_string()))
.collect();
let counter_opts = Opts::new(name, help)
.namespace("quickwit")
.subsystem(subsystem)
.const_labels(owned_const_labels);
let counter = IntCounter::with_opts(counter_opts).expect("failed to create counter");
prometheus::register(Box::new(counter.clone())).expect("failed to register counter");
counter
}

pub fn new_counter_vec<const N: usize>(
name: &str,
help: &str,
Expand Down
42 changes: 22 additions & 20 deletions quickwit/quickwit-storage/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
// See https://prometheus.io/docs/practices/naming/

use once_cell::sync::Lazy;
use quickwit_common::metrics::{new_counter, new_gauge, IntCounter, IntGauge};
use quickwit_common::metrics::{
new_counter, new_counter_with_labels, new_gauge, IntCounter, IntGauge,
};

/// Counters associated to storage operations.
pub struct StorageMetrics {
Expand Down Expand Up @@ -95,38 +97,38 @@ pub struct CacheMetrics {

impl CacheMetrics {
pub fn for_component(component_name: &str) -> Self {
let namespace = format!("cache_{component_name}");
const CACHE_METRICS_NAMESPACE: &str = "cache";
CacheMetrics {
component_name: component_name.to_string(),
in_cache_count: new_gauge(
"in_cache_count",
"Count of {component_name} in cache",
&namespace,
&[],
"Count of in cache by component",
CACHE_METRICS_NAMESPACE,
&[("component_name", component_name)],
),
in_cache_num_bytes: new_gauge(
"in_cache_num_bytes",
"Number of {component_name} bytes in cache",
&namespace,
&[],
"Number of bytes in cache by component",
CACHE_METRICS_NAMESPACE,
&[("component_name", component_name)],
),
hits_num_items: new_counter(
hits_num_items: new_counter_with_labels(
"cache_hits_total",
"Number of {component_name} cache hits",
&namespace,
&[],
"Number of cache hits by component",
CACHE_METRICS_NAMESPACE,
&[("component_name", component_name)],
),
hits_num_bytes: new_counter(
hits_num_bytes: new_counter_with_labels(
"cache_hits_bytes",
"Number of {component_name} cache hits in bytes",
&namespace,
&[],
"Number of cache hits in bytes by component",
CACHE_METRICS_NAMESPACE,
&[("component_name", component_name)],
),
misses_num_items: new_counter(
misses_num_items: new_counter_with_labels(
"cache_misses_total",
"Number of {component_name} cache misses",
&namespace,
&[],
"Number of cache misses by component",
CACHE_METRICS_NAMESPACE,
&[("component_name", component_name)],
),
}
}
Expand Down

0 comments on commit c228fee

Please sign in to comment.