This commit is contained in:
TotallyNot 2025-11-03 16:36:45 +01:00
parent e7d6b74aab
commit 35413b563c
Signed by: pyrite
GPG key ID: 7F1BA9170CD35D15
33 changed files with 10238 additions and 1891 deletions

View file

@ -4,7 +4,7 @@ use std::{
sync::{atomic, Mutex, RwLock},
};
use crate::{dto, entity_registry::EntityRegistry, Stages};
use crate::{entity_registry::EntityRegistry, Stages};
#[derive(Default)]
pub struct Histogram<T>
@ -38,7 +38,7 @@ impl Counter {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MetricKey {
entity: Entity,
entity: Option<Entity>,
label: &'static str,
}
@ -50,7 +50,7 @@ pub struct Metrics {
}
impl Metrics {
pub fn record_histogram(&self, entity: Entity, label: &'static str, value: u32) {
pub fn record_histogram(&self, entity: Option<Entity>, label: &'static str, value: u32) {
if self.active {
let key = MetricKey { entity, label };
let r_hist = self.histograms.read().unwrap();
@ -70,7 +70,7 @@ impl Metrics {
}
}
pub fn increment_counter(&self, entity: Entity, label: &'static str, value: u64) {
pub fn increment_counter(&self, entity: Option<Entity>, label: &'static str, value: u64) {
if self.active {
let key = MetricKey { entity, label };
let r_counters = self.counters.read().unwrap();
@ -91,7 +91,12 @@ impl Metrics {
}
}
pub(crate) fn consume_metrics(world: &World) -> (Vec<dto::Counter>, Vec<dto::Histogram>) {
pub(crate) fn consume_metrics(
world: &World,
) -> (
Vec<proxisim_models::dto::metrics::Counter>,
Vec<proxisim_models::dto::metrics::Histogram>,
) {
let metrics = world.resource::<Metrics>();
let entities = world.resource::<EntityRegistry>();
@ -100,8 +105,12 @@ pub(crate) fn consume_metrics(world: &World) -> (Vec<dto::Counter>, Vec<dto::His
.try_write()
.unwrap()
.drain()
.map(|(key, value)| dto::Counter {
entity: entities.0.get(&key.entity).unwrap().clone(),
.map(|(key, value)| proxisim_models::dto::metrics::Counter {
entity: key
.entity
.as_ref()
.map(|e| entities.0.get(e).unwrap().clone())
.unwrap_or(proxisim_models::dto::metrics::EntityInfo::Global),
value: value.inner.load(atomic::Ordering::Relaxed),
label: key.label,
})
@ -112,8 +121,12 @@ pub(crate) fn consume_metrics(world: &World) -> (Vec<dto::Counter>, Vec<dto::His
.try_write()
.unwrap()
.drain()
.map(|(key, value)| dto::Histogram {
entity: entities.0.get(&key.entity).unwrap().clone(),
.map(|(key, value)| proxisim_models::dto::metrics::Histogram {
entity: key
.entity
.as_ref()
.map(|e| entities.0.get(e).unwrap().clone())
.unwrap_or(proxisim_models::dto::metrics::EntityInfo::Global),
values: value.inner.into_inner().unwrap(),
label: key.label,
})