Add metrics

This commit is contained in:
jude
2024-03-24 20:37:29 +00:00
parent 4a80d42f86
commit ecd75d6f55
4 changed files with 30 additions and 53 deletions

View File

@@ -1,40 +0,0 @@
use axum::{routing::get, Router};
use lazy_static;
use log::warn;
use prometheus::{register_int_counter, IntCounter, Registry};
lazy_static! {
static ref REGISTRY: Registry = Registry::new();
pub static ref REMINDERS_SENT: IntCounter =
register_int_counter!("reminders_sent", "Number of reminders sent").unwrap();
pub static ref REMINDERS_FAILED: IntCounter =
register_int_counter!("reminders_failed", "Number of reminders failed").unwrap();
}
pub fn init_metrics() {
REGISTRY.register(Box::new(REMINDERS_SENT.clone())).unwrap();
REGISTRY.register(Box::new(REMINDERS_FAILED.clone())).unwrap();
}
pub async fn serve() {
let app = Router::new().route("/metrics", get(metrics));
let metrics_port = std::env("PROMETHEUS_PORT").unwrap();
let listener =
tokio::net::TcpListener::bind(format!("localhost:{}", metrics_port)).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn metrics() -> String {
let encoder = prometheus::TextEncoder::new();
let res_custom = encoder.encode_to_string(&REGISTRY.gather());
match res_custom {
Ok(s) => s,
Err(e) => {
warn!("Error encoding metrics: {:?}", e);
String::new()
}
}
}

View File

@@ -26,7 +26,7 @@ use sqlx::{
Executor,
};
use crate::Database;
use crate::{metrics::REMINDER_COUNTER, Database};
lazy_static! {
pub static ref TIMEFROM_REGEX: Regex =
@@ -442,10 +442,13 @@ impl Reminder {
None => error.to_string(),
};
REMINDER_COUNTER.with_label_values(&[self.channel_id.to_string().as_str(), &message]).inc();
error!("[Reminder {}] {}", self.id, message);
}
async fn log_success(&self) {}
async fn log_success(&self) {
REMINDER_COUNTER.with_label_values(&[self.channel_id.to_string().as_str()]).inc()
}
async fn set_sent(&self, pool: impl Executor<'_, Database = Database> + Copy) {
sqlx::query!("UPDATE reminders SET `status` = 'sent' WHERE `id` = ?", self.id)