Skip to content

Commit

Permalink
Merge pull request #26 from cardano-scaling/pi/new-metrics
Browse files Browse the repository at this point in the history
Add new metrics that can be hit by the drone server
  • Loading branch information
Quantumplation authored Nov 12, 2024
2 parents b84e275 + 4314869 commit 47a0190
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 12 deletions.
35 changes: 32 additions & 3 deletions 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::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>>) {
metrics.end_game();
}

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

#[post("/player_left")]
fn player_left(metrics: &State<Arc<Metrics>>) {
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 @@ -112,8 +142,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 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 47a0190

Please sign in to comment.