use anyhow::Context; use serde::{Deserialize, Serialize}; use std::collections::HashMap; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ScrapeTarget { pub name: String, pub addr: String, #[serde(default = "default_metrics_path")] pub metrics_path: String, #[serde(default)] pub labels: HashMap, } fn default_metrics_path() -> String { "/metrics".to_string() } impl ScrapeTarget { pub fn url(&self) -> String { if self.metrics_path.starts_with("http") { self.metrics_path.clone() } else { format!("http://{}{}", self.addr, self.metrics_path) } } } pub async fn load_targets_from_file(path: &str) -> anyhow::Result> { let content = tokio::fs::read_to_string(path) .await .context("read targets file")?; let targets: Vec = serde_json::from_str(&content).with_context(|| format!("parse targets file {path}"))?; Ok(targets) }