From c377b136aaf55ad13f28a34410ca79c35bbfb2f3 Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Wed, 1 Feb 2023 18:09:30 -0600 Subject: [PATCH] improvement: prometheus metric for db connections (sqlite) --- src/repo/sqlite.rs | 6 +++++- src/server.rs | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/repo/sqlite.rs b/src/repo/sqlite.rs index e756af6..d016e71 100644 --- a/src/repo/sqlite.rs +++ b/src/repo/sqlite.rs @@ -351,6 +351,10 @@ impl NostrRepo for SqliteRepo { let mut filter_count = 0; // remove duplicates from the filter list. if let Ok(mut conn) = self.read_pool.get() { + { + let pool_state = self.read_pool.state(); + metrics.db_connections.set((pool_state.connections - pool_state.idle_connections).into()); + } for filter in sub.filters.iter() { let filter_start = Instant::now(); filter_count += 1; @@ -367,7 +371,7 @@ impl NostrRepo for SqliteRepo { let mut last_successful_send = Instant::now(); // execute the query. // make the actual SQL query (with parameters inserted) available - conn.trace(Some(|x| {debug!("SQL trace: {:?}", x)})); + conn.trace(Some(|x| {trace!("SQL trace: {:?}", x)})); let mut stmt = conn.prepare_cached(&q)?; let mut event_rows = stmt.query(rusqlite::params_from_iter(p))?; diff --git a/src/server.rs b/src/server.rs index ef0029d..d956459 100644 --- a/src/server.rs +++ b/src/server.rs @@ -14,6 +14,7 @@ use crate::nip05; use crate::notice::Notice; use crate::subscription::Subscription; use prometheus::IntCounterVec; +use prometheus::IntGauge; use prometheus::{Encoder, Histogram, IntCounter, HistogramOpts, Opts, Registry, TextEncoder}; use futures::SinkExt; use futures::StreamExt; @@ -248,6 +249,9 @@ fn create_metrics() -> (Registry, NostrMetrics) { "nostr_connections_total", "New connections", )).unwrap(); + let db_connections = IntGauge::with_opts(Opts::new( + "nostr_db_connections", "Active database connections" + )).unwrap(); let query_aborts = IntCounterVec::new( Opts::new("nostr_query_abort_total", "Aborted queries"), vec!["reason"].as_slice(), @@ -273,6 +277,7 @@ fn create_metrics() -> (Registry, NostrMetrics) { registry.register(Box::new(write_events.clone())).unwrap(); registry.register(Box::new(sent_events.clone())).unwrap(); registry.register(Box::new(connections.clone())).unwrap(); + registry.register(Box::new(db_connections.clone())).unwrap(); registry.register(Box::new(query_aborts.clone())).unwrap(); registry.register(Box::new(cmd_req.clone())).unwrap(); registry.register(Box::new(cmd_event.clone())).unwrap(); @@ -284,6 +289,7 @@ fn create_metrics() -> (Registry, NostrMetrics) { write_events, sent_events, connections, + db_connections, disconnects, query_aborts, cmd_req, @@ -837,6 +843,7 @@ async fn nostr_server( pub struct NostrMetrics { pub query_sub: Histogram, // response time of successful subscriptions pub query_db: Histogram, // individual database query execution time + pub db_connections: IntGauge, // database connections in use pub write_events: Histogram, // response time of event writes pub sent_events: IntCounterVec, // count of events sent to clients pub connections: IntCounter, // count of websocket connections