refactor metric tags & sources config
This commit is contained in:
+38
-36
@@ -1,26 +1,28 @@
|
||||
use crate::{
|
||||
ChimemonSource, ChimemonSourceChannel, Config, SourceMetric, SourceReport, SourceReportDetails,
|
||||
SourceStatus,
|
||||
};
|
||||
use std::net::{SocketAddr, ToSocketAddrs};
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use chrony_candm::reply::{self, ReplyBody, SourceMode};
|
||||
use chrony_candm::request::{self, RequestBody};
|
||||
use chrony_candm::{ClientOptions, blocking_query};
|
||||
use std::net::{SocketAddr, ToSocketAddrs};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::join;
|
||||
use tracing::{info, warn};
|
||||
|
||||
use crate::{
|
||||
ChimemonSource, ChimemonSourceChannel, ChronyConfig, MetricTags, SourceMetric, SourceReport,
|
||||
SourceReportDetails, SourceStatus,
|
||||
};
|
||||
|
||||
pub struct ChronyClient {
|
||||
pub server: SocketAddr,
|
||||
pub name: String,
|
||||
client_options: ClientOptions,
|
||||
config: Config,
|
||||
config: ChronyConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ChronyTrackingReport {
|
||||
tags: Arc<Vec<(String, String)>>,
|
||||
tags: Arc<MetricTags>,
|
||||
pub ref_id: i64,
|
||||
pub ref_ip_addr: String,
|
||||
pub stratum: i64,
|
||||
@@ -78,9 +80,9 @@ impl SourceReportDetails for ChronySourcesReport {
|
||||
|
||||
for source in &self.sources {
|
||||
let tags = Arc::new(vec![
|
||||
("ref_id".to_owned(), source.ip_addr.to_string()),
|
||||
("ref_id", source.ip_addr.to_string()),
|
||||
(
|
||||
"mode".to_owned(),
|
||||
"mode",
|
||||
match source.mode {
|
||||
SourceMode::Client => String::from("server"),
|
||||
SourceMode::Peer => String::from("peer"),
|
||||
@@ -88,7 +90,7 @@ impl SourceReportDetails for ChronySourcesReport {
|
||||
},
|
||||
),
|
||||
(
|
||||
"state".to_owned(),
|
||||
"state",
|
||||
match source.state {
|
||||
reply::SourceState::Selected => String::from("best"),
|
||||
reply::SourceState::NonSelectable => String::from("unusable"),
|
||||
@@ -129,7 +131,7 @@ impl SourceReportDetails for ChronySourcesReport {
|
||||
|
||||
fn report_from_tracking(
|
||||
t: &reply::Tracking,
|
||||
config: &Config,
|
||||
config: &ChronyConfig,
|
||||
) -> Result<ChronyTrackingReport, Box<dyn std::error::Error>> {
|
||||
let report = ChronyTrackingReport {
|
||||
tags: Arc::new(vec![]), //TODO: allow configuring tags in the source
|
||||
@@ -151,25 +153,6 @@ fn report_from_tracking(
|
||||
}
|
||||
|
||||
impl ChronyClient {
|
||||
pub fn new(config: Config) -> Self {
|
||||
let server = config
|
||||
.sources
|
||||
.chrony
|
||||
.host
|
||||
.to_socket_addrs()
|
||||
.unwrap()
|
||||
.next()
|
||||
.expect("Unable to parse host:port:");
|
||||
let client_options = ClientOptions {
|
||||
n_tries: 3,
|
||||
timeout: config.sources.chrony.timeout,
|
||||
};
|
||||
ChronyClient {
|
||||
server,
|
||||
client_options,
|
||||
config,
|
||||
}
|
||||
}
|
||||
async fn query(&self, request: RequestBody) -> Result<reply::Reply, std::io::Error> {
|
||||
let server = self.server;
|
||||
let client_options = self.client_options;
|
||||
@@ -265,7 +248,7 @@ impl ChronyClient {
|
||||
|
||||
let tracking_data = report_from_tracking(&tracking, &self.config)?;
|
||||
let report = SourceReport {
|
||||
name: "chrony-tracking".to_owned(),
|
||||
name: self.name.clone(),
|
||||
status: SourceStatus::Unknown,
|
||||
details: Arc::new(tracking_data),
|
||||
};
|
||||
@@ -283,7 +266,7 @@ impl ChronyClient {
|
||||
let sources = self.get_sources().await?;
|
||||
let details = ChronySourcesReport { sources };
|
||||
let report = SourceReport {
|
||||
name: "chrony-sources".to_owned(),
|
||||
name: self.name.clone(),
|
||||
status: SourceStatus::Unknown,
|
||||
details: Arc::new(details),
|
||||
};
|
||||
@@ -295,11 +278,30 @@ impl ChronyClient {
|
||||
|
||||
#[async_trait]
|
||||
impl ChimemonSource for ChronyClient {
|
||||
type Config = ChronyConfig;
|
||||
fn new(name: &str, config: Self::Config) -> Self {
|
||||
let server = config
|
||||
.host
|
||||
.to_socket_addrs()
|
||||
.unwrap()
|
||||
.next()
|
||||
.expect("Unable to parse host:port:");
|
||||
let client_options = ClientOptions {
|
||||
n_tries: 3,
|
||||
timeout: config.timeout,
|
||||
};
|
||||
ChronyClient {
|
||||
name: name.to_owned(),
|
||||
server,
|
||||
client_options,
|
||||
config,
|
||||
}
|
||||
}
|
||||
async fn run(self, chan: ChimemonSourceChannel) {
|
||||
info!("Chrony task started");
|
||||
|
||||
let mut t_interval = tokio::time::interval(self.config.sources.chrony.tracking_interval);
|
||||
let mut s_interval = tokio::time::interval(self.config.sources.chrony.sources_interval);
|
||||
let mut t_interval = tokio::time::interval(self.config.tracking_interval);
|
||||
let mut s_interval = tokio::time::interval(self.config.sources_interval);
|
||||
|
||||
let t_future = async {
|
||||
let lchan = chan.clone();
|
||||
|
||||
Reference in New Issue
Block a user