Skip to content

Commit aff4278

Browse files
committed
feat(metrics): export per-level SST file counts to Prometheus
Add rocksdb_num_files_at_level gauge with 'db' and 'level' labels, covering all 7 RocksDB compaction levels (L0-L6).
1 parent b57e5dc commit aff4278

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/new_index/db.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::util::{bincode, spawn_thread, Bytes};
1414

1515
static DB_VERSION: u32 = 3;
1616

17+
const ROCKSDB_NUM_LEVELS: u32 = 7;
18+
1719
#[derive(Debug, Eq, PartialEq)]
1820
pub struct DBRow {
1921
pub key: Vec<u8>,
@@ -389,7 +391,9 @@ impl DB {
389391

390392
pub fn start_stats_exporter(&self, db_metrics: Arc<RocksDbMetrics>, db_name: &str) {
391393
let db_arc = Arc::clone(&self.db);
394+
let db_arc2 = Arc::clone(&self.db);
392395
let label = db_name.to_string();
396+
let label2 = label.clone();
393397

394398
let update_gauge = move |gauge: &GaugeVec, property: &str| {
395399
if let Ok(Some(value)) = db_arc.property_value(property) {
@@ -433,6 +437,15 @@ impl DB {
433437
update_gauge(&db_metrics.block_cache_capacity, "rocksdb.block-cache-capacity");
434438
update_gauge(&db_metrics.block_cache_usage, "rocksdb.block-cache-usage");
435439
update_gauge(&db_metrics.block_cache_pinned_usage, "rocksdb.block-cache-pinned-usage");
440+
for level in 0..ROCKSDB_NUM_LEVELS {
441+
let prop = format!("rocksdb.num-files-at-level{}", level);
442+
if let Ok(Some(value)) = db_arc2.property_value(&prop) {
443+
if let Ok(v) = value.parse::<f64>() {
444+
let level_str = level.to_string();
445+
db_metrics.num_files_at_level.with_label_values(&[&label2, &level_str]).set(v);
446+
}
447+
}
448+
}
436449
thread::sleep(Duration::from_secs(5));
437450
});
438451
}

src/new_index/db_metrics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub struct RocksDbMetrics {
4949

5050
// Level metrics
5151
pub base_level: GaugeVec,
52+
pub num_files_at_level: GaugeVec,
5253

5354
// Write metrics
5455
pub actual_delayed_write_rate: GaugeVec,
@@ -204,6 +205,10 @@ impl RocksDbMetrics {
204205
format!("rocksdb_base_level"),
205206
"Base level for compaction."
206207
), labels),
208+
num_files_at_level: metrics.gauge_vec(MetricOpts::new(
209+
"rocksdb_num_files_at_level",
210+
"Number of SST files at each compaction level."
211+
), &["db", "level"]),
207212

208213
// Write metrics
209214
actual_delayed_write_rate: metrics.gauge_vec(MetricOpts::new(

0 commit comments

Comments
 (0)