use crate::metrics::registry::MetricsRegistry;
use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use metrics::set_global_recorder;
use tracing::error;
#[derive(Default)]
pub struct RegistryPlugin {
registry: Option<MetricsRegistry>,
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, SystemSet)]
pub struct ClearBucketsSystem;
impl RegistryPlugin {
pub fn new() -> Self {
Self::default()
}
pub fn with_registry(registry: MetricsRegistry) -> Self {
Self {
registry: Some(registry),
}
}
}
impl Plugin for RegistryPlugin {
fn build(&self, app: &mut App) {
let registry = if let Some(registry) = &self.registry {
_ = set_global_recorder(registry.clone());
registry.clone()
} else {
let registry = MetricsRegistry::default();
if let Err(e) = set_global_recorder(registry.clone()) {
error!("Failed to set global recorder: {e}");
}
registry
};
app.insert_resource(registry).add_systems(
Last,
MetricsRegistry::clear_atomic_buckets_system.in_set(ClearBucketsSystem),
);
}
}