rkubectl_features/
lib.rs

1use prometheus_parse::Sample;
2use prometheus_parse::Scrape;
3use prometheus_parse::Value;
4
5#[derive(Debug, serde::Serialize, tabled::Tabled)]
6#[tabled(rename_all = "UPPERCASE")]
7pub struct Feature {
8    pub name: String,
9    pub stage: String,
10    pub enabled: bool,
11}
12
13impl Feature {
14    pub const KUBERNETES_FEATURES: &str = "kubernetes_feature_enabled";
15
16    pub fn from_scrape(scrape: Scrape) -> Vec<Self> {
17        scrape
18            .samples
19            .into_iter()
20            .filter(|sample| sample.metric == Self::KUBERNETES_FEATURES)
21            .filter_map(Self::from_sample)
22            .collect::<Vec<_>>()
23    }
24
25    pub fn from_sample(sample: Sample) -> Option<Self> {
26        let name = sample.labels.get("name")?.to_string();
27        let stage = sample.labels.get("stage")?.to_string();
28        let enabled = sample.value == Value::Gauge(1.0);
29        Some(Self {
30            name,
31            stage,
32            enabled,
33        })
34    }
35}