improvement: prometheus metric for db connections (sqlite)

This commit is contained in:
Greg Heartsfield 2023-02-01 18:09:30 -06:00
parent bca5614a82
commit c377b136aa
2 changed files with 12 additions and 1 deletions

View File

@ -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))?;

View File

@ -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