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

View File

@ -5,6 +5,7 @@ use std::{
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
use prometheus::{IntGauge, Registry as MetricRegistry};
use tokio::sync::mpsc::{channel, Sender}; use tokio::sync::mpsc::{channel, Sender};
use crate::{ use crate::{
@ -20,12 +21,16 @@ pub struct RoomId(pub u64);
#[derive(Clone)] #[derive(Clone)]
pub struct RoomRegistry(Arc<RwLock<RoomRegistryInner>>); pub struct RoomRegistry(Arc<RwLock<RoomRegistryInner>>);
impl RoomRegistry { 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 { let inner = RoomRegistryInner {
next_room_id: RoomId(0), next_room_id: RoomId(0),
rooms: HashMap::new(), 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) { pub fn create_room(&mut self) -> (RoomId, RoomHandle) {
@ -50,6 +55,7 @@ impl RoomRegistry {
struct RoomRegistryInner { struct RoomRegistryInner {
next_room_id: RoomId, next_room_id: RoomId,
rooms: HashMap<RoomId, (RoomHandle, JoinHandle<Room>)>, rooms: HashMap<RoomId, (RoomHandle, JoinHandle<Room>)>,
metric_active_rooms: IntGauge,
} }
#[derive(Clone)] #[derive(Clone)]

View File

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