refactor metric tags & sources config

This commit is contained in:
2026-02-04 02:04:51 -08:00
parent d464cf8ee6
commit 156df9ae86
8 changed files with 326 additions and 302 deletions

View File

@@ -4,7 +4,7 @@ pub mod targets;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use figment::{
Figment,
Figment, Provider,
providers::{Format, Serialized, Toml},
util::map,
value::Map,
@@ -43,6 +43,7 @@ impl Default for InfluxConfig {
#[serde_as]
#[derive(Serialize, Deserialize, Clone)]
#[serde(default)]
pub struct ChronyConfig {
pub enabled: bool,
#[serde_as(as = "DurationSeconds<u64>")]
@@ -73,6 +74,7 @@ impl Default for ChronyConfig {
}
#[derive(Serialize, Deserialize, Clone)]
#[serde(default)]
pub struct ChronySockConfig {
pub enabled: bool,
pub sock: String,
@@ -95,6 +97,7 @@ pub struct HwmonSensorConfig {
#[serde_as]
#[derive(Serialize, Deserialize, Clone)]
#[serde(default)]
pub struct HwmonConfig {
pub enabled: bool,
#[serde_as(as = "DurationSeconds<u64>")]
@@ -116,6 +119,7 @@ impl Default for HwmonConfig {
#[serde_as]
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(default)]
pub struct GpsdConfig {
pub enabled: bool,
#[serde_as(as = "DurationSeconds<u64>")]
@@ -135,6 +139,7 @@ impl Default for GpsdConfig {
#[serde_as]
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(default)]
pub struct Prs10Config {
pub enabled: bool,
pub port: String,
@@ -178,40 +183,43 @@ pub enum SourceStatus {
Unknown,
}
#[derive(Clone, Debug)]
#[derive(Copy, Clone, Debug)]
pub enum MetricValue {
Int(i64),
Float(f64),
Bool(bool),
}
type MetricTag = (&'static str, String);
type MetricTags = Vec<MetricTag>;
#[derive(Clone, Debug)]
pub struct SourceMetric {
name: String,
name: &'static str,
value: MetricValue,
tags: Arc<Vec<(String, String)>>,
tags: Arc<MetricTags>,
}
impl SourceMetric {
pub fn new_int(name: &str, value: i64, tags: Arc<Vec<(String, String)>>) -> Self {
pub fn new_int(name: &'static str, value: i64, tags: Arc<MetricTags>) -> Self {
Self {
name: name.to_owned(),
name: name,
value: MetricValue::Int(value),
tags,
}
}
pub fn new_float(name: &str, value: f64, tags: Arc<Vec<(String, String)>>) -> Self {
pub fn new_float(name: &'static str, value: f64, tags: Arc<MetricTags>) -> Self {
Self {
name: name.to_owned(),
name: name,
value: MetricValue::Float(value),
tags,
}
}
pub fn new_bool(name: &str, value: bool, tags: Arc<Vec<(String, String)>>) -> Self {
pub fn new_bool(name: &'static str, value: bool, tags: Arc<MetricTags>) -> Self {
Self {
name: name.to_owned(),
name: name,
value: MetricValue::Bool(value),
tags,
}
@@ -232,6 +240,7 @@ pub struct SourceReport {
#[serde_as]
#[derive(Serialize, Deserialize, Clone)]
#[serde(default)]
pub struct UCCMConfig {
pub enabled: bool,
pub port: String,
@@ -265,6 +274,23 @@ pub struct SourcesConfig {
pub prs10: Prs10Config,
}
#[derive(Serialize, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum SourceConfig {
Chrony(ChronyConfig),
Hwmon(HwmonConfig),
Uccm(UCCMConfig),
Gpsd(GpsdConfig),
Prs10(Prs10Config),
}
#[derive(Serialize, Deserialize, Clone)]
pub struct NamedSourceConfig {
pub name: String,
#[serde(flatten)]
pub source: SourceConfig,
}
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct TargetsConfig {
pub chrony: ChronySockConfig,
@@ -273,10 +299,19 @@ pub struct TargetsConfig {
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct Config {
pub influxdb: InfluxConfig,
pub sources: SourcesConfig,
pub sources: Vec<NamedSourceConfig>,
pub targets: TargetsConfig,
}
impl Provider for Config {
fn metadata(&self) -> figment::Metadata {
figment::Metadata::named("Default config")
}
fn data(&self) -> Result<Map<figment::Profile, figment::value::Dict>, figment::Error> {
Serialized::defaults(Config::default()).data()
}
}
pub fn load_config(filename: &Path) -> Figment {
Figment::from(Serialized::defaults(Config::default())).merge(Toml::file(filename))
}
@@ -317,6 +352,8 @@ pub type ChimemonTargetChannel = Receiver<ChimemonMessage>;
#[async_trait]
pub trait ChimemonSource {
type Config;
fn new(name: &str, config: Self::Config) -> Self;
async fn run(self, chan: ChimemonSourceChannel);
}