Browse Source

grafana imple metric

main
Alexey Velikiy 3 years ago
parent
commit
8ce82e862b
  1. 45
      src/datasources/grafana.rs
  2. 37
      src/datasources/prometheus.rs
  3. 11
      src/main.rs

45
src/datasources/grafana.rs

@ -1,5 +1,6 @@
use crate::metric::Metric; use crate::metric::Metric;
use crate::{metric::MetricResult, types}; use crate::{metric::MetricResult, types};
use async_trait::async_trait;
use hyper::{Body, Client, Method, Request, StatusCode}; use hyper::{Body, Client, Method, Request, StatusCode};
@ -12,11 +13,18 @@ use serde_json;
pub struct Grafana { pub struct Grafana {
url: String, url: String,
api_key: String, api_key: String,
datasource_url: String,
query: String,
} }
impl Grafana { impl Grafana {
pub fn new(url: String, api_key: String) -> Grafana { pub fn new(url: String, api_key: String, datasource_url: String, query: String) -> Grafana {
Grafana { api_key, url } Grafana {
api_key,
url,
datasource_url,
query,
}
} }
pub async fn test_connection(&self) -> types::Result<()> { pub async fn test_connection(&self) -> types::Result<()> {
@ -32,19 +40,17 @@ impl Grafana {
Ok(()) Ok(())
} }
pub async fn extract_metrics( // pub async fn extract_metrics(
&self, // &self,
datasource_url: &str, // from: u64,
query: &str, // to: u64,
from: u64, // step: u64,
to: u64, // ) -> types::Result<MetricResult> {
step: u64, // let pm = prometheus::Prometheus::new(self, &self.datasource_url, &self.query);
) -> types::Result<MetricResult> { // // TODO: split big query to chunks
let pm = prometheus::Prometheus::new(self, datasource_url, query); // let r = pm.query(from, to, step).await?;
// TODO: split big query to chunks // Ok(r)
let r = pm.query(from, to, step).await?; // }
Ok(r)
}
async fn get(&self, suburl: &str) -> types::Result<(StatusCode, serde_json::Value)> { async fn get(&self, suburl: &str) -> types::Result<(StatusCode, serde_json::Value)> {
let req = Request::builder() let req = Request::builder()
@ -113,3 +119,12 @@ impl Grafana {
Ok((status, result)) Ok((status, result))
} }
} }
#[async_trait]
impl Metric for Grafana {
async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult> {
let pm = prometheus::Prometheus::new(self, &self.datasource_url, &self.query);
let r = pm.query(from, to, step).await?;
Ok(r)
}
}

37
src/datasources/prometheus.rs

@ -1,26 +1,49 @@
use std::ptr::NonNull;
use async_trait::async_trait; use async_trait::async_trait;
use hyper::{Body, Client, Method, Request, StatusCode};
use crate::{ use crate::{
metric::{Metric, MetricResult}, metric::{Metric, MetricResult},
types, types,
}; };
use bytes::Buf as _;
struct Prometheus { struct Prometheus {
url: String,
query: String,
} }
impl Prometheus { impl Prometheus {
pub fn new() -> Prometheus { pub fn new(url: &String, query: &String) -> Prometheus {
Prometheus{} Prometheus {
url: url.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]
impl Metric for Prometheus { impl Metric for Prometheus {
async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult> { async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult> {
return None; return Ok(Default::default());
} }
} }

11
src/main.rs

@ -1,4 +1,4 @@
use subbeat::datasources::grafana; use subbeat::{datasources::grafana, metric::Metric};
mod cli; mod cli;
mod types; mod types;
@ -10,15 +10,12 @@ async fn main() -> types::Result<()> {
let gs = grafana::Grafana::new( let gs = grafana::Grafana::new(
cli.query_config.url.to_string(), cli.query_config.url.to_string(),
cli.query_config.key.to_string(), cli.query_config.key.to_string(),
cli.query_config.datasource_url.to_string(),
cli.query_config.query.to_string(),
); );
// gs.test_connection().await?;
// gs.get_datasources().await?;
// "http://localhost:3000/d/YeBxHjzWz/starter-app-stats?editPanel=2&orgId=1"
let r = gs let r = gs
.extract_metrics( .query(
&cli.query_config.datasource_url,
&cli.query_config.query,
cli.query_config.from, cli.query_config.from,
cli.query_config.to, cli.query_config.to,
cli.query_config.step, cli.query_config.step,

Loading…
Cancel
Save