From d6900c7aa926150c8811d52dbf5264d707f2a00e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Christian=20Gr=C3=BCnhage?= Date: Mon, 19 Apr 2021 10:01:35 +0200 Subject: [PATCH] feat: add healthcheck endpoint --- CHANGELOG.md | 2 ++ README.md | 5 +++++ src/metrics.rs | 24 +++++++++++++++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04f27a0..28c18f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- added healthcheck endpoint ## [v0.2.3] - 2020-04-08 ### Fixed diff --git a/README.md b/README.md index ec8581b..a5cda79 100644 --- a/README.md +++ b/README.md @@ -28,3 +28,8 @@ ARGS: ``` For configuration options, see the included sample config file. + +### Endpoints: +There's two endpoints available: + - `/metrics`, which serves the metrics + - `/health`, which should always return a 200 status code diff --git a/src/metrics.rs b/src/metrics.rs index 14a31fd..c9c7561 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -49,7 +49,15 @@ lazy_static! { .unwrap(); } -async fn serve_req(_req: Request) -> std::result::Result, hyper::Error> { +async fn serve_req(req: Request) -> std::result::Result, hyper::Error> { + match req.uri().path() { + "/metrics" => serve_metrics().await, + "/health" => serve_health_check().await, + _ => serve_not_found().await, + } +} + +async fn serve_metrics() -> std::result::Result, hyper::Error> { let encoder = TextEncoder::new(); HTTP_COUNTER.inc(); @@ -71,6 +79,20 @@ async fn serve_req(_req: Request) -> std::result::Result, h Ok(response) } +async fn serve_health_check() -> std::result::Result, hyper::Error> { + Ok(Response::builder() + .status(200) + .body(Body::from(vec![])) + .unwrap()) +} + +async fn serve_not_found() -> std::result::Result, hyper::Error> { + Ok(Response::builder() + .status(404) + .body(Body::from(vec![])) + .unwrap()) +} + pub(crate) async fn start_serving_metrics(config: Config) -> std::result::Result<(), hyper::Error> { let serve_future = Server::bind(&config.listener).serve(make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(serve_req))