add active players and rooms gauges

This commit is contained in:
Nikita Vilunov 2023-02-12 23:23:52 +01:00
parent bd63732e25
commit ae27c04b0a
3 changed files with 24 additions and 7 deletions

View File

@ -12,6 +12,7 @@ use std::{
sync::{Arc, RwLock},
};
use prometheus::{IntGauge, Registry as MetricsRegistry};
use tokio::{
sync::mpsc::{channel, Receiver, Sender},
task::JoinHandle,
@ -19,6 +20,7 @@ use tokio::{
use crate::{
core::room::{RoomId, RoomRegistry},
prelude::*,
util::table::AnonTable,
};
@ -96,13 +98,20 @@ enum PlayerCommand {
#[derive(Clone)]
pub struct PlayerRegistry(Arc<RwLock<PlayerRegistryInner>>);
impl PlayerRegistry {
pub fn empty(room_registry: RoomRegistry) -> PlayerRegistry {
pub fn empty(
room_registry: RoomRegistry,
metrics: &mut MetricsRegistry,
) -> Result<PlayerRegistry> {
let metric_active_players =
IntGauge::new("chat_players_active", "Number of alive player actors")?;
metrics.register(Box::new(metric_active_players.clone()))?;
let inner = PlayerRegistryInner {
next_id: PlayerId(0),
room_registry,
players: HashMap::new(),
metric_active_players,
};
PlayerRegistry(Arc::new(RwLock::new(inner)))
Ok(PlayerRegistry(Arc::new(RwLock::new(inner))))
}
pub async fn create_player(&mut self) -> (PlayerId, PlayerHandle) {
@ -114,6 +123,7 @@ impl PlayerRegistry {
inner.next_id.0 += 1;
let (handle, fiber) = player.launch(id, inner.room_registry.clone());
inner.players.insert(id, (handle.clone(), fiber));
inner.metric_active_players.inc();
(id, handle)
}
}
@ -123,6 +133,7 @@ struct PlayerRegistryInner {
next_id: PlayerId,
room_registry: RoomRegistry,
players: HashMap<PlayerId, (PlayerHandle, JoinHandle<Player>)>,
metric_active_players: IntGauge,
}
/// Player actor inner state representation.

View File

@ -5,6 +5,7 @@ use std::{
sync::{Arc, RwLock},
};
use prometheus::{IntGauge, Registry as MetricRegistry};
use tokio::sync::mpsc::{channel, Sender};
use crate::{
@ -20,12 +21,16 @@ pub struct RoomId(pub u64);
#[derive(Clone)]
pub struct RoomRegistry(Arc<RwLock<RoomRegistryInner>>);
impl RoomRegistry {
pub fn empty() -> RoomRegistry {
pub fn empty(metrics: &mut MetricRegistry) -> Result<RoomRegistry> {
let metric_active_rooms =
IntGauge::new("chat_rooms_active", "Number of alive room actors")?;
metrics.register(Box::new(metric_active_rooms.clone()))?;
let inner = RoomRegistryInner {
next_room_id: RoomId(0),
rooms: HashMap::new(),
metric_active_rooms,
};
RoomRegistry(Arc::new(RwLock::new(inner)))
Ok(RoomRegistry(Arc::new(RwLock::new(inner))))
}
pub fn create_room(&mut self) -> (RoomId, RoomHandle) {
@ -50,6 +55,7 @@ impl RoomRegistry {
struct RoomRegistryInner {
next_room_id: RoomId,
rooms: HashMap<RoomId, (RoomHandle, JoinHandle<Room>)>,
metric_active_rooms: IntGauge,
}
#[derive(Clone)]

View File

@ -39,9 +39,9 @@ async fn main() -> Result<()> {
telemetry: telemetry_config,
irc: irc_config,
} = config;
let metrics = MetricsRegistry::new();
let rooms = RoomRegistry::empty();
let players = PlayerRegistry::empty(rooms.clone());
let mut metrics = MetricsRegistry::new();
let rooms = RoomRegistry::empty(&mut metrics)?;
let players = PlayerRegistry::empty(rooms.clone(), &mut metrics)?;
let telemetry_terminator = util::telemetry::launch(telemetry_config, metrics.clone()).await?;
let irc = projections::irc::launch(irc_config, players, metrics.clone()).await?;
tracing::info!("Started");