Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set user agent on http requests #5

Merged
merged 3 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ pub fn parse(path: String) -> serde_yaml::Result<crate::DataMetrics> {
.targets
.iter()
.map(|t| match t {
Target::Http { url } => crate::metrics::Target::Http {
url: String::from(url),
},
Target::File { path } => crate::metrics::Target::File {
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),
},
})
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 10 additions & 42 deletions src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<prometheus::proto::MetricFamily> {
Expand Down Expand Up @@ -33,32 +33,26 @@ pub async fn collect(metrics: &[Metric]) -> Vec<prometheus::proto::MetricFamily>

#[derive(Debug)]
enum CollectError {
IO(std::io::Error),
Reqwest(reqwest::Error),
ParseError(parsers::ParseError),
MissingValue(String),
}
impl From<std::io::Error> for CollectError {
fn from(e: std::io::Error) -> Self {
CollectError::IO(e)
}
}
impl From<reqwest::Error> for CollectError {
fn from(e: reqwest::Error) -> Self {
CollectError::Reqwest(e)
}
TargetError(targets::TargetError),
}
impl From<parsers::ParseError> for CollectError {
fn from(e: parsers::ParseError) -> Self {
CollectError::ParseError(e)
}
}
impl From<targets::TargetError> for CollectError {
fn from(e: targets::TargetError) -> Self {
CollectError::TargetError(e)
}
}

pub struct MetricBuilder {
name: String,
help: String,
value: Option<f64>,
targets: Vec<Target>,
targets: Vec<targets::Target>,
parser: Option<Box<dyn crate::parsers::Parser + Sync + Send>>,
labels: Vec<String>,
pipeline_stages: Vec<Box<dyn crate::pipeline_stages::PipelineStage + Sync + Send>>,
Expand All @@ -78,7 +72,7 @@ impl MetricBuilder {
pub fn value(&mut self, v: f64) {
self.value = Some(v)
}
pub fn targets(&mut self, t: Vec<Target>) {
pub fn targets(&mut self, t: Vec<targets::Target>) {
self.targets.extend(t.into_iter())
}
pub fn parser(&mut self, p: Box<dyn crate::parsers::Parser + Sync + Send>) {
Expand Down Expand Up @@ -115,7 +109,7 @@ impl MetricBuilder {
pub struct Metric {
pub name: String,
pub value: Option<f64>,
pub targets: Vec<Target>,
pub targets: Vec<targets::Target>,
pub parser: Box<dyn crate::parsers::Parser + Sync + Send>,
pub pipeline_stages: Vec<Box<dyn crate::pipeline_stages::PipelineStage + Sync + Send>>,
pub gauge: GaugeVec,
Expand Down Expand Up @@ -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<String, CollectError> {
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)
}
}
}
}
19 changes: 19 additions & 0 deletions src/targets/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const VERSION: &str = env!("CARGO_PKG_VERSION");
const NAME: &str = env!("CARGO_PKG_NAME");

#[derive(Debug)]
pub struct Config {
pub url: String,
}

impl Config {
pub async fn fetch(&self) -> reqwest::Result<String> {
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?)
}
}
44 changes: 44 additions & 0 deletions src/targets/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use tokio::io::AsyncReadExt;
pub mod http;

#[derive(Debug)]
pub enum TargetError {
HTTP(reqwest::Error),
IO(std::io::Error),
}
impl From<std::io::Error> for TargetError {
fn from(e: std::io::Error) -> Self {
TargetError::IO(e)
}
}
impl From<reqwest::Error> for TargetError {
fn from(e: reqwest::Error) -> Self {
TargetError::HTTP(e)
}
}

#[derive(Debug)]
pub enum Target {
Http(http::Config),
File { path: String },
}

impl Target {
pub fn describe(&self) -> &str {
match self {
Self::Http(http::Config { url }) => url,
Self::File { path } => path,
}
}
pub async fn fetch(&self) -> Result<String, TargetError> {
match &self {
Self::Http(config) => Ok(config.fetch().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)
}
}
}
}