use std::time::Duration;
#[derive(Debug, Clone)]
pub struct MetricsSettings {
pub enabled: bool,
pub http_port: Option<u16>,
pub metrics_path: String,
pub health_path: String,
pub update_interval: Duration,
pub namespace: String,
}
impl Default for MetricsSettings {
fn default() -> Self {
Self {
enabled: true,
http_port: None,
metrics_path: "/metrics".to_owned(),
health_path: "/health".to_owned(),
update_interval: Duration::from_secs(1),
namespace: String::new(),
}
}
}
impl MetricsSettings {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_http_port(mut self, port: u16) -> Self {
self.http_port = Some(port);
self
}
#[must_use]
pub fn with_metrics_path(mut self, path: impl Into<String>) -> Self {
self.metrics_path = path.into();
self
}
#[must_use]
pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
self.namespace = namespace.into();
self
}
#[must_use]
pub fn with_update_interval(mut self, interval: Duration) -> Self {
self.update_interval = interval;
self
}
#[must_use]
pub fn disabled(mut self) -> Self {
self.enabled = false;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_settings() {
let settings = MetricsSettings::default();
assert!(settings.enabled);
assert_eq!(settings.metrics_path, "/metrics");
assert_eq!(settings.namespace, "");
}
#[test]
fn test_builder() {
let settings = MetricsSettings::new()
.with_http_port(9090)
.with_namespace("myapp");
assert_eq!(settings.http_port, Some(9090));
assert_eq!(settings.namespace, "myapp");
}
}