1// Copyright 2019-2020 Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
34// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
89// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
1314// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
1617use crate::{
18 error::{self, Error},
19 metrics::{
20 metric_name, register, F64SharedRef, Gauge, Metric, PrometheusError, Registry,
21 StandaloneMetric, F64,
22 },
23};
2425use async_std::sync::{Arc, RwLock};
26use async_trait::async_trait;
27use std::time::Duration;
2829/// Value update interval.
30const UPDATE_INTERVAL: Duration = Duration::from_secs(300);
3132/// Metric that represents float value received from HTTP service as float gauge.
33///
34/// The float value returned by the service is assumed to be normal (`f64::is_normal`
35/// should return `true`) and strictly positive.
36#[derive(Debug, Clone)]
37pub struct FloatJsonValueMetric {
38 url: String,
39 json_path: String,
40 metric: Gauge<F64>,
41 shared_value_ref: F64SharedRef,
42}
4344impl FloatJsonValueMetric {
45/// Create new metric instance with given name and help.
46pub fn new(
47 url: String,
48 json_path: String,
49 name: String,
50 help: String,
51 ) -> Result<Self, PrometheusError> {
52let shared_value_ref = Arc::new(RwLock::new(None));
53Ok(FloatJsonValueMetric {
54 url,
55 json_path,
56 metric: Gauge::new(metric_name(None, &name), help)?,
57 shared_value_ref,
58 })
59 }
6061/// Get shared reference to metric value.
62pub fn shared_value_ref(&self) -> F64SharedRef {
63self.shared_value_ref.clone()
64 }
6566/// Request value from HTTP service.
67async fn request_value(&self) -> anyhow::Result<String> {
68use isahc::{AsyncReadResponseExt, HttpClient, Request};
6970let request = Request::get(&self.url).header("Accept", "application/json").body(())?;
71let raw_response = HttpClient::new()?.send_async(request).await?.text().await?;
72Ok(raw_response)
73 }
7475/// Read value from HTTP service.
76async fn read_value(&self) -> error::Result<f64> {
77let raw_response = self.request_value().await.map_err(Error::FetchTokenPrice)?;
78 parse_service_response(&self.json_path, &raw_response)
79 }
80}
8182impl Metric for FloatJsonValueMetric {
83fn register(&self, registry: &Registry) -> Result<(), PrometheusError> {
84 register(self.metric.clone(), registry).map(drop)
85 }
86}
8788#[async_trait]
89impl StandaloneMetric for FloatJsonValueMetric {
90fn update_interval(&self) -> Duration {
91 UPDATE_INTERVAL
92 }
9394async fn update(&self) {
95let value = self.read_value().await;
96let maybe_ok = value.as_ref().ok().copied();
97crate::metrics::set_gauge_value(&self.metric, value.map(Some));
98*self.shared_value_ref.write().await = maybe_ok;
99 }
100}
101102/// Parse HTTP service response.
103fn parse_service_response(json_path: &str, response: &str) -> error::Result<f64> {
104let json =
105 serde_json::from_str(response).map_err(|err| Error::ParseHttp(err, response.to_owned()))?;
106107let mut selector = jsonpath_lib::selector(&json);
108let maybe_selected_value =
109 selector(json_path).map_err(|err| Error::SelectResponseValue(err, response.to_owned()))?;
110let selected_value = maybe_selected_value
111 .first()
112 .and_then(|v| v.as_f64())
113 .ok_or_else(|| Error::MissingResponseValue(response.to_owned()))?;
114if !selected_value.is_normal() || selected_value < 0.0 {
115return Err(Error::ParseFloat(selected_value))
116 }
117118Ok(selected_value)
119}
120121#[cfg(test)]
122mod tests {
123use super::*;
124125#[test]
126fn parse_service_response_works() {
127assert_eq!(
128 parse_service_response("$.kusama.usd", r#"{"kusama":{"usd":433.05}}"#).map_err(drop),
129Ok(433.05),
130 );
131 }
132133#[test]
134fn parse_service_response_rejects_negative_numbers() {
135assert!(parse_service_response("$.kusama.usd", r#"{"kusama":{"usd":-433.05}}"#).is_err());
136 }
137138#[test]
139fn parse_service_response_rejects_zero_numbers() {
140assert!(parse_service_response("$.kusama.usd", r#"{"kusama":{"usd":0.0}}"#).is_err());
141 }
142143#[test]
144fn parse_service_response_rejects_nan() {
145assert!(parse_service_response("$.kusama.usd", r#"{"kusama":{"usd":NaN}}"#).is_err());
146 }
147}