Add metrics endpoint

This commit is contained in:
nora 2024-03-31 15:44:39 +02:00
parent 13f89b68ef
commit 316a40442e
6 changed files with 600 additions and 224 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use nix

783
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -9,6 +9,9 @@ edition = "2021"
axum = "0.5.16"
chrono = { version = "0.4.22", features = ["serde", "clock"] }
color-eyre = "0.6.2"
metrics = "0.22.3"
metrics-exporter-prometheus = { version = "0.14.0", default-features = false, features = ["http-listener"] }
once_cell = "1.19.0"
rusqlite = { version = "0.28.0", features = ["bundled", "uuid", "chrono"] }
serde = { version = "1.0.145", features = ["derive"] }
tempdir = "0.3.7"

View file

@ -1,4 +1,4 @@
FROM rust:1.72 as build
FROM rust:1.77 as build
RUN rustup toolchain install nightly
RUN rustup default nightly
@ -22,7 +22,7 @@ RUN touch src/main.rs
RUN cargo build --release
### RUN
FROM rust:1.72
FROM rust:1.77
RUN cargo install cargo-bisect-rustc
@ -32,6 +32,10 @@ WORKDIR /app
RUN useradd --create-home bisector
USER bisector
# this server listens on port 4000 and 4001 (metrics)
EXPOSE 4000
EXPOSE 4001
COPY --from=build /app/target/release/cargo-bisect-rustc-service cargo-bisect-rustc-service
CMD ["/app/cargo-bisect-rustc-service"]

3
shell.nix Normal file
View file

@ -0,0 +1,3 @@
{ pkgs ? import <nixpkgs> { } }: pkgs.mkShell {
packages = with pkgs; [ rustup ];
}

View file

@ -4,7 +4,10 @@ mod bisect;
mod db;
mod toolchain;
use std::sync::{mpsc, Arc, Mutex};
use std::{
future,
sync::{mpsc, Arc, Mutex},
};
use crate::bisect::Job;
use axum::{
@ -15,6 +18,7 @@ use axum::{
Extension, Json,
};
use color_eyre::eyre::Context;
use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle};
use rusqlite::Connection;
use serde::{Deserialize, Serialize};
use std::env;
@ -38,6 +42,22 @@ async fn main() -> color_eyre::Result<()> {
)
.init();
let metrics_app = async {
let prom_handle = setup_metrics();
let router: Router =
Router::new().route("/metrics", get(move || future::ready(prom_handle.render())));
info!("Starting up metrics server on port 4001");
axum::Server::bind(&"0.0.0.0:4001".parse().unwrap())
.serve(router.into_make_service())
.await
.wrap_err("failed to start server")
};
tokio::try_join!(metrics_app, main_server())?;
Ok(())
}
async fn main_server() -> color_eyre::Result<()> {
let (job_queue_send, job_queue_recv) = mpsc::sync_channel(10);
let sqlite_db = env::var("SQLITE_DB").unwrap_or_else(|_| "bisect.sqlite".to_string());
@ -115,6 +135,7 @@ async fn do_bisection(
body: String,
send_channel: Extension<SendChannel>,
) -> impl IntoResponse {
metrics::counter!("bisections").increment(1);
let job_id = Uuid::new_v4();
let job = Job::new(job_id, body, options.0);
@ -130,3 +151,6 @@ async fn do_bisection(
)),
}
}
pub fn setup_metrics() -> PrometheusHandle {
PrometheusBuilder::new().install_recorder().unwrap()
}