risingwave_storage/monitor/
hummock_metrics.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::sync::LazyLock;
16
17use prometheus::core::{AtomicU64, GenericCounter};
18use prometheus::{
19    Histogram, Registry, exponential_buckets, histogram_opts, register_histogram_with_registry,
20    register_int_counter_with_registry,
21};
22use risingwave_common::monitor::GLOBAL_METRICS_REGISTRY;
23
24/// [`HummockMetrics`] stores the performance and IO metrics of hummock storage.
25#[derive(Debug, Clone)]
26pub struct HummockMetrics {
27    pub get_new_sst_ids_counts: GenericCounter<AtomicU64>,
28    pub report_compaction_task_counts: GenericCounter<AtomicU64>,
29    pub get_new_sst_ids_latency: Histogram,
30    pub report_compaction_task_latency: Histogram,
31}
32
33pub static GLOBAL_HUMMOCK_METRICS: LazyLock<HummockMetrics> =
34    LazyLock::new(|| HummockMetrics::new(&GLOBAL_METRICS_REGISTRY));
35
36impl HummockMetrics {
37    fn new(registry: &Registry) -> Self {
38        // ----- Hummock -----
39        let get_new_sst_ids_counts = register_int_counter_with_registry!(
40            "state_store_get_new_sst_ids_counts",
41            "Total number of get_new_table_id requests that have been issued to state store",
42            registry
43        )
44        .unwrap();
45        let report_compaction_task_counts = register_int_counter_with_registry!(
46            "state_store_report_compaction_task_counts",
47            "Total number of report_compaction_task requests that have been issued to state store",
48            registry
49        )
50        .unwrap();
51
52        // 10ms ~ max 2.7h
53        let time_buckets = exponential_buckets(0.01, 10.0, 7).unwrap();
54        // gRPC latency
55
56        // --
57        let get_new_sst_ids_latency_opts = histogram_opts!(
58            "state_store_get_new_sst_ids_latency",
59            "Total latency of get new table id that have been issued to state store",
60            time_buckets.clone()
61        );
62        let get_new_sst_ids_latency =
63            register_histogram_with_registry!(get_new_sst_ids_latency_opts, registry).unwrap();
64
65        // --
66        let report_compaction_task_latency_opts = histogram_opts!(
67            "state_store_report_compaction_task_latency",
68            "Total latency of report compaction task that have been issued to state store",
69            time_buckets
70        );
71        let report_compaction_task_latency =
72            register_histogram_with_registry!(report_compaction_task_latency_opts, registry)
73                .unwrap();
74
75        Self {
76            get_new_sst_ids_counts,
77            report_compaction_task_counts,
78            get_new_sst_ids_latency,
79            report_compaction_task_latency,
80        }
81    }
82
83    /// Creates a new `HummockStateStoreMetrics` instance used in tests or other places.
84    pub fn unused() -> Self {
85        GLOBAL_HUMMOCK_METRICS.clone()
86    }
87}