Alexey Velikiy 3 years ago
parent
commit
974f252c50
  1. 7
      README.md
  2. 16
      src/grafana_service.rs
  3. 8
      src/grafana_service/prometheus.rs
  4. 38
      src/main.rs

7
README.md

@ -1,2 +1,9 @@
# subbeat # subbeat
subbeat subbeat
## Example
```
subbeat http://localhost:3000 eyJrIjoiWnRRMTNmcGpvTHNPb3UzNzdUNUphRm53Rk9tMTNzOTQiLCJuIjoic3ViYmVhdC10ZXN0IiwiaWQiOjF9 "/api/datasources/proxy/1/api/v1/query_range" "rate(go_memstats_alloc_bytes_total[5m])" 1634672070 1634672970
```

16
src/grafana_service.rs

@ -1,7 +1,6 @@
use crate::{metric::MetricResult, types}; use crate::{metric::MetricResult, types};
use hyper::{Body, Client, Method, Request, StatusCode}; use hyper::{Body, Client, Method, Request, StatusCode};
use tokio::io::{stdout, AsyncWriteExt as _};
use bytes::Buf as _; use bytes::Buf as _;
@ -34,12 +33,15 @@ impl GrafanaService {
Ok(()) Ok(())
} }
pub async fn extract_metrics(&self, datasource_url: &str, query: &str, from: u64, to: u64, step: u64) -> types::Result<MetricResult> { pub async fn extract_metrics(
let pm = prometheus::Prometheus::new( &self,
self, datasource_url: &str,
datasource_url, query: &str,
query from: u64,
); to: u64,
step: u64,
) -> types::Result<MetricResult> {
let pm = prometheus::Prometheus::new(self, datasource_url, query);
// TODO: split big query to chunks // TODO: split big query to chunks
let r = pm.query(from, to, step).await?; let r = pm.query(from, to, step).await?;
Ok(r) Ok(r)

8
src/grafana_service/prometheus.rs

@ -23,15 +23,11 @@ struct Query {
query: String, query: String,
start: u64, start: u64,
end: u64, end: u64,
step: u64 step: u64,
} }
impl<'a> Prometheus<'a> { impl<'a> Prometheus<'a> {
pub fn new( pub fn new(grafana_service: &'a GrafanaService, url: &str, query: &str) -> Prometheus<'a> {
grafana_service: &'a GrafanaService,
url: &str,
query: &str,
) -> Prometheus<'a> {
Prometheus { Prometheus {
url: url.to_owned(), url: url.to_owned(),
grafana_service, grafana_service,

38
src/main.rs

@ -20,17 +20,53 @@ async fn main() -> types::Result<()> {
.required(true) .required(true)
.index(2), .index(2),
) )
.arg(
Arg::with_name("datasource_url")
.help("relative path to datasource")
.required(true)
.index(3),
)
.arg(
Arg::with_name("query")
.help("your query to datasource")
.required(true)
.index(4),
)
.arg(
Arg::with_name("from")
.help("timestamp")
.required(true)
.index(5),
)
.arg(
Arg::with_name("to")
.help("timestampt")
.required(true)
.index(6),
)
.get_matches(); .get_matches();
let url = matches.value_of("GRAFANA_URL").unwrap(); let url = matches.value_of("GRAFANA_URL").unwrap();
let key = matches.value_of("GRAFANA_API_KEY").unwrap(); let key = matches.value_of("GRAFANA_API_KEY").unwrap();
let datasource_url = matches.value_of("datasource_url").unwrap();
let query = matches.value_of("query").unwrap();
let from = matches.value_of("from").unwrap().parse().unwrap();
let to = matches.value_of("to").unwrap().parse().unwrap();
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.get_datasources().await?;
// "http://localhost:3000/d/YeBxHjzWz/starter-app-stats?editPanel=2&orgId=1" // "http://localhost:3000/d/YeBxHjzWz/starter-app-stats?editPanel=2&orgId=1"
let r = gs.extract_metrics("/api/datasources/proxy/1/api/v1/query_range", "rate(go_memstats_alloc_bytes_total[5m])", 1634672070, 1634672970, 15) let r = gs
.extract_metrics(
datasource_url,
query,
from,
to,
15,
)
.await?; .await?;
let key = r.keys().nth(0).unwrap(); let key = r.keys().nth(0).unwrap();

Loading…
Cancel
Save