use std::collections::HashMap;
use std::time::Duration;
pub const DEFAULT_ENERGY_INTERVAL_SECS: f64 = 1.0;
#[derive(Clone)]
pub struct AlumetConfig {
pub endpoint: String,
pub scrape_interval: Duration,
pub metric_name: String,
pub label_key: String,
pub energy_interval_secs: f64,
pub service_mappings: HashMap<String, String>,
pub auth_header: Option<String>,
pub database: Option<AlumetDatabaseConfig>,
pub broker: Option<AlumetBrokerConfig>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AlumetBrokerConfig {
pub label_value: String,
pub region: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AlumetDatabaseConfig {
pub label_value: String,
pub region: Option<String>,
}
impl std::fmt::Debug for AlumetConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AlumetConfig")
.field("endpoint", &self.endpoint)
.field("scrape_interval", &self.scrape_interval)
.field("metric_name", &self.metric_name)
.field("label_key", &self.label_key)
.field("energy_interval_secs", &self.energy_interval_secs)
.field("service_mappings", &self.service_mappings)
.field(
"auth_header",
&self.auth_header.as_ref().map(|_| "[REDACTED]"),
)
.field("database", &self.database)
.field("broker", &self.broker)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_config() -> AlumetConfig {
let mut mappings = HashMap::new();
mappings.insert("checkout".to_string(), "checkout-pod".to_string());
AlumetConfig {
endpoint: "http://localhost:9091/metrics".to_string(),
scrape_interval: Duration::from_secs(5),
metric_name: "attributed_energy_cpu_alumet".to_string(),
label_key: "name".to_string(),
energy_interval_secs: DEFAULT_ENERGY_INTERVAL_SECS,
service_mappings: mappings,
auth_header: Some("Authorization: Bearer super-secret-do-not-log".to_string()),
database: None,
broker: None,
}
}
#[test]
fn debug_impl_redacts_auth_header() {
let cfg = sample_config();
crate::test_helpers::assert_debug_redacts_secret!(&cfg, "super-secret-do-not-log");
}
#[test]
fn debug_impl_preserves_non_secret_fields() {
let cfg = sample_config();
let dbg = format!("{cfg:?}");
assert!(dbg.contains("endpoint"));
assert!(dbg.contains("http://localhost:9091/metrics"));
assert!(dbg.contains("attributed_energy_cpu_alumet"));
assert!(dbg.contains("checkout-pod"));
}
#[test]
fn default_energy_interval_matches_alumet_rapl_poll_default() {
assert!((DEFAULT_ENERGY_INTERVAL_SECS - 1.0).abs() < f64::EPSILON);
}
}