From 7733e908cd6fd529dd57728b6132f63f1b785589 Mon Sep 17 00:00:00 2001 From: Paul Z Date: Fri, 10 Nov 2023 16:38:28 +0100 Subject: [PATCH] prometheus --- Cargo.lock | 8 ++++---- src/game.rs | 52 ++++++++++++++++++++++++++++++++++++++++------------ src/main.rs | 13 +++++-------- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d653a94..059dabf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -554,9 +554,9 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.10.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" +checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece" dependencies = [ "humantime", "is-terminal", @@ -813,9 +813,9 @@ dependencies = [ [[package]] name = "http" -version = "0.2.9" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd6effc99afb63425aff9b05836f029929e345a6148a14b7ecd5ab67af944482" +checksum = "f95b9abcae896730d42b78e09c155ed4ddf82c07b4de772c64aee5b2d8b7c150" dependencies = [ "bytes", "fnv", diff --git a/src/game.rs b/src/game.rs index 2d1a73b..d4600b9 100644 --- a/src/game.rs +++ b/src/game.rs @@ -271,23 +271,23 @@ pub struct GameLabels { game: String, } -pub struct GameCollector { +pub struct RunningGamesCollector { games: Arc>>, } -impl GameCollector { +impl RunningGamesCollector { pub fn new(games: Arc>>) -> Self { Self { games } } } -impl Debug for GameCollector { +impl Debug for RunningGamesCollector { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("GameCollector").finish() } } -impl Collector for GameCollector { +impl Collector for RunningGamesCollector { fn encode( &self, mut encoder: prometheus_client::encoding::DescriptorEncoder, @@ -295,6 +295,42 @@ impl Collector for GameCollector { let games = self.games.blocking_read(); let running_games = ConstGauge::new(games.len() as i64); + drop(games); + + let running_games_encoder = encoder.encode_descriptor( + "ars_running_games", + "number of running games", + None, + running_games.metric_type(), + )?; + running_games.encode(running_games_encoder)?; + Ok(()) + } +} + +pub struct GameParticipantsCollector { + games: Arc>>, +} + +impl GameParticipantsCollector { + pub fn new(games: Arc>>) -> Self { + Self { games } + } +} + +impl Debug for GameParticipantsCollector { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("GameCollector").finish() + } +} + +impl Collector for GameParticipantsCollector { + fn encode( + &self, + mut encoder: prometheus_client::encoding::DescriptorEncoder, + ) -> Result<(), std::fmt::Error> { + let games = self.games.blocking_read(); + let participants = Family::::default(); games.iter().for_each(|(id, game)| { @@ -307,14 +343,6 @@ impl Collector for GameCollector { drop(games); - let running_games_encoder = encoder.encode_descriptor( - "ars_running_games", - "number of running games", - None, - running_games.metric_type(), - )?; - running_games.encode(running_games_encoder)?; - let participants_encoder = encoder.encode_descriptor( "ars_game_participants", "number of participants for a game", diff --git a/src/main.rs b/src/main.rs index edbc119..33817ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,13 +23,9 @@ use axum_oidc::{ error::MiddlewareError, EmptyAdditionalClaims, OidcAuthLayer, OidcClaims, OidcLoginLayer, }; use futures_util::Stream; -use game::{Game, GameCollector, GameId, PlayerId}; +use game::{Game, GameId, GameParticipantsCollector, PlayerId, RunningGamesCollector}; use garbage_collector::{start_gc, GarbageCollectorItem}; -use prometheus_client::{ - encoding::EncodeLabelSet, - metrics::{counter::Counter, family::Family, gauge::Gauge}, - registry::{self, Registry}, -}; +use prometheus_client::{metrics::counter::Counter, registry::Registry}; use question::{single_choice::SingleChoiceQuestion, Question}; use sailfish::TemplateOnce; use serde::{Deserialize, Serialize}; @@ -116,12 +112,13 @@ pub async fn main() { let mut registry = Registry::default(); registry.register( - "arc_games_total", + "ars_games_total", "number of games created", app_metrics.arc_games_total.clone(), ); - registry.register_collector(Box::new(GameCollector::new(games.clone()))); + registry.register_collector(Box::new(RunningGamesCollector::new(games.clone()))); + registry.register_collector(Box::new(GameParticipantsCollector::new(games.clone()))); start_gc(game_expiry.clone(), games.clone());