@@ -26,10 +26,10 @@ use spacetimedb_primitives::{ColList, ConstraintId, IndexId, SequenceId, TableId
2626use spacetimedb_sats:: db:: def:: { IndexDef , SequenceDef , TableDef , TableSchema } ;
2727use spacetimedb_sats:: { hash:: Hash , AlgebraicValue , DataKey , ProductType , ProductValue } ;
2828use spacetimedb_table:: { indexes:: RowPointer , table:: RowRef } ;
29- use std:: borrow:: Cow ;
3029use std:: ops:: RangeBounds ;
3130use std:: sync:: Arc ;
3231use std:: time:: Instant ;
32+ use std:: { borrow:: Cow , time:: Duration } ;
3333
3434pub type Result < T > = std:: result:: Result < T , DBError > ;
3535
@@ -474,6 +474,48 @@ impl MutTxDatastore for Locking {
474474 }
475475}
476476
477+ #[ cfg( feature = "metrics" ) ]
478+ pub ( crate ) fn record_metrics ( ctx : & ExecutionContext , tx_timer : Instant , lock_wait_time : Duration , committed : bool ) {
479+ let workload = & ctx. workload ( ) ;
480+ let db = & ctx. database ( ) ;
481+ let reducer = ctx. reducer_name ( ) ;
482+ let elapsed_time = tx_timer. elapsed ( ) ;
483+ let cpu_time = elapsed_time - lock_wait_time;
484+
485+ let elapsed_time = elapsed_time. as_secs_f64 ( ) ;
486+ let cpu_time = cpu_time. as_secs_f64 ( ) ;
487+ // Note, we record empty transactions in our metrics.
488+ // That is, transactions that don't write any rows to the commit log.
489+ DB_METRICS
490+ . rdb_num_txns
491+ . with_label_values ( workload, db, reducer, & committed)
492+ . inc ( ) ;
493+ DB_METRICS
494+ . rdb_txn_cpu_time_sec
495+ . with_label_values ( workload, db, reducer)
496+ . observe ( cpu_time) ;
497+ DB_METRICS
498+ . rdb_txn_elapsed_time_sec
499+ . with_label_values ( workload, db, reducer)
500+ . observe ( elapsed_time) ;
501+
502+ let mut guard = MAX_TX_CPU_TIME . lock ( ) . unwrap ( ) ;
503+ let max_cpu_time = * guard
504+ . entry ( ( * db, * workload, reducer. to_owned ( ) ) )
505+ . and_modify ( |max| {
506+ if cpu_time > * max {
507+ * max = cpu_time;
508+ }
509+ } )
510+ . or_insert_with ( || cpu_time) ;
511+
512+ drop ( guard) ;
513+ DB_METRICS
514+ . rdb_txn_cpu_time_sec_max
515+ . with_label_values ( workload, db, reducer)
516+ . set ( max_cpu_time) ;
517+ }
518+
477519impl MutTx for Locking {
478520 type MutTx = MutTxId ;
479521
@@ -493,74 +535,12 @@ impl MutTx for Locking {
493535 }
494536
495537 fn rollback_mut_tx ( & self , ctx : & ExecutionContext , tx : Self :: MutTx ) {
496- let workload = & ctx. workload ( ) ;
497- let db = & ctx. database ( ) ;
498- let reducer = ctx. reducer_name ( ) ;
499- let elapsed_time = tx. timer . elapsed ( ) ;
500- let cpu_time = elapsed_time - tx. lock_wait_time ;
501- #[ cfg( feature = "metrics" ) ]
502- DB_METRICS
503- . rdb_num_txns
504- . with_label_values ( workload, db, reducer, & false )
505- . inc ( ) ;
506-
507- #[ cfg( feature = "metrics" ) ]
508- DB_METRICS
509- . rdb_txn_cpu_time_sec
510- . with_label_values ( workload, db, reducer)
511- . observe ( cpu_time. as_secs_f64 ( ) ) ;
512-
513- #[ cfg( feature = "metrics" ) ]
514- DB_METRICS
515- . rdb_txn_elapsed_time_sec
516- . with_label_values ( workload, db, reducer)
517- . observe ( elapsed_time. as_secs_f64 ( ) ) ;
538+ record_metrics ( ctx, tx. timer , tx. lock_wait_time , false ) ;
518539 tx. rollback ( ) ;
519540 }
520541
521542 fn commit_mut_tx ( & self , ctx : & ExecutionContext , tx : Self :: MutTx ) -> Result < Option < TxData > > {
522- #[ cfg( feature = "metrics" ) ]
523- {
524- let workload = & ctx. workload ( ) ;
525- let db = & ctx. database ( ) ;
526- let reducer = ctx. reducer_name ( ) ;
527- let elapsed_time = tx. timer . elapsed ( ) ;
528- let cpu_time = elapsed_time - tx. lock_wait_time ;
529-
530- let elapsed_time = elapsed_time. as_secs_f64 ( ) ;
531- let cpu_time = cpu_time. as_secs_f64 ( ) ;
532- // Note, we record empty transactions in our metrics.
533- // That is, transactions that don't write any rows to the commit log.
534- DB_METRICS
535- . rdb_num_txns
536- . with_label_values ( workload, db, reducer, & true )
537- . inc ( ) ;
538- DB_METRICS
539- . rdb_txn_cpu_time_sec
540- . with_label_values ( workload, db, reducer)
541- . observe ( cpu_time) ;
542- DB_METRICS
543- . rdb_txn_elapsed_time_sec
544- . with_label_values ( workload, db, reducer)
545- . observe ( elapsed_time) ;
546-
547- let mut guard = MAX_TX_CPU_TIME . lock ( ) . unwrap ( ) ;
548- let max_cpu_time = * guard
549- . entry ( ( * db, * workload, reducer. to_owned ( ) ) )
550- . and_modify ( |max| {
551- if cpu_time > * max {
552- * max = cpu_time;
553- }
554- } )
555- . or_insert_with ( || cpu_time) ;
556-
557- drop ( guard) ;
558- DB_METRICS
559- . rdb_txn_cpu_time_sec_max
560- . with_label_values ( workload, db, reducer)
561- . set ( max_cpu_time) ;
562- }
563-
543+ record_metrics ( ctx, tx. timer , tx. lock_wait_time , true ) ;
564544 Ok ( Some ( tx. commit ( ) ) )
565545 }
566546
0 commit comments