use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum LogFormat {
#[default]
Pretty,
Json,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObservabilityConfig {
pub service_name: String,
pub tracing_enabled: bool,
pub metrics_enabled: bool,
pub log_format: LogFormat,
#[cfg(feature = "otel")]
pub otlp_endpoint: Option<String>,
#[cfg(feature = "prometheus")]
pub prometheus_port: Option<u16>,
}
impl Default for ObservabilityConfig {
fn default() -> Self {
Self {
service_name: "continuum".to_string(),
tracing_enabled: true,
metrics_enabled: true,
log_format: LogFormat::default(),
#[cfg(feature = "otel")]
otlp_endpoint: None,
#[cfg(feature = "prometheus")]
prometheus_port: None,
}
}
}
impl ObservabilityConfig {
pub fn new(service_name: impl Into<String>) -> Self {
Self {
service_name: service_name.into(),
..Default::default()
}
}
pub fn with_log_format(mut self, format: LogFormat) -> Self {
self.log_format = format;
self
}
pub fn without_tracing(mut self) -> Self {
self.tracing_enabled = false;
self
}
pub fn without_metrics(mut self) -> Self {
self.metrics_enabled = false;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = ObservabilityConfig::default();
assert_eq!(config.service_name, "continuum");
assert!(config.tracing_enabled);
assert!(config.metrics_enabled);
assert_eq!(config.log_format, LogFormat::Pretty);
}
#[test]
fn test_config_builder() {
let config = ObservabilityConfig::new("my-service")
.with_log_format(LogFormat::Json)
.without_tracing();
assert_eq!(config.service_name, "my-service");
assert!(!config.tracing_enabled);
assert!(config.metrics_enabled);
assert_eq!(config.log_format, LogFormat::Json);
}
#[test]
fn test_log_format_default() {
assert_eq!(LogFormat::default(), LogFormat::Pretty);
}
}