Browse Source

prom++

main
Alexey Velikiy 3 years ago
parent
commit
9c1a10a6ca
  1. 7
      README.md
  2. 2
      src/datasources.rs
  3. 21
      src/datasources/prometheus.rs
  4. 14
      src/main.rs

7
README.md

@ -9,5 +9,12 @@ subbeat grafana http://localhost:3000 eyJrIjoiWnRRMTNmcGpvTHNPb3UzNzdUNUphRm53Rk
### Datasources
#### Prometheus
```
subbeat prometheus http://localhost:9090/ "rate(go_memstats_alloc_bytes_total[5m])" 1634172070 1635110190 15
```
#### Grafana
* Prometheus

2
src/datasources.rs

@ -6,7 +6,7 @@ use crate::{
pub mod grafana;
pub mod prometheus;
pub fn resolve(query_config: &QueryConfig) -> Box<dyn Metric> {
pub fn resolve(query_config: &QueryConfig) -> Box<dyn Metric + Sync> {
if query_config.datasource_type == DatasourceType::Grafana {
let gs = grafana::Grafana::new(
query_config.url.to_string(),

21
src/datasources/prometheus.rs

@ -1,11 +1,15 @@
use async_trait::async_trait;
use hyper::{Body, Client, Method, Request, StatusCode};
use serde_derive::{Deserialize, Serialize};
use crate::{
metric::{Metric, MetricResult},
types,
};
use serde_qs as qs;
use bytes::Buf as _;
pub struct Prometheus {
@ -13,6 +17,14 @@ pub struct Prometheus {
query: String,
}
#[derive(Deserialize, Serialize)]
struct Query {
query: String,
start: u64,
end: u64,
step: u64,
}
impl Prometheus {
pub fn new(url: &String, query: &String) -> Prometheus {
Prometheus {
@ -44,6 +56,15 @@ impl Prometheus {
#[async_trait]
impl Metric for Prometheus {
async fn query_chunk(&self, from: u64, to: u64, step: u64) -> types::Result<MetricResult> {
let q = Query {
query: self.query.to_owned(),
start: from,
end: to,
step,
};
let qs = qs::to_string(&q)?;
println!("prom str: {}", qs);
// TODO: query
// TODO: parse
return Ok(Default::default());

14
src/main.rs

@ -1,4 +1,7 @@
use subbeat::{datasources::grafana, metric::Metric};
use subbeat::{
datasources::{grafana, resolve},
metric::Metric,
};
mod cli;
mod types;
@ -7,14 +10,9 @@ mod types;
async fn main() -> types::Result<()> {
let cli = cli::CLI::new();
let gs = grafana::Grafana::new(
cli.query_config.url.to_string(),
cli.query_config.key.to_string(),
cli.query_config.datasource_url.to_string(),
cli.query_config.query.to_string(),
);
let ds = resolve(&cli.query_config);
let r = gs
let r = ds
.query(
cli.query_config.from,
cli.query_config.to,

Loading…
Cancel
Save