Serve metrics via :31755
This commit is contained in:
@@ -5,7 +5,7 @@ use poise::serenity_prelude::{
|
||||
ReactionType,
|
||||
};
|
||||
|
||||
#[cfg(feature = "prometheus")]
|
||||
#[cfg(feature = "metrics")]
|
||||
use crate::metrics::PLAY_COUNTER;
|
||||
use crate::{
|
||||
cmds::autocomplete_sound,
|
||||
@@ -29,7 +29,7 @@ pub async fn play(
|
||||
|
||||
let guild = ctx.guild().unwrap();
|
||||
|
||||
#[cfg(feature = "prometheus")]
|
||||
#[cfg(feature = "metrics")]
|
||||
PLAY_COUNTER.inc();
|
||||
|
||||
ctx.say(
|
||||
|
||||
@@ -5,7 +5,7 @@ mod cmds;
|
||||
mod consts;
|
||||
mod error;
|
||||
mod event_handlers;
|
||||
#[cfg(feature = "prometheus")]
|
||||
#[cfg(feature = "metrics")]
|
||||
mod metrics;
|
||||
mod models;
|
||||
mod utils;
|
||||
@@ -144,6 +144,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
|
||||
sqlx::migrate!().run(&database).await?;
|
||||
|
||||
#[cfg(feature = "metrics")]
|
||||
{
|
||||
metrics::init_metrics();
|
||||
tokio::spawn(async { metrics::serve().await });
|
||||
}
|
||||
|
||||
poise::Framework::builder()
|
||||
.token(discord_token)
|
||||
.setup(move |ctx, _bot, framework| {
|
||||
|
||||
@@ -1,7 +1,40 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use axum::{routing::get, Router};
|
||||
use lazy_static;
|
||||
use prometheus::{register_int_counter, IntCounter};
|
||||
use log::warn;
|
||||
use prometheus::{register_int_counter, IntCounter, Registry};
|
||||
|
||||
lazy_static! {
|
||||
static ref REGISTRY: Registry = Registry::new();
|
||||
pub static ref PLAY_COUNTER: IntCounter =
|
||||
register_int_counter!("play", "Number of calls to /play").unwrap();
|
||||
}
|
||||
|
||||
pub fn init_metrics() {
|
||||
REGISTRY.register(Box::new(PLAY_COUNTER.clone())).unwrap();
|
||||
}
|
||||
|
||||
pub async fn serve() {
|
||||
let app = Router::new().route("/", get(metrics));
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 31755));
|
||||
|
||||
axum::Server::bind(&addr)
|
||||
.serve(app.into_make_service())
|
||||
.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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user