From 04244fd87d1b749c270be0f7cdbf40e804f10661 Mon Sep 17 00:00:00 2001 From: fredr Date: Tue, 21 Dec 2021 11:47:14 +0100 Subject: [PATCH 1/3] move targets to its own package --- src/config/mod.rs | 4 ++-- src/lib.rs | 1 + src/metrics/mod.rs | 52 +++++++++------------------------------------- src/targets/mod.rs | 43 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 44 deletions(-) create mode 100644 src/targets/mod.rs diff --git a/src/config/mod.rs b/src/config/mod.rs index b66f4d7..e65da3f 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -108,10 +108,10 @@ pub fn parse(path: String) -> serde_yaml::Result { .targets .iter() .map(|t| match t { - Target::Http { url } => crate::metrics::Target::Http { + Target::Http { url } => crate::targets::Target::Http { url: String::from(url), }, - Target::File { path } => crate::metrics::Target::File { + Target::File { path } => crate::targets::Target::File { path: String::from(path), }, }) diff --git a/src/lib.rs b/src/lib.rs index 8ef3b22..88aec0a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ pub mod config; pub mod metrics; pub mod parsers; pub mod pipeline_stages; +pub mod targets; use lazy_static::lazy_static; use prometheus::core::Collector; diff --git a/src/metrics/mod.rs b/src/metrics/mod.rs index d8ef2f2..d9e3c6d 100644 --- a/src/metrics/mod.rs +++ b/src/metrics/mod.rs @@ -3,9 +3,9 @@ use log::warn; use prometheus::core::Collector; use prometheus::{opts, GaugeVec}; use std::collections::HashMap; -use tokio::io::AsyncReadExt; use crate::parsers; +use crate::targets; #[tokio::main] pub async fn collect(metrics: &[Metric]) -> Vec { @@ -33,32 +33,26 @@ pub async fn collect(metrics: &[Metric]) -> Vec #[derive(Debug)] enum CollectError { - IO(std::io::Error), - Reqwest(reqwest::Error), ParseError(parsers::ParseError), MissingValue(String), -} -impl From for CollectError { - fn from(e: std::io::Error) -> Self { - CollectError::IO(e) - } -} -impl From for CollectError { - fn from(e: reqwest::Error) -> Self { - CollectError::Reqwest(e) - } + TargetError(targets::TargetError), } impl From for CollectError { fn from(e: parsers::ParseError) -> Self { CollectError::ParseError(e) } } +impl From for CollectError { + fn from(e: targets::TargetError) -> Self { + CollectError::TargetError(e) + } +} pub struct MetricBuilder { name: String, help: String, value: Option, - targets: Vec, + targets: Vec, parser: Option>, labels: Vec, pipeline_stages: Vec>, @@ -78,7 +72,7 @@ impl MetricBuilder { pub fn value(&mut self, v: f64) { self.value = Some(v) } - pub fn targets(&mut self, t: Vec) { + pub fn targets(&mut self, t: Vec) { self.targets.extend(t.into_iter()) } pub fn parser(&mut self, p: Box) { @@ -115,7 +109,7 @@ impl MetricBuilder { pub struct Metric { pub name: String, pub value: Option, - pub targets: Vec, + pub targets: Vec, pub parser: Box, pub pipeline_stages: Vec>, pub gauge: GaugeVec, @@ -156,29 +150,3 @@ impl Metric { Ok(self.gauge.collect()) } } - -#[derive(Debug)] -pub enum Target { - Http { url: String }, - File { path: String }, -} - -impl Target { - fn describe(&self) -> &str { - match self { - Self::Http { url } => url, - Self::File { path } => path, - } - } - async fn fetch(&self) -> Result { - match &self { - Self::Http { url } => Ok(reqwest::get(url).await?.text().await?), - Self::File { path } => { - let mut file = tokio::fs::File::open(path).await?; - let mut buffer = String::new(); - file.read_to_string(&mut buffer).await?; - Ok(buffer) - } - } - } -} diff --git a/src/targets/mod.rs b/src/targets/mod.rs new file mode 100644 index 0000000..41466e4 --- /dev/null +++ b/src/targets/mod.rs @@ -0,0 +1,43 @@ +use tokio::io::AsyncReadExt; + +#[derive(Debug)] +pub enum TargetError { + HTTP(reqwest::Error), + IO(std::io::Error), +} +impl From for TargetError { + fn from(e: std::io::Error) -> Self { + TargetError::IO(e) + } +} +impl From for TargetError { + fn from(e: reqwest::Error) -> Self { + TargetError::HTTP(e) + } +} + +#[derive(Debug)] +pub enum Target { + Http { url: String }, + File { path: String }, +} + +impl Target { + pub fn describe(&self) -> &str { + match self { + Self::Http { url } => url, + Self::File { path } => path, + } + } + pub async fn fetch(&self) -> Result { + match &self { + Self::Http { url } => Ok(reqwest::get(url).await?.text().await?), + Self::File { path } => { + let mut file = tokio::fs::File::open(path).await?; + let mut buffer = String::new(); + file.read_to_string(&mut buffer).await?; + Ok(buffer) + } + } + } +} From bddc9530f0f110df872373ea464b48d4a3a4335b Mon Sep 17 00:00:00 2001 From: fredr Date: Tue, 21 Dec 2021 12:00:35 +0100 Subject: [PATCH 2/3] move http fetch code to sub module --- src/config/mod.rs | 8 +++++--- src/targets/http.rs | 10 ++++++++++ src/targets/mod.rs | 7 ++++--- 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 src/targets/http.rs diff --git a/src/config/mod.rs b/src/config/mod.rs index e65da3f..a08b8f1 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -108,9 +108,11 @@ pub fn parse(path: String) -> serde_yaml::Result { .targets .iter() .map(|t| match t { - Target::Http { url } => crate::targets::Target::Http { - url: String::from(url), - }, + Target::Http { url } => { + crate::targets::Target::Http(crate::targets::http::Config { + url: String::from(url), + }) + } Target::File { path } => crate::targets::Target::File { path: String::from(path), }, diff --git a/src/targets/http.rs b/src/targets/http.rs new file mode 100644 index 0000000..849759f --- /dev/null +++ b/src/targets/http.rs @@ -0,0 +1,10 @@ +#[derive(Debug)] +pub struct Config { + pub url: String, +} + +impl Config { + pub async fn fetch(&self) -> reqwest::Result { + Ok(reqwest::get(&self.url).await?.text().await?) + } +} diff --git a/src/targets/mod.rs b/src/targets/mod.rs index 41466e4..cbddfe6 100644 --- a/src/targets/mod.rs +++ b/src/targets/mod.rs @@ -1,4 +1,5 @@ use tokio::io::AsyncReadExt; +pub mod http; #[derive(Debug)] pub enum TargetError { @@ -18,20 +19,20 @@ impl From for TargetError { #[derive(Debug)] pub enum Target { - Http { url: String }, + Http(http::Config), File { path: String }, } impl Target { pub fn describe(&self) -> &str { match self { - Self::Http { url } => url, + Self::Http(http::Config { url }) => url, Self::File { path } => path, } } pub async fn fetch(&self) -> Result { match &self { - Self::Http { url } => Ok(reqwest::get(url).await?.text().await?), + Self::Http(config) => Ok(config.fetch().await?), Self::File { path } => { let mut file = tokio::fs::File::open(path).await?; let mut buffer = String::new(); From fa809e6e76c0661727269072cdcdd11b40190778 Mon Sep 17 00:00:00 2001 From: fredr Date: Tue, 21 Dec 2021 12:15:02 +0100 Subject: [PATCH 3/3] set user agent --- src/targets/http.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/targets/http.rs b/src/targets/http.rs index 849759f..dba240d 100644 --- a/src/targets/http.rs +++ b/src/targets/http.rs @@ -1,3 +1,6 @@ +const VERSION: &str = env!("CARGO_PKG_VERSION"); +const NAME: &str = env!("CARGO_PKG_NAME"); + #[derive(Debug)] pub struct Config { pub url: String, @@ -5,6 +8,12 @@ pub struct Config { impl Config { pub async fn fetch(&self) -> reqwest::Result { - Ok(reqwest::get(&self.url).await?.text().await?) + let client = reqwest::Client::new(); + let req = client + .request(reqwest::Method::GET, &self.url) + .header(reqwest::header::USER_AGENT, format!("{}/{}", NAME, VERSION)); + let resp = req.send().await?; + + Ok(resp.text().await?) } }