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

V0.2.0 #8

Merged
merged 4 commits into from
Sep 29, 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
4 changes: 3 additions & 1 deletion src/custom_include/include_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ impl<'a> IncludeProcessor<'a> {

fn json_object_to_metric(&self, include_selector: &str, json_object: Value) -> Result<Vec<PromMetric>, CustomIncludeError> {
let mut metrics = vec![];
let mut labels = self.labels(include_selector)?;

if self.config.has_gauge_values() {
for gauge_field_value in self.config.gauge_field_values.as_ref().unwrap() {
let mut labels = self.labels(include_selector)?;
labels.push(
PromLabel::new(self.config.gauge_field.to_string(), gauge_field_value.to_string())
);
Expand All @@ -57,6 +57,8 @@ impl<'a> IncludeProcessor<'a> {
}
} else {
if let Some(json_value) = json_object.get(self.config.gauge_field.to_string()) {
let labels = self.labels(include_selector)?;

metrics.push(PromMetric::new(
self.include.name.to_string(),
utils::json_value_to_i64(json_value),
Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Opts {
entry_point: Option<String>
}

async fn fetch_json(json_endpoint: String) -> Result<String, Box<dyn std::error::Error>> {
async fn fetch_json(json_endpoint: String) -> Result<String, reqwest::Error> {
let res = reqwest::get(json_endpoint).await?;
let body = res.text().await?;
Ok(body)
Expand Down Expand Up @@ -63,7 +63,12 @@ async fn metrics() -> status::Custom<content::Plain<String>> {
|metrics| status::Custom(Status::Ok, content::Plain(metrics)))
},
Err(err) => {
status::Custom(Status::InternalServerError, content::Plain(err.to_string()))
if err.is_timeout() || err.is_connect() {
status::Custom(Status::GatewayTimeout, content::Plain(err.to_string()))
}
else {
status::Custom(Status::InternalServerError, content::Plain(err.to_string()))
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,15 @@ includes:
assert!(metrics[0].labels.as_ref().unwrap().iter().find(|l| l.name == "backend").is_some());
assert!(metrics[1].labels.as_ref().unwrap().iter().find(|l| l.name == "status").is_none());
}

#[test]
fn convert_json_with_custom_include_no_duplicate_status_tags() {
let json_str = json_with_several_components();
let payload = Payload::new(json_str, Some(".components".into()), config_with_custom_includes());
let metrics = payload.json_to_metrics().unwrap();

for metric in metrics {
assert_eq!(metric.labels.as_ref().unwrap().iter().filter(|l| l.name == "status").count(), 1, "metric {} has more than one status label", metric.name);
}
}
}