Skip to content

Commit ad5a496

Browse files
authored
feat(l1): add datadir size metric and panel (#4715)
**Motivation** We want to have an initial way to know how much is taking our DB **Description** This PR is an initial iteration over the addition of DB related metrics to our dashboards, in this case we add a metric and corresponding panel with the datadir size. Closes #4174
1 parent eedb9dd commit ad5a496

3 files changed

Lines changed: 165 additions & 7 deletions

File tree

cmd/ethrex/initializers.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ pub fn open_store(datadir: &Path) -> Store {
110110
panic!("Specify the desired database engine.");
111111
}
112112
};
113+
#[cfg(feature = "metrics")]
114+
ethrex_metrics::metrics_process::set_datadir_path(datadir.to_path_buf());
113115
Store::new(datadir, engine_type).expect("Failed to create Store")
114116
}
115117
}

crates/blockchain/metrics/metrics_process.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
use prometheus::{Encoder, Registry, TextEncoder};
2-
use std::sync::LazyLock;
1+
use prometheus::{Encoder, IntGauge, Registry, TextEncoder};
2+
use std::{
3+
fs, io,
4+
path::{Path, PathBuf},
5+
sync::{LazyLock, OnceLock},
6+
};
37

48
use crate::MetricsError;
59

610
pub static METRICS_PROCESS: LazyLock<MetricsProcess> = LazyLock::new(MetricsProcess::default);
11+
static DATADIR_PATH: OnceLock<PathBuf> = OnceLock::new();
712

813
#[derive(Debug, Clone)]
914
pub struct MetricsProcess;
@@ -37,6 +42,20 @@ impl MetricsProcess {
3742
})?;
3843
}
3944

45+
if let Some(path) = DATADIR_PATH.get() {
46+
if let Ok(size) = directory_size(path) {
47+
let gauge = IntGauge::new(
48+
"datadir_size_bytes",
49+
"Total size in bytes consumed by the configured datadir.",
50+
)
51+
.map_err(|e| MetricsError::PrometheusErr(e.to_string()))?;
52+
let clamped = size.min(i64::MAX as u64);
53+
gauge.set(clamped as i64);
54+
r.register(Box::new(gauge))
55+
.map_err(|e| MetricsError::PrometheusErr(e.to_string()))?;
56+
}
57+
}
58+
4059
let encoder = TextEncoder::new();
4160
let metric_families = r.gather();
4261

@@ -49,3 +68,37 @@ impl MetricsProcess {
4968
Ok(res)
5069
}
5170
}
71+
72+
pub fn set_datadir_path(path: PathBuf) {
73+
let _ = DATADIR_PATH.set(path);
74+
}
75+
76+
fn directory_size(root: &Path) -> io::Result<u64> {
77+
let mut total = 0;
78+
let mut stack = vec![root.to_path_buf()];
79+
80+
while let Some(path) = stack.pop() {
81+
let entries = match fs::read_dir(&path) {
82+
Ok(entries) => entries,
83+
Err(err) if err.kind() == io::ErrorKind::NotFound => continue,
84+
Err(err) => return Err(err),
85+
};
86+
87+
for entry in entries {
88+
let entry = entry?;
89+
let metadata = match entry.metadata() {
90+
Ok(metadata) => metadata,
91+
Err(err) if err.kind() == io::ErrorKind::NotFound => continue,
92+
Err(err) => return Err(err),
93+
};
94+
95+
if metadata.is_dir() {
96+
stack.push(entry.path());
97+
} else {
98+
total += metadata.len();
99+
}
100+
}
101+
}
102+
103+
Ok(total)
104+
}

metrics/provisioning/grafana/dashboards/common_dashboards/ethrex_l1_perf.json

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,109 @@
13241324
"title": "Open FDs Historic",
13251325
"type": "timeseries"
13261326
},
1327+
{
1328+
"datasource": {
1329+
"type": "prometheus",
1330+
"uid": "${DS_PROMETHEUS}"
1331+
},
1332+
"description": "This calculates the size in bytes of the datadir path where the DB is stored.",
1333+
"fieldConfig": {
1334+
"defaults": {
1335+
"color": {
1336+
"mode": "palette-classic"
1337+
},
1338+
"custom": {
1339+
"axisBorderShow": false,
1340+
"axisCenteredZero": false,
1341+
"axisColorMode": "text",
1342+
"axisLabel": "",
1343+
"axisPlacement": "auto",
1344+
"barAlignment": 0,
1345+
"barWidthFactor": 0.6,
1346+
"drawStyle": "line",
1347+
"fillOpacity": 0,
1348+
"gradientMode": "none",
1349+
"hideFrom": {
1350+
"legend": false,
1351+
"tooltip": false,
1352+
"viz": false
1353+
},
1354+
"insertNulls": false,
1355+
"lineInterpolation": "linear",
1356+
"lineWidth": 1,
1357+
"pointSize": 5,
1358+
"scaleDistribution": {
1359+
"type": "linear"
1360+
},
1361+
"showPoints": "auto",
1362+
"spanNulls": false,
1363+
"stacking": {
1364+
"group": "A",
1365+
"mode": "none"
1366+
},
1367+
"thresholdsStyle": {
1368+
"mode": "off"
1369+
}
1370+
},
1371+
"mappings": [],
1372+
"thresholds": {
1373+
"mode": "absolute",
1374+
"steps": [
1375+
{
1376+
"color": "green",
1377+
"value": 0
1378+
},
1379+
{
1380+
"color": "red",
1381+
"value": 80
1382+
}
1383+
]
1384+
},
1385+
"unit": "decbytes"
1386+
},
1387+
"overrides": []
1388+
},
1389+
"gridPos": {
1390+
"h": 6,
1391+
"w": 24,
1392+
"x": 0,
1393+
"y": 31
1394+
},
1395+
"id": 46,
1396+
"options": {
1397+
"legend": {
1398+
"calcs": [
1399+
"max",
1400+
"mean",
1401+
"last"
1402+
],
1403+
"displayMode": "list",
1404+
"placement": "bottom",
1405+
"showLegend": true
1406+
},
1407+
"tooltip": {
1408+
"hideZeros": false,
1409+
"mode": "single",
1410+
"sort": "none"
1411+
}
1412+
},
1413+
"pluginVersion": "12.2.0",
1414+
"targets": [
1415+
{
1416+
"disableTextWrap": false,
1417+
"editorMode": "builder",
1418+
"expr": "datadir_size_bytes{job=\"$job\"}",
1419+
"fullMetaSearch": false,
1420+
"includeNullMetadata": true,
1421+
"legendFormat": "Datadir Size (GB)",
1422+
"range": true,
1423+
"refId": "A",
1424+
"useBackend": false
1425+
}
1426+
],
1427+
"title": "Datadir Size",
1428+
"type": "timeseries"
1429+
},
13271430
{
13281431
"datasource": {
13291432
"type": "prometheus",
@@ -1391,7 +1494,7 @@
13911494
"h": 8,
13921495
"w": 12,
13931496
"x": 0,
1394-
"y": 31
1497+
"y": 37
13951498
},
13961499
"id": 45,
13971500
"options": {
@@ -1529,7 +1632,7 @@
15291632
"h": 8,
15301633
"w": 12,
15311634
"x": 12,
1532-
"y": 31
1635+
"y": 37
15331636
},
15341637
"id": 42,
15351638
"options": {
@@ -1638,7 +1741,7 @@
16381741
"h": 6,
16391742
"w": 12,
16401743
"x": 0,
1641-
"y": 39
1744+
"y": 45
16421745
},
16431746
"id": 30,
16441747
"options": {
@@ -1765,7 +1868,7 @@
17651868
"h": 6,
17661869
"w": 12,
17671870
"x": 12,
1768-
"y": 39
1871+
"y": 45
17691872
},
17701873
"id": 31,
17711874
"options": {
@@ -1882,5 +1985,5 @@
18821985
"timezone": "browser",
18831986
"title": "Ethrex L1 - Perf Dashboard",
18841987
"uid": "beoru4vp59yiof",
1885-
"version": 7
1988+
"version": 8
18861989
}

0 commit comments

Comments
 (0)