use std::collections::HashMap;
use std::time::Duration;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum KeplerMetricKind {
#[default]
Container,
Process,
}
impl KeplerMetricKind {
#[must_use]
pub const fn metric_name(self) -> &'static str {
match self {
Self::Container => "kepler_container_cpu_joules_total",
Self::Process => "kepler_process_cpu_joules_total",
}
}
#[must_use]
pub const fn label_key(self) -> &'static str {
match self {
Self::Container => "container_name",
Self::Process => "comm",
}
}
}
#[derive(Clone)]
pub struct KeplerConfig {
pub endpoint: String,
pub scrape_interval: Duration,
pub metric_kind: KeplerMetricKind,
pub service_mappings: HashMap<String, String>,
pub auth_header: Option<String>,
}
impl std::fmt::Debug for KeplerConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("KeplerConfig")
.field("endpoint", &self.endpoint)
.field("scrape_interval", &self.scrape_interval)
.field("metric_kind", &self.metric_kind)
.field("service_mappings", &self.service_mappings)
.field(
"auth_header",
&self.auth_header.as_ref().map(|_| "[REDACTED]"),
)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_config() -> KeplerConfig {
let mut mappings = HashMap::new();
mappings.insert("order-svc".to_string(), "order-svc-deployment".to_string());
KeplerConfig {
endpoint: "http://kepler:9102/metrics".to_string(),
scrape_interval: Duration::from_secs(5),
metric_kind: KeplerMetricKind::Container,
service_mappings: mappings,
auth_header: Some("Authorization: Bearer super-secret-do-not-log".to_string()),
}
}
#[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://kepler:9102/metrics"));
assert!(dbg.contains("order-svc"));
assert!(dbg.contains("order-svc-deployment"));
}
#[test]
fn metric_kind_names_and_labels() {
assert_eq!(
KeplerMetricKind::Container.metric_name(),
"kepler_container_cpu_joules_total"
);
assert_eq!(KeplerMetricKind::Container.label_key(), "container_name");
assert_eq!(
KeplerMetricKind::Process.metric_name(),
"kepler_process_cpu_joules_total"
);
assert_eq!(KeplerMetricKind::Process.label_key(), "comm");
}
}