Browse Source

prom++

main
Alexey Velikiy 3 years ago
parent
commit
775e6f33aa
  1. 29
      src/datasources/prometheus.rs
  2. 1
      src/lib.rs
  3. 40
      src/utils.rs

29
src/datasources/prometheus.rs

@ -5,12 +5,13 @@ use serde_derive::{Deserialize, Serialize};
use crate::{ use crate::{
metric::{Metric, MetricResult}, metric::{Metric, MetricResult},
types, types, utils,
}; };
use serde_qs as qs; use serde_qs as qs;
use bytes::Buf as _; use bytes::Buf as _;
use std::io::Read;
pub struct Prometheus { pub struct Prometheus {
url: String, url: String,
@ -32,25 +33,6 @@ impl Prometheus {
query: query.to_owned(), query: query.to_owned(),
} }
} }
// TODO: move to utils
async fn get(&self, suburl: &str) -> types::Result<(StatusCode, serde_json::Value)> {
let req = Request::builder()
.method(Method::GET)
.uri(self.url.to_owned() + suburl)
.header("Accept", "application/json")
.body(Body::empty())
.unwrap();
let client = Client::new();
let res = client.request(req).await?;
let status = res.status();
let body = hyper::body::aggregate(res).await?;
let reader = body.reader();
let result: serde_json::Value = serde_json::from_reader(reader)?;
Ok((status, result))
}
} }
#[async_trait] #[async_trait]
@ -64,7 +46,12 @@ impl Metric for Prometheus {
}; };
let qs = qs::to_string(&q)?; let qs = qs::to_string(&q)?;
println!("prom str: {}", qs); let url = format!("{}/api/v1/query_range?{}", self.url, qs);
let v = utils::get(&url).await?;
println!("Prom value:");
println!("{:?}", &v);
// TODO: query // TODO: query
// TODO: parse // TODO: parse
return Ok(Default::default()); return Ok(Default::default());

1
src/lib.rs

@ -1,3 +1,4 @@
pub mod datasources; pub mod datasources;
pub mod metric; pub mod metric;
pub mod types; pub mod types;
pub mod utils;

40
src/utils.rs

@ -0,0 +1,40 @@
use bytes::Buf as _;
use hyper::{Body, Client, Method, Request, StatusCode};
use std::io::Read;
use crate::types;
// TODO: move to utils
pub async fn get(url: &String) -> types::Result<(StatusCode, serde_json::Value)> {
let req = Request::builder()
.method(Method::GET)
.uri(url)
.header("Accept", "application/json")
.body(Body::empty())
.unwrap();
let client = Client::new();
let res = client.request(req).await?;
let status = res.status();
let body = hyper::body::aggregate(res).await?;
let mut reader = body.reader();
{
let mut dst = [0; 1024];
let num = reader.read(&mut dst).unwrap();
let mut vec = Vec::<u8>::new();
for i in 0..num {
vec.push(dst[i]);
}
let str = String::from_utf8(vec).unwrap();
println!("{}", str);
}
panic!("asdas");
// String::from_utf8(reader.bytes());
// let result: serde_json::Value = serde_json::from_reader(reader)?;
// Ok((status, result))
}
Loading…
Cancel
Save