diff --git a/libsql-server/src/connection/libsql.rs b/libsql-server/src/connection/libsql.rs index 21ee7d99da..aabcfa25c1 100644 --- a/libsql-server/src/connection/libsql.rs +++ b/libsql-server/src/connection/libsql.rs @@ -945,6 +945,7 @@ impl Connection { }; self.stats.inc_rows_read(rows_read); self.stats.inc_rows_written(rows_written); + self.stats.inc_query(elapsed); let weight = rows_read + rows_written; if self.stats.qualifies_as_top_query(weight) { self.stats.add_top_query(crate::stats::TopQuery::new( diff --git a/libsql-server/src/http/admin/stats.rs b/libsql-server/src/http/admin/stats.rs index 75c772e4b7..56fb33e8f0 100644 --- a/libsql-server/src/http/admin/stats.rs +++ b/libsql-server/src/http/admin/stats.rs @@ -18,6 +18,8 @@ pub struct StatsResponse { pub rows_read_count: u64, pub rows_written_count: u64, pub storage_bytes_used: u64, + pub query_count: u64, + pub query_latency: u64, pub write_requests_delegated: u64, pub replication_index: FrameNo, pub top_queries: Vec, @@ -35,6 +37,8 @@ impl From<&Stats> for StatsResponse { write_requests_delegated: stats.write_requests_delegated(), replication_index: stats.get_current_frame_no(), embedded_replica_frames_replicated: stats.get_embedded_replica_frames_replicated(), + query_count: stats.get_query_count(), + query_latency: stats.get_query_latency(), top_queries: stats .top_queries() .read() diff --git a/libsql-server/src/stats.rs b/libsql-server/src/stats.rs index 7860702012..1a3464513c 100644 --- a/libsql-server/src/stats.rs +++ b/libsql-server/src/stats.rs @@ -82,6 +82,10 @@ pub struct Stats { slowest_queries: Arc>>, #[serde(default)] embedded_replica_frames_replicated: AtomicU64, + #[serde(default)] + query_count: AtomicU64, + #[serde(default)] + query_latency: AtomicU64, } impl Stats { @@ -119,6 +123,13 @@ impl Stats { self.rows_written.fetch_add(n, Ordering::Relaxed); } + pub fn inc_query(&self, ms: u64) { + counter!("libsql_server_query_count", 1, "namespace" => self.namespace.to_string()); + counter!("libsql_server_query_latency", ms, "namespace" => self.namespace.to_string()); + self.query_count.fetch_add(1, Ordering::Relaxed); + self.query_latency.fetch_add(ms, Ordering::Relaxed); + } + /// increments the number of read rows by n pub fn inc_rows_read(&self, n: u64) { counter!("libsql_server_rows_read", n, "namespace" => self.namespace.to_string()); @@ -177,6 +188,14 @@ impl Stats { self.current_frame_no.load(Ordering::Relaxed) } + pub(crate) fn get_query_count(&self) -> FrameNo { + self.query_count.load(Ordering::Relaxed) + } + + pub(crate) fn get_query_latency(&self) -> FrameNo { + self.query_latency.load(Ordering::Relaxed) + } + pub(crate) fn add_top_query(&self, query: TopQuery) { let mut top_queries = self.top_queries.write().unwrap(); tracing::debug!(