Add metrics
This commit is contained in:
		| @@ -1,10 +1,11 @@ | |||||||
| use poise::serenity_prelude::ActivityData; |  | ||||||
| use poise::{ | use poise::{ | ||||||
|     serenity_prelude as serenity, |     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( | pub async fn listener( | ||||||
|     ctx: &serenity::Context, |     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; |                 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(); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|         _ => {} |         _ => {} | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -6,12 +6,22 @@ use prometheus::{IntCounterVec, Opts, Registry}; | |||||||
| lazy_static! { | lazy_static! { | ||||||
|     pub static ref REGISTRY: Registry = Registry::new(); |     pub static ref REGISTRY: Registry = Registry::new(); | ||||||
|     pub static ref REQUEST_COUNTER: IntCounterVec = |     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(); |             .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() { | pub fn init_metrics() { | ||||||
|     REGISTRY.register(Box::new(REQUEST_COUNTER.clone())).unwrap(); |     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() { | pub async fn serve() { | ||||||
| @@ -25,12 +35,9 @@ async fn metrics() -> String { | |||||||
|     let encoder = prometheus::TextEncoder::new(); |     let encoder = prometheus::TextEncoder::new(); | ||||||
|     let res_custom = encoder.encode_to_string(®ISTRY.gather()); |     let res_custom = encoder.encode_to_string(®ISTRY.gather()); | ||||||
|  |  | ||||||
|     match res_custom { |     res_custom.unwrap_or_else(|e| { | ||||||
|         Ok(s) => s, |         warn!("Error encoding metrics: {:?}", e); | ||||||
|         Err(e) => { |  | ||||||
|             warn!("Error encoding metrics: {:?}", e); |  | ||||||
|  |  | ||||||
|             String::new() |         String::new() | ||||||
|         } |     }) | ||||||
|     } |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -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(®ISTRY.gather()); |  | ||||||
|  |  | ||||||
|     match res_custom { |  | ||||||
|         Ok(s) => s, |  | ||||||
|         Err(e) => { |  | ||||||
|             warn!("Error encoding metrics: {:?}", e); |  | ||||||
|  |  | ||||||
|             String::new() |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -26,7 +26,7 @@ use sqlx::{ | |||||||
|     Executor, |     Executor, | ||||||
| }; | }; | ||||||
|  |  | ||||||
| use crate::Database; | use crate::{metrics::REMINDER_COUNTER, Database}; | ||||||
|  |  | ||||||
| lazy_static! { | lazy_static! { | ||||||
|     pub static ref TIMEFROM_REGEX: Regex = |     pub static ref TIMEFROM_REGEX: Regex = | ||||||
| @@ -442,10 +442,13 @@ impl Reminder { | |||||||
|             None => error.to_string(), |             None => error.to_string(), | ||||||
|         }; |         }; | ||||||
|  |  | ||||||
|  |         REMINDER_COUNTER.with_label_values(&[self.channel_id.to_string().as_str(), &message]).inc(); | ||||||
|         error!("[Reminder {}] {}", self.id, message); |         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) { |     async fn set_sent(&self, pool: impl Executor<'_, Database = Database> + Copy) { | ||||||
|         sqlx::query!("UPDATE reminders SET `status` = 'sent' WHERE `id` = ?", self.id) |         sqlx::query!("UPDATE reminders SET `status` = 'sent' WHERE `id` = ?", self.id) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user