use super::hook::{MetricsHook, NoOpMetricsHook};
use super::registry::{MetricsConfig, MetricsRegistry};
use super::system::MetricsSystem;
use crate::Plugin;
use std::sync::Arc;
#[derive(Plugin)]
#[plugin(name = "issun:metrics")]
pub struct MetricsPlugin {
#[plugin(skip)]
hook: Arc<dyn MetricsHook>,
#[plugin(resource)]
config: MetricsConfig,
#[plugin(runtime_state)]
registry: MetricsRegistry,
#[plugin(system)]
system: MetricsSystem,
}
impl MetricsPlugin {
pub fn new() -> Self {
let hook = Arc::new(NoOpMetricsHook);
let config = MetricsConfig::default();
Self {
hook: hook.clone(),
config: config.clone(),
registry: MetricsRegistry::with_config(config),
system: MetricsSystem::new(hook),
}
}
pub fn with_hook<H: MetricsHook + 'static>(mut self, hook: H) -> Self {
let hook = Arc::new(hook);
self.hook = hook.clone();
self.system = MetricsSystem::new(hook);
self
}
pub fn with_config(mut self, config: MetricsConfig) -> Self {
self.config = config.clone();
self.registry = MetricsRegistry::with_config(config);
self
}
}
impl Default for MetricsPlugin {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_plugin_creation() {
let _plugin = MetricsPlugin::new();
}
#[test]
fn test_plugin_with_custom_hook() {
struct CustomHook;
#[async_trait::async_trait]
impl MetricsHook for CustomHook {}
let _plugin = MetricsPlugin::new().with_hook(CustomHook);
}
#[test]
fn test_plugin_with_custom_config() {
let config = MetricsConfig {
max_values_per_metric: 5000,
enable_periodic_snapshots: true,
..Default::default()
};
let _plugin = MetricsPlugin::new().with_config(config);
}
}