Adding metric support

This commit is contained in:
jude 2023-10-21 21:33:50 +01:00
parent 6cfdc10a6a
commit 6615e05196
6 changed files with 40 additions and 4 deletions

22
Cargo.lock generated
View File

@ -1549,6 +1549,27 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "prometheus"
version = "0.13.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "449811d15fbdf5ceb5c1144416066429cf82316e2ec8ce0c1f6f8a02e7bbcf8c"
dependencies = [
"cfg-if",
"fnv",
"lazy_static",
"memchr",
"parking_lot 0.12.1",
"protobuf",
"thiserror",
]
[[package]]
name = "protobuf"
version = "2.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "106dd99e98437432fed6519dedecfade6a06a73bb7b2a1e019fdd2bee5778d94"
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.29" version = "1.0.29"
@ -2137,6 +2158,7 @@ dependencies = [
"lazy_static", "lazy_static",
"log", "log",
"poise", "poise",
"prometheus",
"regex", "regex",
"reqwest", "reqwest",
"serde", "serde",

View File

@ -20,6 +20,7 @@ serde_json = "1.0"
dashmap = "5.3" dashmap = "5.3"
serde = "1.0" serde = "1.0"
dotenv = "0.15.0" dotenv = "0.15.0"
prometheus = { version = "0.13.3", optional = true }
[patch."https://github.com/serenity-rs/serenity"] [patch."https://github.com/serenity-rs/serenity"]
serenity = { version = "0.11.6" } serenity = { version = "0.11.6" }

View File

@ -5,6 +5,8 @@ use poise::serenity_prelude::{
ReactionType, ReactionType,
}; };
#[cfg(feature = "prometheus")]
use crate::metrics::PLAY_COUNTER;
use crate::{ use crate::{
cmds::autocomplete_sound, cmds::autocomplete_sound,
models::{guild_data::CtxGuildData, sound::SoundCtx}, models::{guild_data::CtxGuildData, sound::SoundCtx},
@ -27,6 +29,9 @@ pub async fn play(
let guild = ctx.guild().unwrap(); let guild = ctx.guild().unwrap();
#[cfg(feature = "prometheus")]
PLAY_COUNTER.inc();
ctx.say( ctx.say(
play_from_query( play_from_query(
&ctx.serenity_context(), &ctx.serenity_context(),

View File

@ -105,10 +105,9 @@ pub async fn listener(ctx: &Context, event: &poise::Event<'_>, data: &Data) -> R
let mut sound = sqlx::query_as_unchecked!( let mut sound = sqlx::query_as_unchecked!(
Sound, Sound,
" "
SELECT name, id, public, server_id, uploader_id SELECT name, id, public, server_id, uploader_id
FROM sounds FROM sounds
WHERE id = ? WHERE id = ?",
",
join_id join_id
) )
.fetch_one(&data.database) .fetch_one(&data.database)

View File

@ -5,6 +5,8 @@ mod cmds;
mod consts; mod consts;
mod error; mod error;
mod event_handlers; mod event_handlers;
#[cfg(feature = "prometheus")]
mod metrics;
mod models; mod models;
mod utils; mod utils;

7
src/metrics.rs Normal file
View File

@ -0,0 +1,7 @@
use lazy_static;
use prometheus::{register_int_counter, IntCounter};
lazy_static! {
pub static ref PLAY_COUNTER: IntCounter =
register_int_counter!("play", "Number of calls to /play").unwrap();
}