Skip to content

Commit c306579

Browse files
committed
wip
1 parent afed854 commit c306579

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

crates/storage/db/src/mdbx/metrics.rs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ impl Default for DbMetrics {
2626
impl DbMetrics {
2727
/// Creates a new instance of database metrics.
2828
pub fn new() -> Self {
29+
// database tables metrics
30+
::metrics::describe_gauge!(
31+
"db.table_size",
32+
::metrics::Unit::Bytes,
33+
"Total size of the table"
34+
);
35+
::metrics::describe_gauge!(
36+
"db.table_pages",
37+
::metrics::Unit::Count,
38+
"Number of pages in the table"
39+
);
40+
::metrics::describe_gauge!(
41+
"db.table_entries",
42+
::metrics::Unit::Count,
43+
"Number of entries in the table"
44+
);
45+
::metrics::describe_gauge!(
46+
"db.freelist",
47+
::metrics::Unit::Bytes,
48+
"Size of the database freelist"
49+
);
50+
2951
Self {
3052
inner: Arc::new(DbMetricsInner {
3153
transaction: DbTransactionMetrics::default(),
@@ -35,21 +57,27 @@ impl DbMetrics {
3557
}
3658

3759
/// Records a transaction creation.
38-
pub fn record_tx_create(&self, is_write: bool) {
39-
if is_write {
40-
self.inner.transaction.rw_created.increment(1);
41-
} else {
42-
self.inner.transaction.ro_created.increment(1);
43-
}
60+
pub fn record_ro_tx_create(&self) {
61+
self.inner.transaction.ro_created.increment(1);
62+
}
63+
64+
pub fn record_rw_tx_create(&self) {
65+
self.inner.transaction.rw_created.increment(1);
4466
}
4567

4668
/// Records a transaction commit with timing.
69+
///
70+
/// ## Arguments
71+
///
72+
/// * `duration` - Time taken for the get operation in seconds
73+
/// * `success` - Whether the commit operation completed sucessfully or not.
4774
pub fn record_tx_commit(&self, duration: f64, success: bool) {
4875
if success {
4976
self.inner.transaction.commits_successful.increment(1);
5077
} else {
5178
self.inner.transaction.commits_failed.increment(1);
5279
}
80+
5381
self.inner.transaction.commit_time_seconds.record(duration);
5482
}
5583

@@ -59,32 +87,51 @@ impl DbMetrics {
5987
}
6088

6189
/// Records a get operation.
90+
///
91+
/// ## Arguments
92+
///
93+
/// * `duration` - Time taken for the get operation in seconds
94+
/// * `found` - Whether the requested value was found
6295
pub fn record_get(&self, duration: f64, found: bool) {
6396
if found {
6497
self.inner.operations.get_hits.increment(1);
6598
} else {
6699
self.inner.operations.get_misses.increment(1);
67100
}
101+
68102
self.inner.operations.get_time_seconds.record(duration);
69103
}
70104

71105
/// Records a put operation.
106+
///
107+
/// ## Arguments
108+
///
109+
/// * `duration` - Time taken for the put operation in seconds
72110
pub fn record_put(&self, duration: f64) {
73111
self.inner.operations.puts.increment(1);
74112
self.inner.operations.put_time_seconds.record(duration);
75113
}
76114

77115
/// Records a delete operation.
116+
///
117+
/// ## Arguments
118+
///
119+
/// * `duration` - Time taken for the delete operation in seconds
78120
pub fn record_delete(&self, duration: f64, deleted: bool) {
79121
if deleted {
80122
self.inner.operations.deletes_successful.increment(1);
81123
} else {
82124
self.inner.operations.deletes_failed.increment(1);
83125
}
126+
84127
self.inner.operations.delete_time_seconds.record(duration);
85128
}
86129

87130
/// Records a clear operation.
131+
///
132+
/// ## Arguments
133+
///
134+
/// * `duration` - Time taken for the clear operation in seconds
88135
pub fn record_clear(&self, duration: f64) {
89136
self.inner.operations.clears.increment(1);
90137
self.inner.operations.clear_time_seconds.record(duration);

crates/storage/db/src/mdbx/mod.rs

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl DbEnvBuilder {
101101
let dir = path.as_ref().to_path_buf();
102102
let metrics = DbMetrics::new();
103103

104-
Ok(DbEnv { inner: Arc::new(DbEnvInner { env, dir, metrics }) }.with_metrics())
104+
Ok(DbEnv { inner: Arc::new(DbEnvInner { env, dir, metrics }) })
105105
}
106106
}
107107

@@ -161,30 +161,6 @@ impl DbEnv {
161161
pub fn path(&self) -> &Path {
162162
&self.inner.dir
163163
}
164-
165-
pub(super) fn with_metrics(self) -> Self {
166-
::metrics::describe_gauge!(
167-
"db.table_size",
168-
::metrics::Unit::Bytes,
169-
"Total size of the table"
170-
);
171-
::metrics::describe_gauge!(
172-
"db.table_pages",
173-
::metrics::Unit::Count,
174-
"Number of pages in the table"
175-
);
176-
::metrics::describe_gauge!(
177-
"db.table_entries",
178-
::metrics::Unit::Count,
179-
"Number of entries in the table"
180-
);
181-
::metrics::describe_gauge!(
182-
"db.freelist",
183-
::metrics::Unit::Bytes,
184-
"Size of the database freelist"
185-
);
186-
self
187-
}
188164
}
189165

190166
impl Database for DbEnv {
@@ -194,20 +170,16 @@ impl Database for DbEnv {
194170

195171
#[tracing::instrument(level = "trace", name = "db_txn_ro_create", skip_all)]
196172
fn tx(&self) -> Result<Self::Tx, DatabaseError> {
197-
self.inner.metrics.record_tx_create(false);
198-
Ok(Tx::new(
199-
self.inner.env.begin_ro_txn().map_err(DatabaseError::CreateROTx)?,
200-
self.inner.metrics.clone(),
201-
))
173+
let tx = self.inner.env.begin_ro_txn().map_err(DatabaseError::CreateROTx)?;
174+
self.inner.metrics.record_ro_tx_create();
175+
Ok(Tx::new(tx, self.inner.metrics.clone()))
202176
}
203177

204178
#[tracing::instrument(level = "trace", name = "db_txn_rw_create", skip_all)]
205179
fn tx_mut(&self) -> Result<Self::TxMut, DatabaseError> {
206-
self.inner.metrics.record_tx_create(true);
207-
Ok(Tx::new(
208-
self.inner.env.begin_rw_txn().map_err(DatabaseError::CreateRWTx)?,
209-
self.inner.metrics.clone(),
210-
))
180+
let tx = self.inner.env.begin_rw_txn().map_err(DatabaseError::CreateRWTx)?;
181+
self.inner.metrics.record_rw_tx_create();
182+
Ok(Tx::new(tx, self.inner.metrics.clone()))
211183
}
212184

213185
fn stats(&self) -> Result<Self::Stats, DatabaseError> {

0 commit comments

Comments
 (0)