Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Prometheus from control plane #87

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions crates/snot-agent/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,32 @@ impl AgentService for AgentRpcServer {
}
}

async fn get_metrics(self, _: context::Context) -> Result<String, AgentError> {
if !matches!(
self.state.agent_state.read().await.deref(),
AgentState::Node(_, _)
) {
return Err(AgentError::InvalidState);
}

let url = format!("http://127.0.0.1:{}/", self.state.cli.ports.metrics);

let response = reqwest::Client::new()
.get(url)
.send()
.await
.map_err(|_| AgentError::FailedToMakeRequest)?;

if !response.status().is_success() {
return Err(AgentError::FailedToMakeRequest);
}

response
.text()
.await
.map_err(|_| AgentError::FailedToMakeRequest)
}

async fn get_metric(self, _: context::Context, metric: AgentMetric) -> f64 {
let metrics = self.state.metrics.read().await;

Expand Down
5 changes: 4 additions & 1 deletion crates/snot-common/src/rpc/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ pub trait AgentService {
async fn broadcast_tx(tx: String) -> Result<(), AgentError>;

/// Locally execute an authorization, using the given query
/// environment id is passed so the agent can determine which aot binary to use
/// environment id is passed so the agent can determine which aot binary to
/// use
async fn execute_authorization(
env_id: usize,
query: String,
auth: String,
) -> Result<(), AgentError>;

async fn get_metrics() -> Result<String, AgentError>;

async fn get_metric(metric: AgentMetric) -> f64;
}

Expand Down
2 changes: 0 additions & 2 deletions crates/snot-common/src/rpc/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use crate::state::AgentId;

#[tarpc::service]
pub trait ControlService {
async fn placeholder() -> String;

/// Resolve the addresses of the given agents.
async fn resolve_addrs(
peers: HashSet<AgentId>,
Expand Down
1 change: 1 addition & 0 deletions crates/snot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ tracing-subscriber.workspace = true
url = { workspace = true, features = ["serde"] }
uuid = { workspace = true, features = ["v4", "fast-rng"] }
wildmatch = "2.3.3"
bollard = "0.16.1"
4 changes: 4 additions & 0 deletions crates/snot/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub struct Cli {
/// Control plane server port
pub port: u16,

#[arg(long, default_value = "9090")]
/// Prometheus server port
pub prometheus: u16,

#[arg(long, default_value = "snot-control-data")]
/// Path to the directory containing the stored data
pub path: PathBuf,
Expand Down
31 changes: 29 additions & 2 deletions crates/snot/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use std::io;
use std::{io, sync::Arc};

use clap::Parser;
use cli::Cli;
use surrealdb::Surreal;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::prelude::*;

use crate::state::GlobalState;

pub mod cannon;
pub mod cli;
pub mod env;
pub mod logging;
pub mod prometheus;
pub mod schema;
pub mod server;
pub mod state;
Expand All @@ -31,6 +35,8 @@ async fn main() {
.add_directive("surrealdb=off".parse().unwrap())
.add_directive("tungstenite=off".parse().unwrap())
.add_directive("tokio_tungstenite=off".parse().unwrap())
.add_directive("tokio_util=off".parse().unwrap())
.add_directive("bollard=ERROR".parse().unwrap())
.add_directive("tarpc::client=ERROR".parse().unwrap())
.add_directive("tarpc::server=ERROR".parse().unwrap())
.add_directive("tower_http::trace::on_request=off".parse().unwrap())
Expand All @@ -56,5 +62,26 @@ async fn main() {

let cli = Cli::parse();

server::start(cli).await.expect("start server");
let mut path = cli.path.clone();
path.push("data.db");

let db = Surreal::new::<surrealdb::engine::local::File>(path)
.await
.expect("failed to create surrealDB");

let state = GlobalState {
cli,
db,
prom_ctr: Default::default(),
pool: Default::default(),
storage_ids: Default::default(),
storage: Default::default(),
envs: Default::default(),
};

prometheus::init(&state)
.await
.expect("failed to launch prometheus container");

server::start(Arc::new(state)).await.expect("start server");
}
47 changes: 47 additions & 0 deletions crates/snot/src/prometheus/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use indexmap::IndexMap;
use serde::Serialize;

// TODO: we could probably clean this up or make it look a little bit prettier
// later

#[derive(Debug, Clone, Serialize)]
pub struct PrometheusConfig {
pub global: GlobalConfig,
pub scrape_configs: Vec<ScrapeConfig>,
}

#[derive(Debug, Clone, Serialize)]
pub struct GlobalConfig {
pub scrape_interval: String,
pub scrape_timeout: String,
pub evaluation_interval: String,
}

impl Default for GlobalConfig {
fn default() -> Self {
Self {
scrape_interval: "15s".into(),
scrape_timeout: "10s".into(),
evaluation_interval: "1m".into(),
}
}
}

#[derive(Debug, Clone, Serialize)]
pub struct ScrapeConfig {
pub job_name: String,
pub honor_timestamps: Option<bool>,
pub scrape_interval: Option<String>,
pub scrape_timeout: Option<String>,
pub metrics_path: Option<String>,
pub scheme: Option<String>,
pub follow_redirects: Option<bool>,
#[serde(default)]
pub static_configs: Vec<StaticConfig>,
}

#[derive(Debug, Clone, Serialize)]
pub struct StaticConfig {
pub targets: Vec<String>,
pub labels: IndexMap<String, String>,
}
Loading