diff --git a/Dockerfile b/Dockerfile index 654e314..5d66c30 100644 --- a/Dockerfile +++ b/Dockerfile @@ -34,7 +34,7 @@ RUN adduser \ WORKDIR /app COPY --from=planner /app/recipe.json . -RUN cargo chef cook --release --recipe-path recipe.json +RUN cargo chef cook --target x86_64-unknown-linux-musl --release --recipe-path recipe.json COPY . . diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index a5f2d57..eeadb95 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -14,7 +14,7 @@ pub mod posts; lazy_static! { pub static ref HIT_COUNTER: IntCounterVec = - prometheus::register_int_counter_vec!(opts!("hits", "page hits"), &["page"]).unwrap(); + prometheus::register_int_counter_vec!(opts!("page_hits", "Number of hits to various pages"), &["page"]).unwrap(); } #[instrument(skip(state))] @@ -26,7 +26,7 @@ pub async fn index( error!("Failed rendering index: {}", e); WebsiteError::NotFound })?; - HIT_COUNTER.with_label_values(&["/"]); + HIT_COUNTER.with_label_values(&["/"]).inc(); Ok(Html(res.into())) } diff --git a/src/handlers/posts.rs b/src/handlers/posts.rs index 0efaf78..b47648c 100644 --- a/src/handlers/posts.rs +++ b/src/handlers/posts.rs @@ -28,7 +28,7 @@ pub async fn view( let res = render_post(&state.tera, post).await?; - HIT_COUNTER.with_label_values(&[&format!("/posts/{}/", slug)]); + HIT_COUNTER.with_label_values(&[&format!("/posts/{}/", slug)]).inc(); Ok(Html(res)) } @@ -61,6 +61,6 @@ pub async fn index(Extension(state): Extension>) -> Result Result { .nest("/posts", handlers::posts::router()) .nest_service("/static", tower_http::services::ServeDir::new("./static")) .route("/.healthcheck", get(healthcheck)) + .route("/metrics", get(metrics)) .layer(middleware); Ok(app) @@ -58,6 +61,18 @@ async fn healthcheck() -> &'static str { "OK" } +async fn metrics() -> Response { + let encoder = TextEncoder::new(); + let metric_families = prometheus::gather(); + let mut buffer = vec![]; + encoder.encode(&metric_families, &mut buffer).unwrap(); + Response::builder() + .status(200) + .header(CONTENT_TYPE, encoder.format_type()) + .body(body::boxed(body::Full::from(buffer))) + .unwrap() +} + #[derive(Debug)] pub enum WebsiteError { NotFound, diff --git a/src/post.rs b/src/post.rs index f4bf996..83ce45b 100644 --- a/src/post.rs +++ b/src/post.rs @@ -138,7 +138,7 @@ mod tests { use tera::Tera; #[tokio::test] - async fn render_all() { + async fn render_all_posts() { let tera = Tera::new("templates/**/*").unwrap(); let posts = super::load_all().await.unwrap();