Skip to content

Commit

Permalink
Add a game state metric
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantumplation committed Nov 12, 2024
1 parent 9036363 commit 556c989
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
17 changes: 16 additions & 1 deletion crates/rpc/src/bin/metric_exporter/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,33 @@ async fn main() -> Result<()> {

let _ = rocket::build()
.manage(metrics)
.mount("/", routes![metrics_endpoint])
.mount("/", routes![
metrics_endpoint,
start_server,
start_game,
end_game,
player_joined,
player_left,
player_killed,
player_suicided,
])
.launch()
.await?;

Ok(())
}


#[get("/metrics")]
fn metrics_endpoint(metrics: &State<Arc<Metrics>>) -> String {
metrics.gather()
}

#[get("/start_server")]
fn start_server(metrics: &State<Arc<Metrics>>) {
metrics.start_server();
}

#[post("/start_game")]
fn start_game(metrics: &State<Arc<Metrics>>) {
metrics.start_game();
Expand Down
52 changes: 46 additions & 6 deletions crates/rpc/src/bin/metric_exporter/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,39 @@ pub enum NodeState {
HeadIsOpen,
}

impl Into<i64> for NodeState {
fn into(self) -> i64 {
match self {
NodeState::Offline => 0,
NodeState::Online => 1,
NodeState::HeadIsInitializing => 2,
NodeState::HeadIsOpen => 3,
}
}
}

pub enum GameState {
Waiting,
Lobby,
Running,
Done,
}

impl Into<i64> for GameState {
fn into(self) -> i64 {
match self {
GameState::Waiting => 0,
GameState::Lobby => 1,
GameState::Running => 2,
GameState::Done => 3,
}
}
}

pub struct Metrics {
pub registry: Registry,
pub state: IntGauge,
pub game_state: IntGauge,
pub transactions: IntCounter,
pub bytes: IntCounter,

Expand All @@ -33,6 +63,12 @@ impl Metrics {
)
.unwrap();

let game_state = IntGauge::new(
"hydra_doom_game_state",
"0 for WAITING, 1 for LOBBY, 2 for RUNNING, 3 for DONE",
)
.unwrap();

let transactions = IntCounter::new(
"hydra_doom_node_transactions",
"Number of executed transactions.",
Expand Down Expand Up @@ -86,6 +122,7 @@ impl Metrics {

let registry = Registry::default();
registry.register(Box::new(state.clone()))?;
registry.register(Box::new(game_state.clone()))?;
registry.register(Box::new(transactions.clone()))?;
registry.register(Box::new(bytes.clone()))?;
registry.register(Box::new(games_current.clone()))?;
Expand All @@ -98,6 +135,7 @@ impl Metrics {
Ok(Self {
registry,
state,
game_state,
transactions,
bytes,
games_current,
Expand All @@ -112,27 +150,28 @@ impl Metrics {
}

pub fn set_state(&self, state: NodeState) {
self.state.set(match state {
NodeState::Offline => 0,
NodeState::Online => 1,
NodeState::HeadIsInitializing => 2,
NodeState::HeadIsOpen => 3,
})
self.state.set(state.into())
}

pub fn new_transaction(&self, bytes: u64) {
self.transactions.inc();
self.bytes.inc_by(bytes);
}

pub fn start_server(&self) {
self.game_state.set(GameState::Waiting.into());
}

pub fn start_game(&self) {
self.games_current.inc();
self.game_state.set(GameState::Running.into());
let mut guard = self.game_timer.lock().unwrap();
*guard = Some(self.games_seconds.start_timer());
}

pub fn end_game(&self) {
self.games_current.dec();
self.game_state.set(GameState::Done.into());
let mut guard = self.game_timer.lock().unwrap();
if let Some(timer) = guard.take() {
timer.observe_duration();
Expand All @@ -142,6 +181,7 @@ impl Metrics {
pub fn player_joined(&self) {
self.players_total.inc();
self.players_current.inc();
self.game_state.set(GameState::Lobby.into());
}

pub fn player_left(&self) {
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/src/model/cluster/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{anyhow, Context, Result};
use hex::FromHex;
use pallas::{
crypto::key::ed25519::SecretKey,
ledger::addresses::{Address, Network, PaymentKeyHash},
ledger::addresses::{Address, Network},
};
use reqwest::Url;
use serde::{Deserialize, Serialize};
Expand Down
1 change: 0 additions & 1 deletion crates/rpc/src/model/game/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use pallas::ledger::primitives::conway::NativeScript;
use pallas::ledger::traverse::ComputeHash;

use super::contract::game_state::PaymentCredential;
use super::contract::validator::Validator;

pub struct Player {
pub signing_key: Hash<28>,
Expand Down

0 comments on commit 556c989

Please sign in to comment.