Skip to content

Commit

Permalink
Add query count and latency to stats
Browse files Browse the repository at this point in the history
  • Loading branch information
athoscouto committed Feb 26, 2024
1 parent 52d8b8f commit 5b28653
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions libsql-server/src/connection/libsql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,7 @@ impl<W: Wal> Connection<W> {
};
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(
Expand Down
4 changes: 4 additions & 0 deletions libsql-server/src/http/admin/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TopQuery>,
Expand All @@ -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()
Expand Down
19 changes: 19 additions & 0 deletions libsql-server/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ pub struct Stats {
slowest_queries: Arc<RwLock<BTreeSet<SlowestQuery>>>,
#[serde(default)]
embedded_replica_frames_replicated: AtomicU64,
#[serde(default)]
query_count: AtomicU64,
#[serde(default)]
query_latency: AtomicU64,
}

impl Stats {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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!(
Expand Down

0 comments on commit 5b28653

Please sign in to comment.