diff --git a/libsql-server/Cargo.toml b/libsql-server/Cargo.toml index 5515efe192..f0e2171916 100644 --- a/libsql-server/Cargo.toml +++ b/libsql-server/Cargo.toml @@ -35,6 +35,7 @@ jsonwebtoken = "8.2.0" libsql = { path = "../libsql/", optional = true } libsql-replication = { path = "../libsql-replication" } metrics = "0.21.1" +metrics-util = "0.15" metrics-exporter-prometheus = "0.12.1" mimalloc = { version = "0.1.36", default-features = false } nix = { version = "0.26.2", features = ["fs"] } diff --git a/libsql-server/src/connection/libsql.rs b/libsql-server/src/connection/libsql.rs index 597e204919..439463ec17 100644 --- a/libsql-server/src/connection/libsql.rs +++ b/libsql-server/src/connection/libsql.rs @@ -749,9 +749,8 @@ impl Connection { )); } - // TODO: change how these metrics are gathered to limit cardinality - // self.stats - // .update_query_metrics(sql, rows_read, rows_written, mem_used, elapsed) + self.stats + .update_query_metrics(rows_read, rows_written, mem_used, elapsed) } fn describe(&self, sql: &str) -> crate::Result { diff --git a/libsql-server/src/http/admin/mod.rs b/libsql-server/src/http/admin/mod.rs index 245cc447a6..3e3935f71d 100644 --- a/libsql-server/src/http/admin/mod.rs +++ b/libsql-server/src/http/admin/mod.rs @@ -12,6 +12,7 @@ use std::cell::OnceCell; use std::io::ErrorKind; use std::path::PathBuf; use std::sync::Arc; +use std::time::Duration; use tokio_util::io::ReaderStream; use url::Url; @@ -65,9 +66,24 @@ where M: MakeNamespace, C: Connector, { + let app_label = std::env::var("SQLD_APP_LABEL").ok(); + let prom_handle = if !disable_metrics { let lock = PROM_HANDLE.lock(); - let prom_handle = lock.get_or_init(|| PrometheusBuilder::new().install_recorder().unwrap()); + let prom_handle = lock.get_or_init(|| { + let b = PrometheusBuilder::new().idle_timeout( + metrics_util::MetricKindMask::ALL, + Some(Duration::from_secs(120)), + ); + + if let Some(app_label) = app_label { + b.add_global_label("app", app_label) + .install_recorder() + .unwrap() + } else { + b.install_recorder().unwrap() + } + }); Some(prom_handle.clone()) } else { None diff --git a/libsql-server/src/stats.rs b/libsql-server/src/stats.rs index 5e06e512eb..62e76e3522 100644 --- a/libsql-server/src/stats.rs +++ b/libsql-server/src/stats.rs @@ -211,19 +211,20 @@ impl Stats { self.slowest_query_threshold.store(0, Ordering::Relaxed); } + // TOOD: Update these metrics with namespace labels in the future so we can localize + // issues to a specific namespace. pub(crate) fn update_query_metrics( &self, - sql: String, rows_read: u64, rows_written: u64, mem_used: u64, elapsed: u64, ) { - increment_counter!("libsql_server_query_count", "namespace" => self.namespace.to_string(), "query" => sql.clone()); - counter!("libsql_server_query_latency", elapsed, "namespace" => self.namespace.to_string(), "query" => sql.clone()); - counter!("libsql_server_query_rows_read", rows_read, "namespace" => self.namespace.to_string(), "query" => sql.clone()); - counter!("libsql_server_query_rows_written", rows_written, "namespace" => self.namespace.to_string(), "query" => sql.clone()); - counter!("libsql_server_query_mem_used", mem_used, "namespace" => self.namespace.to_string(), "query" => sql.clone()); + increment_counter!("libsql_server_query_count"); + counter!("libsql_server_query_latency", elapsed); + counter!("libsql_server_query_rows_read", rows_read); + counter!("libsql_server_query_rows_written", rows_written); + counter!("libsql_server_query_mem_used", mem_used); } }