Skip to content

Commit b75b3ed

Browse files
Latency metrics for the KeyValueStoreView. (#2540)
* Add metrics to the KeyValueStoreView.
1 parent 0a6eccc commit b75b3ed

File tree

1 file changed

+119
-5
lines changed

1 file changed

+119
-5
lines changed

linera-views/src/views/key_value_store_view.rs

+119-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,111 @@ use crate::{
2727
};
2828

2929
#[cfg(with_metrics)]
30-
/// The runtime of hash computation
31-
static KEY_VALUE_STORE_VIEW_HASH_RUNTIME: LazyLock<HistogramVec> = LazyLock::new(|| {
30+
/// The latency of hash computation
31+
static KEY_VALUE_STORE_VIEW_HASH_LATENCY: LazyLock<HistogramVec> = LazyLock::new(|| {
3232
prometheus_util::register_histogram_vec(
33-
"key_value_store_view_hash_runtime",
34-
"KeyValueStoreView hash runtime",
33+
"key_value_store_view_hash_latency",
34+
"KeyValueStoreView hash latency",
35+
&[],
36+
Some(vec![
37+
0.001, 0.003, 0.01, 0.03, 0.1, 0.2, 0.3, 0.4, 0.5, 0.75, 1.0, 2.0, 5.0,
38+
]),
39+
)
40+
.expect("Histogram can be created")
41+
});
42+
43+
#[cfg(with_metrics)]
44+
/// The latency of get operation
45+
static KEY_VALUE_STORE_VIEW_GET_LATENCY: LazyLock<HistogramVec> = LazyLock::new(|| {
46+
prometheus_util::register_histogram_vec(
47+
"key_value_store_view_get_latency",
48+
"KeyValueStoreView get latency",
49+
&[],
50+
Some(vec![
51+
0.001, 0.003, 0.01, 0.03, 0.1, 0.2, 0.3, 0.4, 0.5, 0.75, 1.0, 2.0, 5.0,
52+
]),
53+
)
54+
.expect("Histogram can be created")
55+
});
56+
57+
#[cfg(with_metrics)]
58+
/// The latency of multi get
59+
static KEY_VALUE_STORE_VIEW_MULTI_GET_LATENCY: LazyLock<HistogramVec> = LazyLock::new(|| {
60+
prometheus_util::register_histogram_vec(
61+
"key_value_store_view_multi_get_latency",
62+
"KeyValueStoreView multi get latency",
63+
&[],
64+
Some(vec![
65+
0.001, 0.003, 0.01, 0.03, 0.1, 0.2, 0.3, 0.4, 0.5, 0.75, 1.0, 2.0, 5.0,
66+
]),
67+
)
68+
.expect("Histogram can be created")
69+
});
70+
71+
#[cfg(with_metrics)]
72+
/// The latency of contains key
73+
static KEY_VALUE_STORE_VIEW_CONTAINS_KEY_LATENCY: LazyLock<HistogramVec> = LazyLock::new(|| {
74+
prometheus_util::register_histogram_vec(
75+
"key_value_store_view_contains_key_latency",
76+
"KeyValueStoreView contains key latency",
77+
&[],
78+
Some(vec![
79+
0.001, 0.003, 0.01, 0.03, 0.1, 0.2, 0.3, 0.4, 0.5, 0.75, 1.0, 2.0, 5.0,
80+
]),
81+
)
82+
.expect("Histogram can be created")
83+
});
84+
85+
#[cfg(with_metrics)]
86+
/// The latency of contains keys
87+
static KEY_VALUE_STORE_VIEW_CONTAINS_KEYS_LATENCY: LazyLock<HistogramVec> = LazyLock::new(|| {
88+
prometheus_util::register_histogram_vec(
89+
"key_value_store_view_contains_keys_latency",
90+
"KeyValueStoreView contains keys latency",
91+
&[],
92+
Some(vec![
93+
0.001, 0.003, 0.01, 0.03, 0.1, 0.2, 0.3, 0.4, 0.5, 0.75, 1.0, 2.0, 5.0,
94+
]),
95+
)
96+
.expect("Histogram can be created")
97+
});
98+
99+
#[cfg(with_metrics)]
100+
/// The latency of find keys by prefix operation
101+
static KEY_VALUE_STORE_VIEW_FIND_KEYS_BY_PREFIX_LATENCY: LazyLock<HistogramVec> =
102+
LazyLock::new(|| {
103+
prometheus_util::register_histogram_vec(
104+
"key_value_store_view_find_keys_by_prefix_latency",
105+
"KeyValueStoreView find keys by prefix latency",
106+
&[],
107+
Some(vec![
108+
0.001, 0.003, 0.01, 0.03, 0.1, 0.2, 0.3, 0.4, 0.5, 0.75, 1.0, 2.0, 5.0,
109+
]),
110+
)
111+
.expect("Histogram can be created")
112+
});
113+
114+
#[cfg(with_metrics)]
115+
/// The latency of find key values by prefix operation
116+
static KEY_VALUE_STORE_VIEW_FIND_KEY_VALUES_BY_PREFIX_LATENCY: LazyLock<HistogramVec> =
117+
LazyLock::new(|| {
118+
prometheus_util::register_histogram_vec(
119+
"key_value_store_view_find_key_values_by_prefix_latency",
120+
"KeyValueStoreView find key values by prefix latency",
121+
&[],
122+
Some(vec![
123+
0.001, 0.003, 0.01, 0.03, 0.1, 0.2, 0.3, 0.4, 0.5, 0.75, 1.0, 2.0, 5.0,
124+
]),
125+
)
126+
.expect("Histogram can be created")
127+
});
128+
129+
#[cfg(with_metrics)]
130+
/// The latency of write batch operation
131+
static KEY_VALUE_STORE_VIEW_WRITE_BATCH_LATENCY: LazyLock<HistogramVec> = LazyLock::new(|| {
132+
prometheus_util::register_histogram_vec(
133+
"key_value_store_view_write_batch_latency",
134+
"KeyValueStoreView write batch latency",
35135
&[],
36136
Some(vec![
37137
0.001, 0.003, 0.01, 0.03, 0.1, 0.2, 0.3, 0.4, 0.5, 0.75, 1.0, 2.0, 5.0,
@@ -614,6 +714,8 @@ where
614714
/// # })
615715
/// ```
616716
pub async fn get(&self, index: &[u8]) -> Result<Option<Vec<u8>>, ViewError> {
717+
#[cfg(with_metrics)]
718+
let _latency = KEY_VALUE_STORE_VIEW_GET_LATENCY.measure_latency();
617719
ensure!(index.len() <= self.max_key_size(), ViewError::KeyTooLong);
618720
if let Some(update) = self.updates.get(index) {
619721
let value = match update {
@@ -643,6 +745,8 @@ where
643745
/// # })
644746
/// ```
645747
pub async fn contains_key(&self, index: &[u8]) -> Result<bool, ViewError> {
748+
#[cfg(with_metrics)]
749+
let _latency = KEY_VALUE_STORE_VIEW_CONTAINS_KEY_LATENCY.measure_latency();
646750
ensure!(index.len() <= self.max_key_size(), ViewError::KeyTooLong);
647751
if let Some(update) = self.updates.get(index) {
648752
let test = match update {
@@ -673,6 +777,8 @@ where
673777
/// # })
674778
/// ```
675779
pub async fn contains_keys(&self, indices: Vec<Vec<u8>>) -> Result<Vec<bool>, ViewError> {
780+
#[cfg(with_metrics)]
781+
let _latency = KEY_VALUE_STORE_VIEW_CONTAINS_KEYS_LATENCY.measure_latency();
676782
let mut results = Vec::with_capacity(indices.len());
677783
let mut missed_indices = Vec::new();
678784
let mut vector_query = Vec::new();
@@ -719,6 +825,8 @@ where
719825
&self,
720826
indices: Vec<Vec<u8>>,
721827
) -> Result<Vec<Option<Vec<u8>>>, ViewError> {
828+
#[cfg(with_metrics)]
829+
let _latency = KEY_VALUE_STORE_VIEW_MULTI_GET_LATENCY.measure_latency();
722830
let mut result = Vec::with_capacity(indices.len());
723831
let mut missed_indices = Vec::new();
724832
let mut vector_query = Vec::new();
@@ -765,6 +873,8 @@ where
765873
/// # })
766874
/// ```
767875
pub async fn write_batch(&mut self, batch: Batch) -> Result<(), ViewError> {
876+
#[cfg(with_metrics)]
877+
let _latency = KEY_VALUE_STORE_VIEW_WRITE_BATCH_LATENCY.measure_latency();
768878
*self.hash.get_mut().unwrap() = None;
769879
let max_key_size = self.max_key_size();
770880
for operation in batch.operations {
@@ -901,6 +1011,8 @@ where
9011011
/// # })
9021012
/// ```
9031013
pub async fn find_keys_by_prefix(&self, key_prefix: &[u8]) -> Result<Vec<Vec<u8>>, ViewError> {
1014+
#[cfg(with_metrics)]
1015+
let _latency = KEY_VALUE_STORE_VIEW_FIND_KEYS_BY_PREFIX_LATENCY.measure_latency();
9041016
ensure!(
9051017
key_prefix.len() <= self.max_key_size(),
9061018
ViewError::KeyTooLong
@@ -975,6 +1087,8 @@ where
9751087
&self,
9761088
key_prefix: &[u8],
9771089
) -> Result<Vec<(Vec<u8>, Vec<u8>)>, ViewError> {
1090+
#[cfg(with_metrics)]
1091+
let _latency = KEY_VALUE_STORE_VIEW_FIND_KEY_VALUES_BY_PREFIX_LATENCY.measure_latency();
9781092
ensure!(
9791093
key_prefix.len() <= self.max_key_size(),
9801094
ViewError::KeyTooLong
@@ -1033,7 +1147,7 @@ where
10331147

10341148
async fn compute_hash(&self) -> Result<<sha3::Sha3_256 as Hasher>::Output, ViewError> {
10351149
#[cfg(with_metrics)]
1036-
let _hash_latency = KEY_VALUE_STORE_VIEW_HASH_RUNTIME.measure_latency();
1150+
let _hash_latency = KEY_VALUE_STORE_VIEW_HASH_LATENCY.measure_latency();
10371151
let mut hasher = sha3::Sha3_256::default();
10381152
let mut count = 0;
10391153
self.for_each_index_value(|index, value| -> Result<(), ViewError> {

0 commit comments

Comments
 (0)