Skip to content

Commit

Permalink
refactor(metrics): Move table size metric into lib
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joshua-spacetime committed Dec 8, 2023
1 parent 4b9f30f commit 89b2864
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 16 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions crates/core/src/db/commit_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
execution_context::ExecutionContext,
};
use anyhow::Context;
use spacetimedb_lib::metrics::METRICS;
use spacetimedb_sats::hash::{hash_bytes, Hash};
use spacetimedb_sats::DataKey;
use std::{
Expand Down Expand Up @@ -380,7 +381,7 @@ impl CommitLogMut {
.with_label_values(workload, db, reducer_or_query, &table_id)
.inc();
// Increment table rows gauge
DB_METRICS.rdb_num_table_rows.with_label_values(db, &table_id).inc();
METRICS.rdb_num_table_rows.with_label_values(db, &table_id).inc();
Operation::Insert
}
TxOp::Delete => {
Expand All @@ -390,7 +391,7 @@ impl CommitLogMut {
.with_label_values(workload, db, reducer_or_query, &table_id)
.inc();
// Decrement table rows gauge
DB_METRICS.rdb_num_table_rows.with_label_values(db, &table_id).dec();
METRICS.rdb_num_table_rows.with_label_values(db, &table_id).dec();
Operation::Delete
}
};
Expand Down
16 changes: 8 additions & 8 deletions crates/core/src/db/datastore/locking_tx_datastore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use crate::{
};
use anyhow::anyhow;
use parking_lot::{lock_api::ArcRwLockWriteGuard, RawRwLock, RwLock};
use spacetimedb_lib::Address;
use spacetimedb_lib::{metrics::METRICS, Address};
use spacetimedb_primitives::*;
use spacetimedb_sats::data_key::{DataKey, ToDataKey};
use spacetimedb_sats::db::def::*;
Expand Down Expand Up @@ -250,7 +250,7 @@ impl CommittedState {
for schema in system_tables() {
let table_id = schema.table_id;
// Reset the row count metric for this system table
DB_METRICS
METRICS
.rdb_num_table_rows
.with_label_values(&database_address, &table_id.0)
.set(0);
Expand Down Expand Up @@ -283,7 +283,7 @@ impl CommittedState {

st_columns.rows.insert(RowId(data_key), row);
// Increment row count for st_columns
DB_METRICS
METRICS
.rdb_num_table_rows
.with_label_values(&database_address, &ST_COLUMNS_ID.into())
.inc();
Expand Down Expand Up @@ -311,7 +311,7 @@ impl CommittedState {
let data_key = row.to_data_key();
st_constraints.rows.insert(RowId(data_key), row);
// Increment row count for st_constraints
DB_METRICS
METRICS
.rdb_num_table_rows
.with_label_values(&database_address, &ST_CONSTRAINTS_ID.into())
.inc();
Expand Down Expand Up @@ -340,7 +340,7 @@ impl CommittedState {
let data_key = row.to_data_key();
st_indexes.rows.insert(RowId(data_key), row);
// Increment row count for st_indexes
DB_METRICS
METRICS
.rdb_num_table_rows
.with_label_values(&database_address, &ST_INDEXES_ID.into())
.inc();
Expand Down Expand Up @@ -375,7 +375,7 @@ impl CommittedState {
let data_key = row.to_data_key();
st_sequences.rows.insert(RowId(data_key), row);
// Increment row count for st_sequences
DB_METRICS
METRICS
.rdb_num_table_rows
.with_label_values(&database_address, &ST_SEQUENCES_ID.into())
.inc();
Expand Down Expand Up @@ -1885,7 +1885,7 @@ impl Locking {
committed_state
.table_rows(table_id, schema)
.remove(&RowId(write.data_key));
DB_METRICS
METRICS
.rdb_num_table_rows
.with_label_values(&self.database_address, &table_id.into())
.dec();
Expand Down Expand Up @@ -1914,7 +1914,7 @@ impl Locking {
committed_state
.table_rows(table_id, schema)
.insert(RowId(write.data_key), product_value);
DB_METRICS
METRICS
.rdb_num_table_rows
.with_label_values(&self.database_address, &table_id.into())
.inc();
Expand Down
5 changes: 0 additions & 5 deletions crates/core/src/db/db_metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ metrics_group!(
#[labels(table_id: u32)]
pub rdb_delete_by_rel_time: HistogramVec,

#[name = spacetime_num_table_rows]
#[help = "The number of rows in a table"]
#[labels(db: Address, table_id: u32)]
pub rdb_num_table_rows: IntGaugeVec,

#[name = spacetime_num_rows_inserted_cumulative]
#[help = "The cumulative number of rows inserted into a table"]
#[labels(txn_type: WorkloadType, db: Address, reducer_or_query: str, table_id: u32)]
Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/db/relational_db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use fs2::FileExt;
use nonempty::NonEmpty;
use spacetimedb_lib::metrics::METRICS;
use std::borrow::Cow;
use std::fs::{create_dir_all, File};
use std::ops::RangeBounds;
Expand Down Expand Up @@ -376,7 +377,7 @@ impl RelationalDB {
.with_label_values(&table_id.0)
.start_timer();
self.inner.drop_table_mut_tx(tx, table_id).map(|_| {
DB_METRICS
METRICS
.rdb_num_table_rows
.with_label_values(&self.address, &table_id.into())
.set(0)
Expand Down
2 changes: 2 additions & 0 deletions crates/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ derive_more.workspace = true
enum-as-inner.workspace = true
hex.workspace = true
itertools.workspace = true
once_cell.workspace = true
prometheus.workspace = true
serde = { workspace = true, optional = true }
serde_with = {workspace = true, optional = true }
thiserror.workspace = true
Expand Down
1 change: 1 addition & 0 deletions crates/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use spacetimedb_sats::{impl_serialize, WithTypespace};
pub mod address;
pub mod filter;
pub mod identity;
pub mod metrics;
pub mod name;
pub mod operator;
pub mod primary_key;
Expand Down
16 changes: 16 additions & 0 deletions crates/lib/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use once_cell::sync::Lazy;
use prometheus::IntGaugeVec;
use spacetimedb_lib::Address;
use spacetimedb_metrics::metrics_group;

metrics_group!(
#[non_exhaustive]
pub struct Metrics {
#[name = spacetime_num_table_rows]
#[help = "The number of rows in a table"]
#[labels(db: Address, table_id: u32)]
pub rdb_num_table_rows: IntGaugeVec,
}
);

pub static METRICS: Lazy<Metrics> = Lazy::new(Metrics::new);

0 comments on commit 89b2864

Please sign in to comment.