Skip to content

Commit a9ef414

Browse files
refactor(623): Move table size metric into lib
Query optimization will soon need this metric. Therefore it must be moved out of core and into lib. Eventually we might want to move all metrics into lib.
1 parent ccf291b commit a9ef414

File tree

8 files changed

+34
-16
lines changed

8 files changed

+34
-16
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/core/src/db/commit_log.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::{
1818
execution_context::ExecutionContext,
1919
};
2020
use anyhow::Context;
21+
use spacetimedb_lib::metrics::METRICS;
2122
use spacetimedb_sats::hash::{hash_bytes, Hash};
2223
use spacetimedb_sats::DataKey;
2324
use std::{
@@ -380,7 +381,7 @@ impl CommitLogMut {
380381
.with_label_values(workload, db, reducer_or_query, &table_id)
381382
.inc();
382383
// Increment table rows gauge
383-
DB_METRICS.rdb_num_table_rows.with_label_values(db, &table_id).inc();
384+
METRICS.rdb_num_table_rows.with_label_values(db, &table_id).inc();
384385
Operation::Insert
385386
}
386387
TxOp::Delete => {
@@ -390,7 +391,7 @@ impl CommitLogMut {
390391
.with_label_values(workload, db, reducer_or_query, &table_id)
391392
.inc();
392393
// Decrement table rows gauge
393-
DB_METRICS.rdb_num_table_rows.with_label_values(db, &table_id).dec();
394+
METRICS.rdb_num_table_rows.with_label_values(db, &table_id).dec();
394395
Operation::Delete
395396
}
396397
};

crates/core/src/db/datastore/locking_tx_datastore/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use crate::{
4848
};
4949
use anyhow::anyhow;
5050
use parking_lot::{lock_api::ArcRwLockWriteGuard, RawRwLock, RwLock};
51-
use spacetimedb_lib::Address;
51+
use spacetimedb_lib::{metrics::METRICS, Address};
5252
use spacetimedb_primitives::*;
5353
use spacetimedb_sats::data_key::{DataKey, ToDataKey};
5454
use spacetimedb_sats::db::def::*;
@@ -250,7 +250,7 @@ impl CommittedState {
250250
for schema in system_tables() {
251251
let table_id = schema.table_id;
252252
// Reset the row count metric for this system table
253-
DB_METRICS
253+
METRICS
254254
.rdb_num_table_rows
255255
.with_label_values(&database_address, &table_id.0)
256256
.set(0);
@@ -283,7 +283,7 @@ impl CommittedState {
283283

284284
st_columns.rows.insert(RowId(data_key), row);
285285
// Increment row count for st_columns
286-
DB_METRICS
286+
METRICS
287287
.rdb_num_table_rows
288288
.with_label_values(&database_address, &ST_COLUMNS_ID.into())
289289
.inc();
@@ -311,7 +311,7 @@ impl CommittedState {
311311
let data_key = row.to_data_key();
312312
st_constraints.rows.insert(RowId(data_key), row);
313313
// Increment row count for st_constraints
314-
DB_METRICS
314+
METRICS
315315
.rdb_num_table_rows
316316
.with_label_values(&database_address, &ST_CONSTRAINTS_ID.into())
317317
.inc();
@@ -340,7 +340,7 @@ impl CommittedState {
340340
let data_key = row.to_data_key();
341341
st_indexes.rows.insert(RowId(data_key), row);
342342
// Increment row count for st_indexes
343-
DB_METRICS
343+
METRICS
344344
.rdb_num_table_rows
345345
.with_label_values(&database_address, &ST_INDEXES_ID.into())
346346
.inc();
@@ -375,7 +375,7 @@ impl CommittedState {
375375
let data_key = row.to_data_key();
376376
st_sequences.rows.insert(RowId(data_key), row);
377377
// Increment row count for st_sequences
378-
DB_METRICS
378+
METRICS
379379
.rdb_num_table_rows
380380
.with_label_values(&database_address, &ST_SEQUENCES_ID.into())
381381
.inc();
@@ -1885,7 +1885,7 @@ impl Locking {
18851885
committed_state
18861886
.table_rows(table_id, schema)
18871887
.remove(&RowId(write.data_key));
1888-
DB_METRICS
1888+
METRICS
18891889
.rdb_num_table_rows
18901890
.with_label_values(&self.database_address, &table_id.into())
18911891
.dec();
@@ -1914,7 +1914,7 @@ impl Locking {
19141914
committed_state
19151915
.table_rows(table_id, schema)
19161916
.insert(RowId(write.data_key), product_value);
1917-
DB_METRICS
1917+
METRICS
19181918
.rdb_num_table_rows
19191919
.with_label_values(&self.database_address, &table_id.into())
19201920
.inc();

crates/core/src/db/db_metrics/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ metrics_group!(
5555
#[labels(table_id: u32)]
5656
pub rdb_delete_by_rel_time: HistogramVec,
5757

58-
#[name = spacetime_num_table_rows]
59-
#[help = "The number of rows in a table"]
60-
#[labels(db: Address, table_id: u32)]
61-
pub rdb_num_table_rows: IntGaugeVec,
62-
6358
#[name = spacetime_num_rows_inserted_cumulative]
6459
#[help = "The cumulative number of rows inserted into a table"]
6560
#[labels(txn_type: WorkloadType, db: Address, reducer_or_query: str, table_id: u32)]

crates/core/src/db/relational_db.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use fs2::FileExt;
22
use nonempty::NonEmpty;
3+
use spacetimedb_lib::metrics::METRICS;
34
use std::borrow::Cow;
45
use std::fs::{create_dir_all, File};
56
use std::ops::RangeBounds;
@@ -376,7 +377,7 @@ impl RelationalDB {
376377
.with_label_values(&table_id.0)
377378
.start_timer();
378379
self.inner.drop_table_mut_tx(tx, table_id).map(|_| {
379-
DB_METRICS
380+
METRICS
380381
.rdb_num_table_rows
381382
.with_label_values(&self.address, &table_id.into())
382383
.set(0)

crates/lib/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ derive_more.workspace = true
3434
enum-as-inner.workspace = true
3535
hex.workspace = true
3636
itertools.workspace = true
37+
once_cell.workspace = true
38+
prometheus.workspace = true
3739
serde = { workspace = true, optional = true }
3840
serde_with = {workspace = true, optional = true }
3941
thiserror.workspace = true

crates/lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use spacetimedb_sats::{impl_serialize, WithTypespace};
55
pub mod address;
66
pub mod filter;
77
pub mod identity;
8+
pub mod metrics;
89
pub mod name;
910
pub mod operator;
1011
pub mod primary_key;

crates/lib/src/metrics.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use once_cell::sync::Lazy;
2+
use prometheus::IntGaugeVec;
3+
use spacetimedb_lib::Address;
4+
use spacetimedb_metrics::metrics_group;
5+
6+
metrics_group!(
7+
#[non_exhaustive]
8+
pub struct Metrics {
9+
#[name = spacetime_num_table_rows]
10+
#[help = "The number of rows in a table"]
11+
#[labels(db: Address, table_id: u32)]
12+
pub rdb_num_table_rows: IntGaugeVec,
13+
}
14+
);
15+
16+
pub static METRICS: Lazy<Metrics> = Lazy::new(Metrics::new);

0 commit comments

Comments
 (0)