prometheus

This commit is contained in:
Paul Zinselmeyer 2023-11-10 16:38:28 +01:00
parent d4d0f21f7d
commit 7733e908cd
3 changed files with 49 additions and 24 deletions

8
Cargo.lock generated
View file

@ -554,9 +554,9 @@ dependencies = [
[[package]] [[package]]
name = "env_logger" name = "env_logger"
version = "0.10.0" version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece"
dependencies = [ dependencies = [
"humantime", "humantime",
"is-terminal", "is-terminal",
@ -813,9 +813,9 @@ dependencies = [
[[package]] [[package]]
name = "http" name = "http"
version = "0.2.9" version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd6effc99afb63425aff9b05836f029929e345a6148a14b7ecd5ab67af944482" checksum = "f95b9abcae896730d42b78e09c155ed4ddf82c07b4de772c64aee5b2d8b7c150"
dependencies = [ dependencies = [
"bytes", "bytes",
"fnv", "fnv",

View file

@ -271,23 +271,23 @@ pub struct GameLabels {
game: String, game: String,
} }
pub struct GameCollector { pub struct RunningGamesCollector {
games: Arc<RwLock<HashMap<GameId, Game>>>, games: Arc<RwLock<HashMap<GameId, Game>>>,
} }
impl GameCollector { impl RunningGamesCollector {
pub fn new(games: Arc<RwLock<HashMap<GameId, Game>>>) -> Self { pub fn new(games: Arc<RwLock<HashMap<GameId, Game>>>) -> Self {
Self { games } Self { games }
} }
} }
impl Debug for GameCollector { impl Debug for RunningGamesCollector {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GameCollector").finish() f.debug_struct("GameCollector").finish()
} }
} }
impl Collector for GameCollector { impl Collector for RunningGamesCollector {
fn encode( fn encode(
&self, &self,
mut encoder: prometheus_client::encoding::DescriptorEncoder, mut encoder: prometheus_client::encoding::DescriptorEncoder,
@ -295,6 +295,42 @@ impl Collector for GameCollector {
let games = self.games.blocking_read(); let games = self.games.blocking_read();
let running_games = ConstGauge::new(games.len() as i64); 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<RwLock<HashMap<GameId, Game>>>,
}
impl GameParticipantsCollector {
pub fn new(games: Arc<RwLock<HashMap<GameId, Game>>>) -> 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::<GameLabels, Gauge>::default(); let participants = Family::<GameLabels, Gauge>::default();
games.iter().for_each(|(id, game)| { games.iter().for_each(|(id, game)| {
@ -307,14 +343,6 @@ impl Collector for GameCollector {
drop(games); 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( let participants_encoder = encoder.encode_descriptor(
"ars_game_participants", "ars_game_participants",
"number of participants for a game", "number of participants for a game",

View file

@ -23,13 +23,9 @@ use axum_oidc::{
error::MiddlewareError, EmptyAdditionalClaims, OidcAuthLayer, OidcClaims, OidcLoginLayer, error::MiddlewareError, EmptyAdditionalClaims, OidcAuthLayer, OidcClaims, OidcLoginLayer,
}; };
use futures_util::Stream; use futures_util::Stream;
use game::{Game, GameCollector, GameId, PlayerId}; use game::{Game, GameId, GameParticipantsCollector, PlayerId, RunningGamesCollector};
use garbage_collector::{start_gc, GarbageCollectorItem}; use garbage_collector::{start_gc, GarbageCollectorItem};
use prometheus_client::{ use prometheus_client::{metrics::counter::Counter, registry::Registry};
encoding::EncodeLabelSet,
metrics::{counter::Counter, family::Family, gauge::Gauge},
registry::{self, Registry},
};
use question::{single_choice::SingleChoiceQuestion, Question}; use question::{single_choice::SingleChoiceQuestion, Question};
use sailfish::TemplateOnce; use sailfish::TemplateOnce;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -116,12 +112,13 @@ pub async fn main() {
let mut registry = Registry::default(); let mut registry = Registry::default();
registry.register( registry.register(
"arc_games_total", "ars_games_total",
"number of games created", "number of games created",
app_metrics.arc_games_total.clone(), 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()); start_gc(game_expiry.clone(), games.clone());