From 6aeab7f7aa4a2a41258fff503e7a1b536048aaf4 Mon Sep 17 00:00:00 2001 From: Astro Date: Tue, 20 Dec 2022 03:40:51 +0100 Subject: [PATCH] db: sprinkle with metrics::histogram! --- src/db.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/db.rs b/src/db.rs index 72f3126..ce944b7 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,8 +1,8 @@ -use std::sync::Arc; +use std::{sync::Arc, time::Instant}; +use metrics::histogram; use tokio_postgres::{Client, Error, NoTls, Statement}; - const CREATE_SCHEMA_COMMANDS: &[&str] = &[ "CREATE TABLE IF NOT EXISTS follows (id TEXT, inbox TEXT, actor TEXT, UNIQUE (inbox, actor))", "CREATE INDEX IF NOT EXISTS follows_actor ON follows (actor) INCLUDE (inbox)", @@ -58,20 +58,29 @@ impl Database { } pub async fn add_follow(&self, id: &str, inbox: &str, actor: &str) -> Result<(), Error> { + let t1 = Instant::now(); self.inner.client.execute(&self.inner.add_follow, &[&id, &inbox, &actor]) .await?; + let t2 = Instant::now(); + histogram!("sql", t2 - t1, "query" => "add_follow"); Ok(()) } pub async fn del_follow(&self, id: &str, actor: &str) -> Result<(), Error> { + let t1 = Instant::now(); self.inner.client.execute(&self.inner.del_follow, &[&id, &actor]) .await?; + let t2 = Instant::now(); + histogram!("sql", t2 - t1, "query" => "del_follow"); Ok(()) } pub async fn get_following_inboxes(&self, actor: &str) -> Result, Error> { + let t1 = Instant::now(); let rows = self.inner.client.query(&self.inner.get_following_inboxes, &[&actor]) .await?; + let t2 = Instant::now(); + histogram!("sql", t2 - t1, "query" => "get_following_inboxes"); Ok(rows.into_iter() .map(|row| row.get(0)) )