Browse Source

some commit

main
Alexey Velikiy 3 years ago
parent
commit
0b8ad185ab
  1. 52
      src/grafana_service.rs
  2. 1
      src/main.rs

52
src/grafana_service.rs

@ -1,4 +1,4 @@
use std::io::BufRead; use std::{io::BufRead};
use hyper::{Body, Client, Method, Request}; use hyper::{Body, Client, Method, Request};
use tokio::io::{stdout, AsyncWriteExt as _}; use tokio::io::{stdout, AsyncWriteExt as _};
@ -18,55 +18,63 @@ impl GrafanaService {
} }
pub async fn test_connection(&self) -> types::Result<()> { pub async fn test_connection(&self) -> types::Result<()> {
let req = Request::builder() println!("Test connection response");
.method(Method::GET) self.get("/api").await?;
.uri(self.url.clone()) Ok(())
.header("Authorization", format!("Bearer {}", self.api_key)) }
.body(Default::default())
.unwrap();
let client = Client::new(); pub async fn get_datasources(&self) -> types::Result<()> {
let resp = client.request(req).await?; self.get("/api/datasources").await?;
println!("Response: {}", resp.status());
Ok(()) Ok(())
} }
pub async fn extract_metrics(&self) -> types::Result<()> { pub async fn extract_metrics(&self) -> types::Result<()> {
let req = Request::builder() let req = Request::builder()
.method(Method::POST)
.uri(self.url.clone() + "/api/datasources/proxy/1/api/v1/query_range") .uri(self.url.clone() + "/api/datasources/proxy/1/api/v1/query_range")
.header("content-type", "application/json")
.header("Authorization", format!("Bearer {}", self.api_key)) .header("Authorization", format!("Bearer {}", self.api_key))
.method(Method::POST)
.header("content-type", "application/json")
.body(Body::from(json!({ .body(Body::from(json!({
"query":"go_memstats_alloc_bytes_total", "query":"go_memstats_alloc_bytes_total",
"start": "1634163645", "from": "1634163645",
"end": "1634163945", "to": "1634163945",
"step": "15" "step": "15"
}).to_string()) }).to_string())
) )
.unwrap(); .unwrap();
Ok(())
}
async fn get(&self, suburl:&str) -> types::Result<()> {
let req = Request::builder()
.method(Method::GET)
.uri(self.url.to_owned() + suburl)
.header("Authorization", format!("Bearer {}", self.api_key))
.body(Body::empty())
.unwrap();
let client = Client::new(); let client = Client::new();
let res = client.request(req).await?; let res = client.request(req).await?;
println!("Response: {}", res.status()); println!("Response: {}", res.status());
println!("");
// while let Some(next) = resp.body().data().await {
// let chunk = next?;
// io::stdout().write_all(&chunk).await?;
// }
let body = hyper::body::aggregate(res).await?; let body = hyper::body::aggregate(res).await?;
let mut reader = body.reader(); let mut reader = body.reader();
let mut line = String::new(); let mut line = String::new();
loop { loop {
match reader.read_line(&mut line) { match reader.read_line(&mut line) {
Ok(_) => println!("{}", line), Ok(s) => {
if s == 0 {
break;
}
println!("{}", line);
line.clear();
},
Err(_) => break Err(_) => break
} }
} }
Ok(()) Ok(())
} }
} }

1
src/main.rs

@ -28,6 +28,7 @@ async fn main() -> types::Result<()> {
let gs = grafana_service::GrafanaService::new(url.to_string(), key.to_string()); let gs = grafana_service::GrafanaService::new(url.to_string(), key.to_string());
gs.test_connection().await?; gs.test_connection().await?;
gs.get_datasources().await?;
gs.extract_metrics().await?; gs.extract_metrics().await?;
Ok(()) Ok(())

Loading…
Cancel
Save