Skip to content

Commit 9cd387a

Browse files
author
puetzp
committed
Use serde to deserialize targets
1 parent 6748110 commit 9cd387a

File tree

3 files changed

+25
-54
lines changed

3 files changed

+25
-54
lines changed

src/client.rs

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::error::{
44
};
55
use crate::response::*;
66
use crate::selector::Selector;
7-
use crate::util::{validate_duration, TargetHealth, TargetState};
7+
use crate::util::{validate_duration, TargetState};
88
use std::collections::HashMap;
99

1010
/// A helper enum that is passed to the [Client::new] function in
@@ -561,52 +561,9 @@ fn parse_target_response(response: HashMap<String, serde_json::Value>) -> Result
561561

562562
match status {
563563
"success" => {
564-
let data_obj = response["data"].as_object().unwrap();
565-
let mut active = vec![];
566-
let mut dropped = vec![];
567-
568-
for target in data_obj["activeTargets"].as_array().unwrap() {
569-
let target_obj = target.as_object().unwrap();
570-
let discovered_labels =
571-
parse_metric(target_obj["discoveredLabels"].as_object().unwrap());
572-
let labels = parse_metric(target_obj["labels"].as_object().unwrap());
573-
let scrape_pool = target_obj["scrapePool"].as_str().unwrap().to_string();
574-
let scrape_url = target_obj["scrapeUrl"].as_str().unwrap().to_string();
575-
let global_url = target_obj["globalUrl"].as_str().unwrap().to_string();
576-
let last_error = target_obj["lastError"].as_str().unwrap().to_string();
577-
let last_scrape = target_obj["lastScrape"].as_str().unwrap().to_string();
578-
let last_scrape_duration = target_obj["lastScrapeDuration"].as_f64().unwrap();
579-
580-
let health = match target_obj["health"].as_str().unwrap() {
581-
"up" => TargetHealth::Up,
582-
"down" => TargetHealth::Down,
583-
"unknown" => TargetHealth::Unknown,
584-
_ => unreachable!(),
585-
};
586-
587-
active.push(ActiveTarget {
588-
discovered_labels,
589-
labels,
590-
scrape_pool,
591-
scrape_url,
592-
global_url,
593-
last_error,
594-
last_scrape,
595-
last_scrape_duration,
596-
health,
597-
});
598-
}
599-
600-
for target in data_obj["droppedTargets"].as_array().unwrap() {
601-
let target_obj = target.as_object().unwrap();
602-
let discovered_labels =
603-
parse_metric(target_obj["discoveredLabels"].as_object().unwrap());
604-
dropped.push(DroppedTarget(discovered_labels));
605-
}
606-
607-
let result = Targets { active, dropped };
608-
609-
Ok(Response::Targets(result))
564+
let raw_targets = response["data"].to_owned();
565+
let targets: Targets = serde_json::from_value(raw_targets).unwrap();
566+
Ok(Response::Targets(targets))
610567
}
611568
"error" => {
612569
return Err(Error::ResponseError(ResponseError {

src/response.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ impl Sample {
7676
}
7777

7878
/// Collection of active and dropped targets as returned by the API.
79-
#[derive(Debug)]
79+
#[derive(Debug, Deserialize)]
8080
pub struct Targets {
81+
#[serde(alias = "activeTargets")]
8182
pub(crate) active: Vec<ActiveTarget>,
83+
#[serde(alias = "droppedTargets")]
8284
pub(crate) dropped: Vec<DroppedTarget>,
8385
}
8486

@@ -95,15 +97,22 @@ impl Targets {
9597
}
9698

9799
/// A single active target.v
98-
#[derive(Debug)]
100+
#[derive(Debug, Deserialize)]
99101
pub struct ActiveTarget {
102+
#[serde(alias = "discoveredLabels")]
100103
pub(crate) discovered_labels: HashMap<String, String>,
101104
pub(crate) labels: HashMap<String, String>,
105+
#[serde(alias = "scrapePool")]
102106
pub(crate) scrape_pool: String,
107+
#[serde(alias = "scrapeUrl")]
103108
pub(crate) scrape_url: String,
109+
#[serde(alias = "globalUrl")]
104110
pub(crate) global_url: String,
111+
#[serde(alias = "lastError")]
105112
pub(crate) last_error: String,
113+
#[serde(alias = "lastScrape")]
106114
pub(crate) last_scrape: String,
115+
#[serde(alias = "lastScrapeDuration")]
107116
pub(crate) last_scrape_duration: f64,
108117
pub(crate) health: TargetHealth,
109118
}
@@ -156,13 +165,15 @@ impl ActiveTarget {
156165
}
157166

158167
/// A single dropped target.
159-
#[derive(Debug)]
160-
pub struct DroppedTarget(pub(crate) HashMap<String, String>);
168+
#[derive(Debug, Deserialize)]
169+
pub struct DroppedTarget {
170+
#[serde(alias = "discoveredLabels")]
171+
pub(crate) discovered_labels: HashMap<String, String>,
172+
}
161173

162174
impl DroppedTarget {
163175
/// Get a list of unmodified labels as before relabelling occurred.
164176
pub fn discovered_labels(&self) -> &HashMap<String, String> {
165-
let DroppedTarget(discovered_labels) = self;
166-
discovered_labels
177+
&self.discovered_labels
167178
}
168179
}

src/util.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@ impl fmt::Display for TargetState {
7474
}
7575

7676
/// A helper type to represent possible target health states.
77-
#[derive(Debug, Copy, Clone)]
77+
#[derive(Debug, Copy, Clone, serde::Deserialize)]
7878
pub enum TargetHealth {
79+
#[serde(alias = "up")]
7980
Up,
81+
#[serde(alias = "down")]
8082
Down,
83+
#[serde(alias = "unknown")]
8184
Unknown,
8285
}
8386

0 commit comments

Comments
 (0)