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
+29 -25
View File
@@ -1,13 +1,15 @@
use crate::{
ChimemonSource, ChimemonSourceChannel, Config, SourceMetric, SourceReport, SourceReportDetails,
SourceStatus,
};
use std::{fs::File, io::Read, path::PathBuf, sync::Arc};
use async_trait::async_trait;
use std::{fs::File, io::Read, path::PathBuf, sync::Arc, time::Duration};
use tracing::{debug, error, info, warn};
use crate::{
ChimemonSource, ChimemonSourceChannel, HwmonConfig, MetricTags, SourceMetric, SourceReport,
SourceReportDetails, SourceStatus,
};
pub struct HwmonSource {
config: Config,
name: String,
config: HwmonConfig,
sensors: Vec<Arc<HwmonSensor>>,
}
@@ -18,7 +20,7 @@ struct HwmonSensor {
device: String,
sensor: String,
label: Option<String>,
tags: Arc<Vec<(String, String)>>,
tags: Arc<MetricTags>,
}
impl HwmonSensor {
@@ -40,12 +42,12 @@ impl HwmonSensor {
None
};
let mut tags_vec = vec![
("name".to_owned(), name.to_owned()),
("device".to_owned(), device.to_owned()),
("sensor".to_owned(), sensor.to_owned()),
("name", name.to_owned()),
("device", device.to_owned()),
("sensor", sensor.to_owned()),
];
if let Some(label) = &label {
tags_vec.push(("label".to_owned(), label.clone()))
tags_vec.push(("label", label.clone()))
}
Self {
value_path,
@@ -92,18 +94,6 @@ impl SourceReportDetails for HwmonReport {
const HWMON_ROOT: &str = "/sys/class/hwmon";
impl HwmonSource {
pub fn new(config: Config) -> Self {
let sensors = config
.sources
.hwmon
.sensors
.iter()
.map(|(k, v)| Arc::new(HwmonSensor::new(k, &v.name, &v.sensor)))
.collect();
HwmonSource { config, sensors }
}
async fn get_raw_value(sensor: &HwmonSensor) -> Result<String, std::io::Error> {
tokio::fs::read_to_string(&sensor.value_path).await
}
@@ -111,9 +101,23 @@ impl HwmonSource {
#[async_trait]
impl ChimemonSource for HwmonSource {
type Config = HwmonConfig;
fn new(name: &str, config: Self::Config) -> Self {
let sensors = config
.sensors
.iter()
.map(|(k, v)| Arc::new(HwmonSensor::new(k, &v.name, &v.sensor)))
.collect();
HwmonSource {
name: name.to_owned(),
config,
sensors,
}
}
async fn run(self, chan: ChimemonSourceChannel) {
info!("hwmon task started");
let mut interval = tokio::time::interval(self.config.sources.hwmon.interval);
let mut interval = tokio::time::interval(self.config.interval);
loop {
interval.tick().await;
let mut values = Vec::new();
@@ -137,7 +141,7 @@ impl ChimemonSource for HwmonSource {
}
}
let report = SourceReport {
name: "hwmon".to_owned(),
name: self.name.clone(),
status: SourceStatus::Healthy,
details: Arc::new(HwmonReport { values }),
};