Add metrics

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

View File

@ -1,10 +1,11 @@
use poise::serenity_prelude::ActivityData;
use poise::{
serenity_prelude as serenity,
serenity_prelude::{CreateEmbed, CreateMessage, FullEvent},
serenity_prelude::{ActivityData, CreateEmbed, CreateMessage, FullEvent},
};
use crate::{component_models::ComponentDataModel, Data, Error, THEME_COLOR};
use crate::{
component_models::ComponentDataModel, metrics::COMMAND_COUNTER, Data, Error, THEME_COLOR,
};
pub async fn listener(
ctx: &serenity::Context,
@ -67,6 +68,10 @@ To stay up to date on the latest features and fixes, join our [Discord](https://
component_model.act(ctx, data, &component).await;
}
if let Some(command) = interaction.clone().command() {
COMMAND_COUNTER.with_label_values(&[command.data.name.as_str()]).inc();
}
}
_ => {}
}

View File

@ -6,12 +6,22 @@ use prometheus::{IntCounterVec, Opts, Registry};
lazy_static! {
pub static ref REGISTRY: Registry = Registry::new();
pub static ref REQUEST_COUNTER: IntCounterVec =
IntCounterVec::new(Opts::new("requests", "Requests"), &["method", "status", "route"])
IntCounterVec::new(Opts::new("requests", "Web requests"), &["method", "status", "route"])
.unwrap();
pub static ref REMINDER_COUNTER: IntCounterVec =
IntCounterVec::new(Opts::new("reminders_sent", "Reminders sent"), &["channel"]).unwrap();
pub static ref REMINDER_FAIL_COUNTER: IntCounterVec =
IntCounterVec::new(Opts::new("reminders_sent", "Reminders failed"), &["channel", "error"])
.unwrap();
pub static ref COMMAND_COUNTER: IntCounterVec =
IntCounterVec::new(Opts::new("commands", "Commands used"), &["command"]).unwrap();
}
pub fn init_metrics() {
REGISTRY.register(Box::new(REQUEST_COUNTER.clone())).unwrap();
REGISTRY.register(Box::new(REMINDER_COUNTER.clone())).unwrap();
REGISTRY.register(Box::new(REMINDER_FAIL_COUNTER.clone())).unwrap();
REGISTRY.register(Box::new(COMMAND_COUNTER.clone())).unwrap();
}
pub async fn serve() {
@ -25,12 +35,9 @@ 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);
res_custom.unwrap_or_else(|e| {
warn!("Error encoding metrics: {:?}", e);
String::new()
}
}
String::new()
})
}

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)