mirror of
https://github.com/zaphar/Heracles.git
synced 2025-07-27 14:29:51 -04:00
Compare commits
1 Commits
716f235335
...
fc06c85356
Author | SHA1 | Date | |
---|---|---|---|
fc06c85356 |
10
Cargo.toml
10
Cargo.toml
@ -7,20 +7,14 @@ license = "Apache-2.0"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.79"
|
|
||||||
async-io = "2.3.1"
|
async-io = "2.3.1"
|
||||||
axum = { version = "0.7.4", features = [ "ws" ] }
|
axum = "0.7.4"
|
||||||
axum-macros = "0.4.1"
|
|
||||||
clap = { version = "4.4.18", features = ["derive"] }
|
clap = { version = "4.4.18", features = ["derive"] }
|
||||||
maud = { version = "0.26.0", features = ["axum"] }
|
maud = { version = "0.26.0", features = ["axum"] }
|
||||||
prometheus-http-query = "0.8.2"
|
prometheus-http-api = "0.2.0"
|
||||||
serde = { version = "1.0.196", features = ["derive"] }
|
serde = { version = "1.0.196", features = ["derive"] }
|
||||||
serde_json = "1.0.113"
|
serde_json = "1.0.113"
|
||||||
serde_yaml = "0.9.31"
|
serde_yaml = "0.9.31"
|
||||||
smol = "2.0.0"
|
smol = "2.0.0"
|
||||||
smol-axum = "0.1.0"
|
smol-axum = "0.1.0"
|
||||||
smol-macros = "0.1.0"
|
smol-macros = "0.1.0"
|
||||||
tokio = { version = "1.36.0", features = ["net", "rt", "rt-multi-thread"] }
|
|
||||||
tower-http = { version = "0.5.1", features = ["trace"] }
|
|
||||||
tracing = "0.1.40"
|
|
||||||
tracing-subscriber = "0.3.18"
|
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
---
|
|
||||||
- title: Test Dasbboard 1
|
|
||||||
graphs:
|
|
||||||
- title: Node cpu
|
|
||||||
source: http://heimdall:9001
|
|
||||||
query: 'node_cpu_seconds_total{job="nodestats"}'
|
|
||||||
- title: Node memory
|
|
||||||
source: http://heimdall:9001
|
|
||||||
query: 'node_memory_MemFree_bytes{instance="andrew:9002",job="nodestats"}'
|
|
||||||
- title: Test Dasbboard 2
|
|
||||||
graphs:
|
|
||||||
- title: Node cpu
|
|
||||||
source: http://heimdall:9001
|
|
||||||
query: 'node_cpu_seconds_total{job="nodestats"}'
|
|
||||||
- title: Node memory
|
|
||||||
source: http://heimdall:9001
|
|
||||||
query: 'node_memory_MemFree_bytes{instance="andrew:9002",job="nodestats"}'
|
|
@ -1,45 +0,0 @@
|
|||||||
// Copyright 2021 Jeremy Wall
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use serde::Deserialize;
|
|
||||||
use serde_yaml;
|
|
||||||
use tracing::{debug, info};
|
|
||||||
|
|
||||||
use crate::query::QueryConn;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct Dashboard {
|
|
||||||
pub title: String,
|
|
||||||
pub graphs: Vec<Graph>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct Graph {
|
|
||||||
pub title: String,
|
|
||||||
pub source: String,
|
|
||||||
pub query: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Graph {
|
|
||||||
pub fn get_query_connection<'conn, 'graph: 'conn>(&'graph self) -> QueryConn<'conn> {
|
|
||||||
debug!(query=self.query, source=self.source, "Getting query connection for graph");
|
|
||||||
QueryConn::new(&self.source, &self.query)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_dashboard_list(path: &Path) -> anyhow::Result<Vec<Dashboard>> {
|
|
||||||
let f = std::fs::File::open(path)?;
|
|
||||||
Ok(serde_yaml::from_reader(f)?)
|
|
||||||
}
|
|
66
src/main.rs
66
src/main.rs
@ -11,66 +11,36 @@
|
|||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
use std::path::PathBuf;
|
use std::io;
|
||||||
|
use std::net::TcpListener;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow;
|
use async_io::Async;
|
||||||
use axum::{self, extract::State, routing::*, Router};
|
use axum::{self, routing::*, Router};
|
||||||
use clap::{self, Parser, ValueEnum};
|
use clap::{self, Parser};
|
||||||
use tokio::net::TcpListener;
|
use smol_macros::main;
|
||||||
use tower_http::trace::TraceLayer;
|
|
||||||
use tracing::Level;
|
|
||||||
use tracing_subscriber::FmtSubscriber;
|
|
||||||
|
|
||||||
mod dashboard;
|
|
||||||
mod query;
|
|
||||||
mod routes;
|
mod routes;
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
|
|
||||||
enum Verbosity {
|
|
||||||
ERROR,
|
|
||||||
WARN,
|
|
||||||
INFO,
|
|
||||||
DEBUG,
|
|
||||||
TRACE,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(clap::Parser)]
|
#[derive(clap::Parser)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub listen: Option<String>,
|
listen: Option<std::net::SocketAddr>,
|
||||||
#[arg(long)]
|
|
||||||
pub config: PathBuf,
|
|
||||||
#[arg(long, value_enum, default_value_t = Verbosity::INFO)]
|
|
||||||
pub verbose: Verbosity,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
main! {
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main(ex: &Arc<smol_macros::Executor<'_>>) -> io::Result<()> {
|
||||||
let args = Cli::parse();
|
let args = Cli::parse();
|
||||||
let subscriber_builder = FmtSubscriber::builder().with_max_level(match args.verbose {
|
|
||||||
Verbosity::ERROR => Level::ERROR,
|
|
||||||
Verbosity::WARN => Level::WARN,
|
|
||||||
Verbosity::INFO => Level::INFO,
|
|
||||||
Verbosity::DEBUG => Level::DEBUG,
|
|
||||||
Verbosity::TRACE => Level::TRACE,
|
|
||||||
});
|
|
||||||
tracing::subscriber::set_global_default(
|
|
||||||
subscriber_builder.with_writer(std::io::stderr).finish(),
|
|
||||||
)
|
|
||||||
.expect("setting default subscriber failed");
|
|
||||||
|
|
||||||
let config = std::sync::Arc::new(dashboard::read_dashboard_list(args.config.as_path())?);
|
|
||||||
let router = Router::new()
|
let router = Router::new()
|
||||||
// JSON api endpoints
|
// JSON api endpoints
|
||||||
.nest("/api", routes::mk_api_routes(config.clone()))
|
.nest("/api", routes::mk_api_routes())
|
||||||
// HTMX ui component endpoints
|
// HTMX ui component endpoints
|
||||||
.nest("/ui", routes::mk_ui_routes(config.clone()))
|
.nest("/ui", routes::mk_ui_routes())
|
||||||
.route("/", get(routes::index).with_state(config.clone()))
|
.route("/", get(routes::index));
|
||||||
.layer(TraceLayer::new_for_http())
|
let socket_addr = args.listen.unwrap_or("127.0.0.1:3000".parse().unwrap());
|
||||||
.with_state(State(config.clone()));
|
// TODO(jwall): Take this from clap arguments
|
||||||
let socket_addr = args.listen.unwrap_or("127.0.0.1:3000".to_string());
|
let listener = Async::<TcpListener>::bind(socket_addr).unwrap();
|
||||||
let listener = TcpListener::bind(socket_addr).await.expect("Unable to bind listener to address");
|
smol_axum::serve(ex.clone(), listener, router).await
|
||||||
axum::serve(listener, router).await?;
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
73
src/query.rs
73
src/query.rs
@ -1,73 +0,0 @@
|
|||||||
// Copyright 2021 Jeremy Wall
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use prometheus_http_query::{Client, response::{PromqlResult, Data}};
|
|
||||||
use serde::{Serialize, Deserialize};
|
|
||||||
use tracing::debug;
|
|
||||||
|
|
||||||
pub struct QueryConn<'conn> {
|
|
||||||
source: &'conn str,
|
|
||||||
query: &'conn str,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'conn> QueryConn<'conn> {
|
|
||||||
pub fn new<'a: 'conn>(source: &'a str, query: &'a str) -> Self {
|
|
||||||
Self {
|
|
||||||
source,
|
|
||||||
query,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_results(&self) -> anyhow::Result<PromqlResult> {
|
|
||||||
debug!("Getting results for query");
|
|
||||||
let client = Client::try_from(self.source)?;
|
|
||||||
Ok(client.query(self.query).get().await?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
|
||||||
pub struct DataPoint {
|
|
||||||
timesstamp: f64,
|
|
||||||
value: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
|
||||||
pub enum QueryResult {
|
|
||||||
Series(Vec<(HashMap<String, String>, Vec<DataPoint>)>),
|
|
||||||
Scalar(DataPoint),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn to_samples(data: Data) -> QueryResult {
|
|
||||||
match data {
|
|
||||||
Data::Matrix(mut range) => {
|
|
||||||
QueryResult::Series(range.drain(0..).map(|rv| {
|
|
||||||
let (metric, mut samples) = rv.into_inner();
|
|
||||||
(metric, samples.drain(0..).map(|s| {
|
|
||||||
DataPoint { timesstamp: s.timestamp(), value: s.value() }
|
|
||||||
}).collect())
|
|
||||||
}).collect())
|
|
||||||
}
|
|
||||||
Data::Vector(mut vector) => {
|
|
||||||
QueryResult::Series(vector.drain(0..).map(|iv| {
|
|
||||||
let (metric, sample) = iv.into_inner();
|
|
||||||
(metric, vec![DataPoint { timesstamp: sample.timestamp(), value: sample.value() }])
|
|
||||||
}).collect())
|
|
||||||
}
|
|
||||||
Data::Scalar(sample) => {
|
|
||||||
QueryResult::Scalar(DataPoint { timesstamp: sample.timestamp(), value: sample.value() })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,85 +11,26 @@
|
|||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use axum::{
|
|
||||||
extract::{Path, State},
|
|
||||||
routing::get,
|
|
||||||
Json, Router,
|
|
||||||
};
|
|
||||||
use maud::{html, Markup};
|
use maud::{html, Markup};
|
||||||
use tracing::debug;
|
use axum::Router;
|
||||||
|
|
||||||
use crate::dashboard::Dashboard;
|
pub fn mk_api_routes() -> Router {
|
||||||
use crate::query::{to_samples, QueryResult};
|
|
||||||
|
|
||||||
type Config = State<Arc<Vec<Dashboard>>>;
|
|
||||||
|
|
||||||
//#[axum_macros::debug_handler]
|
|
||||||
pub async fn graph_query(
|
|
||||||
State(config): Config,
|
|
||||||
Path((dash_idx, graph_idx)): Path<(usize, usize)>,
|
|
||||||
) -> Json<QueryResult> {
|
|
||||||
debug!("Getting data for query");
|
|
||||||
let graph = config
|
|
||||||
.get(dash_idx)
|
|
||||||
.expect("No such dashboard index")
|
|
||||||
.graphs
|
|
||||||
.get(graph_idx)
|
|
||||||
.expect(&format!("No such graph in dasboard {}", dash_idx));
|
|
||||||
let data = to_samples(
|
|
||||||
graph
|
|
||||||
.get_query_connection()
|
|
||||||
.get_results()
|
|
||||||
.await
|
|
||||||
.expect("Unable to get query results")
|
|
||||||
.data()
|
|
||||||
.clone(),
|
|
||||||
);
|
|
||||||
Json(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mk_api_routes(config: Arc<Vec<Dashboard>>) -> Router<Config> {
|
|
||||||
// Query routes
|
|
||||||
Router::new().route(
|
|
||||||
"/dash/:dash_idx/graph/:graph_idx",
|
|
||||||
get(graph_query).with_state(config),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mk_ui_routes(config: Arc<Vec<Dashboard>>) -> Router<Config> {
|
|
||||||
Router::new()
|
Router::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn index(State(config): Config) -> Markup {
|
pub fn mk_ui_routes() -> Router {
|
||||||
|
Router::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn index() -> Markup {
|
||||||
html! {
|
html! {
|
||||||
html {
|
html {
|
||||||
head {
|
head {
|
||||||
title { ("Heracles - Prometheus Unshackled") }
|
title { ("Heracles - Prometheus Unshackled") }
|
||||||
}
|
}
|
||||||
body {
|
body {
|
||||||
(app(State(config.clone())).await)
|
("hello world")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn app(State(config): Config) -> Markup {
|
|
||||||
let titles = config
|
|
||||||
.iter()
|
|
||||||
.map(|d| d.title.clone())
|
|
||||||
.collect::<Vec<String>>();
|
|
||||||
html! {
|
|
||||||
div {
|
|
||||||
// Header menu
|
|
||||||
ul {
|
|
||||||
@for title in &titles {
|
|
||||||
li { (title) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// dashboard display
|
|
||||||
div { }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user