prometheus
This commit is contained in:
parent
d4d0f21f7d
commit
7733e908cd
3 changed files with 49 additions and 24 deletions
52
src/game.rs
52
src/game.rs
|
|
@ -271,23 +271,23 @@ pub struct GameLabels {
|
|||
game: String,
|
||||
}
|
||||
|
||||
pub struct GameCollector {
|
||||
pub struct RunningGamesCollector {
|
||||
games: Arc<RwLock<HashMap<GameId, Game>>>,
|
||||
}
|
||||
|
||||
impl GameCollector {
|
||||
impl RunningGamesCollector {
|
||||
pub fn new(games: Arc<RwLock<HashMap<GameId, Game>>>) -> 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<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();
|
||||
|
||||
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",
|
||||
|
|
|
|||
13
src/main.rs
13
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());
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue