Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into refactor/monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantumplation committed Nov 12, 2024
2 parents ad02f21 + 47a0190 commit ea805fd
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 12 deletions.
35 changes: 32 additions & 3 deletions crates/rpc/src/bin/metric_exporter/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hydra_control_plane_rpc::model::{
hydra_socket::HydraSocket,
},
};
use rocket::{get, routes, State};
use rocket::{get, post, routes, State};
use std::{sync::Arc, time::Duration};
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
use tracing::{info, warn};
Expand Down Expand Up @@ -71,6 +71,36 @@ fn metrics_endpoint(metrics: &State<Arc<Metrics>>) -> String {
metrics.gather()
}

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

#[post("/end_game")]
fn end_game(metrics: &State<Arc<Metrics>>) {

Check warning on line 80 in crates/rpc/src/bin/metric_exporter/main.rs

View workflow job for this annotation

GitHub Actions / build (Linux-x86_64, ubuntu-latest, x86_64-unknown-linux-gnu, --locked --release)

function `end_game` is never used
metrics.end_game();
}

#[post("/player_joined")]
fn player_joined(metrics: &State<Arc<Metrics>>) {

Check warning on line 85 in crates/rpc/src/bin/metric_exporter/main.rs

View workflow job for this annotation

GitHub Actions / build (Linux-x86_64, ubuntu-latest, x86_64-unknown-linux-gnu, --locked --release)

function `player_joined` is never used
metrics.player_joined();
}

#[post("/player_left")]
fn player_left(metrics: &State<Arc<Metrics>>) {

Check warning on line 90 in crates/rpc/src/bin/metric_exporter/main.rs

View workflow job for this annotation

GitHub Actions / build (Linux-x86_64, ubuntu-latest, x86_64-unknown-linux-gnu, --locked --release)

function `player_left` is never used
metrics.player_left();
}

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

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

async fn update_connection_state(metrics: Arc<Metrics>, socket: Arc<HydraSocket>) {
loop {
tokio::time::sleep(Duration::from_secs(10)).await;
Expand Down Expand Up @@ -111,8 +141,7 @@ async fn update(metrics: Arc<Metrics>, mut rx: UnboundedReceiver<HydraData>) {
};
}
HydraEventMessage::TxValid(valid) => {
metrics.inc_transactions();
metrics.inc_bytes(valid.cbor.len() as u64);
metrics.new_transaction(valid.cbor.len() as u64);
}
_ => {}
},
Expand Down
107 changes: 98 additions & 9 deletions crates/rpc/src/bin/metric_exporter/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use prometheus::{Encoder, IntCounter, IntGauge, Registry, TextEncoder};
use std::sync::Mutex;

use prometheus::{histogram_opts, linear_buckets, Encoder, Histogram, HistogramTimer, IntCounter, IntGauge, Registry, TextEncoder};

pub enum NodeState {
Offline,
Expand All @@ -7,15 +9,20 @@ pub enum NodeState {
HeadIsOpen,
}

#[derive(Clone)]
pub struct Metrics {
pub registry: Registry,
pub state: IntGauge,
pub transactions: IntCounter,
pub bytes: IntCounter,
// pub kills: IntCounterVec,
// pub items: IntCounterVec,
// pub secrets: IntCounterVec,

pub games_current: IntGauge,
pub games_seconds: Histogram,
pub players_total: IntCounter,
pub players_current: IntGauge,
pub kills: IntCounter,
pub suicides: IntCounter,

game_timer: Mutex<Option<HistogramTimer>>,
}

impl Metrics {
Expand All @@ -38,15 +45,69 @@ impl Metrics {
)
.unwrap();

let games_current = IntGauge::new(
"hydra_doom_games_current",
"Number of games currently running.",
)
.unwrap();

let games_seconds = Histogram::with_opts(
histogram_opts!(
"hydra_doom_games_seconds",
"Duration of games in seconds.",
linear_buckets(0.0, 60.0, 20)?,
)
)
.unwrap();

let players_total = IntCounter::new(
"hydra_doom_players_total",
"Total number of players that have joined the game.",
)
.unwrap();

let players_current = IntGauge::new(
"hydra_doom_players_current",
"Number of players currently in the game.",
)
.unwrap();

let kills = IntCounter::new(
"hydra_doom_kills",
"Number of kills in the game.",
)
.unwrap();

let suicides = IntCounter::new(
"hydra_doom_suicides",
"Number of suicides in the game.",
)
.unwrap();

let registry = Registry::default();
registry.register(Box::new(state.clone()))?;
registry.register(Box::new(transactions.clone()))?;
registry.register(Box::new(bytes.clone()))?;
registry.register(Box::new(games_current.clone()))?;
registry.register(Box::new(games_seconds.clone()))?;
registry.register(Box::new(players_total.clone()))?;
registry.register(Box::new(players_current.clone()))?;
registry.register(Box::new(kills.clone()))?;
registry.register(Box::new(suicides.clone()))?;

Ok(Self {
registry,
state,
transactions,
bytes,
games_current,
games_seconds,
players_total,
players_current,
kills,
suicides,

game_timer: Mutex::new(None),
})
}

Expand All @@ -59,12 +120,40 @@ impl Metrics {
})
}

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

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

pub fn end_game(&self) {
self.games_current.dec();
let mut guard = self.game_timer.lock().unwrap();
if let Some(timer) = guard.take() {
timer.observe_duration();
}
}

pub fn player_joined(&self) {
self.players_total.inc();
self.players_current.inc();
}

pub fn player_left(&self) {
self.players_current.dec();
}

pub fn player_killed(&self) {
self.kills.inc();
}

pub fn inc_bytes(&self, bytes: u64) {
self.bytes.inc_by(bytes)
pub fn player_suicided(&self) {
self.suicides.inc();
}

pub fn gather(&self) -> String {
Expand Down

0 comments on commit ea805fd

Please sign in to comment.