Alexey Velikiy 3 years ago
parent
commit
fe5b111589
  1. 10
      src/grafana_service.rs
  2. 15
      src/grafana_service/prometheus.rs

10
src/grafana_service.rs

@ -35,10 +35,14 @@ impl GrafanaService {
} }
pub async fn extract_metrics(&self, panel_url: &str) -> types::Result<()> { pub async fn extract_metrics(&self, panel_url: &str) -> types::Result<()> {
let pm = prometheus::Prometheus::new(self, "rate(go_memstats_alloc_bytes_total[5m])", 15); let pm = prometheus::Prometheus::new(
let r = pm.query(1634672070, 1634672970).await; self,
"/api/datasources/proxy/1/api/v1/query_range",
"rate(go_memstats_alloc_bytes_total[5m])",
15,
);
let r = pm.query(1634672070, 1634672970).await?;
// println!("{}", p.to_string());
Ok(()) Ok(())
} }

15
src/grafana_service/prometheus.rs

@ -1,5 +1,3 @@
use std::{collections::HashMap, result};
use async_trait::async_trait; use async_trait::async_trait;
use serde_json::Value; use serde_json::Value;
@ -15,6 +13,7 @@ use serde_qs as qs;
use super::GrafanaService; use super::GrafanaService;
pub struct Prometheus<'a> { pub struct Prometheus<'a> {
url: String,
query: String, query: String,
step: u32, step: u32,
grafana_service: &'a GrafanaService, grafana_service: &'a GrafanaService,
@ -29,8 +28,14 @@ struct Query {
} }
impl<'a> Prometheus<'a> { impl<'a> Prometheus<'a> {
pub fn new(grafana_service: &'a GrafanaService, query: &str, step: u32) -> Prometheus<'a> { pub fn new(
grafana_service: &'a GrafanaService,
url: &str,
query: &str,
step: u32,
) -> Prometheus<'a> {
Prometheus { Prometheus {
url: url.to_owned(),
grafana_service, grafana_service,
query: query.to_string(), query: query.to_string(),
step, step,
@ -80,11 +85,11 @@ impl Metric for Prometheus<'_> {
start: from, start: from,
end: to, end: to,
}; };
let url = "/api/datasources/proxy/1/api/v1/query_range";
// TODO: use serialisatoin from serde // TODO: use serialisatoin from serde
let rq = qs::to_string(&q)?; let rq = qs::to_string(&q)?;
let (status_code, value) = self.grafana_service.post_form(&url, &rq).await?; let (status_code, value) = self.grafana_service.post_form(&self.url, &rq).await?;
// TODO: return error // TODO: return error
// if status_code != StatusCode::OK { // if status_code != StatusCode::OK {
// return std::error::("Bad status code"); // return std::error::("Bad status code");

Loading…
Cancel
Save