Skip to content

Commit

Permalink
fix: metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Nov 16, 2024
1 parent a0735f3 commit b7dfa3b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub async fn init_config() {
}),
site: Set(crate::config::site::Config {
title: String::from("CdsCTF"),
description: String::from("CdsCTF is a CTF platform."),
description: String::from("Reality is an illusion, the universe is a hologram."),
color: String::from("#0C4497"),
favicon: String::from(""),
}),
Expand Down
2 changes: 1 addition & 1 deletion src/metric/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub static MEMORY_USAGE: Lazy<IntGauge> = Lazy::new(|| {
});

pub static MEMORY_USAGE_RATIO: Lazy<Gauge> = Lazy::new(|| {
let opts = Opts::new("memory_usage_bytes", "Memory usage in bytes")
let opts = Opts::new("memory_usage_ratio", "Memory usage ratio")
.namespace(crate::env::get_env().metric.namespace.clone());
let gauge = Gauge::with_opts(opts).expect("metric can be created");
METRICS_REGISTRY.register(Box::new(gauge.clone())).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/model/challenge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ pub struct Model {
#[sea_orm(default_value = 1800)]
pub duration: i64,
pub ports: Vec<i32>,
#[sea_orm(column_type = "Json")]
#[sea_orm(column_type = "JsonBinary")]
pub envs: Vec<Env>,
#[sea_orm(column_type = "Json")]
#[sea_orm(column_type = "JsonBinary")]
pub flags: Vec<Flag>,
pub created_at: i64,
pub updated_at: i64,
Expand Down
2 changes: 1 addition & 1 deletion src/model/pod/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Model {
pub team_id: Option<i64>,
pub game_id: Option<i64>,
pub challenge_id: i64,
#[sea_orm(column_type = "Json")]
#[sea_orm(column_type = "JsonBinary")]
pub nats: Vec<Nat>,
pub removed_at: i64,
pub created_at: i64,
Expand Down
21 changes: 13 additions & 8 deletions src/web/router/metric/worker.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::time::Duration;

use sysinfo::System;
use sysinfo::{Pid, System};
use tokio::time::interval;
use tracing::info;

use crate::metric::{CPU_USAGE, MEMORY_USAGE};
use crate::metric::{CPU_USAGE, MEMORY_USAGE, MEMORY_USAGE_RATIO};

pub async fn init() {
tokio::spawn(async {
Expand All @@ -14,14 +14,19 @@ pub async fn init() {
loop {
sys.refresh_all();

let total_memory = sys.total_memory();
let used_memory = sys.used_memory();
let memory_usage = (used_memory as f64 / total_memory as f64 * 100.0) as i64;
let pid = std::process::id();
if let Some(process) = sys.process(Pid::from_u32(pid)) {
let used_memory = process.memory();
MEMORY_USAGE.set(used_memory as i64);

MEMORY_USAGE.set(memory_usage);
let total_memory = sys.total_memory();
let memory_usage = used_memory as f64 / total_memory as f64 * 100.0;

let cpu_usage = sys.global_cpu_usage() as i64;
CPU_USAGE.set(cpu_usage);
MEMORY_USAGE_RATIO.set(memory_usage);

let cpu_usage = process.cpu_usage() as i64;
CPU_USAGE.set(cpu_usage);
}

tokio::time::sleep(interval).await;
}
Expand Down

0 comments on commit b7dfa3b

Please sign in to comment.