From 3b91fc2c64575e2708b64e2936fe75513dd07078 Mon Sep 17 00:00:00 2001 From: Eugene Tolbakov Date: Wed, 21 Jun 2023 03:50:08 +0100 Subject: [PATCH] feat: add initial implementation for status endpoint (#1789) * feat: add initial implementation for status endpoint * feat(status_endpoint): add more data to response * feat(status_endpoint): use build data env vars * feat(status_endpoint): add simple test * fix(status_endpoint): adjust the toml indentation --- src/cmd/Cargo.toml | 2 +- src/servers/Cargo.toml | 3 +++ src/servers/build.rs | 6 ++++++ src/servers/src/http.rs | 2 ++ src/servers/src/http/handler.rs | 24 +++++++++++++++++++++ src/servers/tests/http/http_handler_test.rs | 15 +++++++++++++ 6 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/cmd/Cargo.toml b/src/cmd/Cargo.toml index 5bcd4392d2..c326ab7b05 100644 --- a/src/cmd/Cargo.toml +++ b/src/cmd/Cargo.toml @@ -52,4 +52,4 @@ serde.workspace = true toml = "0.5" [build-dependencies] -build-data = "0.1.3" +build-data = "0.1.4" diff --git a/src/servers/Cargo.toml b/src/servers/Cargo.toml index 8621aa0522..d0f1523beb 100644 --- a/src/servers/Cargo.toml +++ b/src/servers/Cargo.toml @@ -103,3 +103,6 @@ table = { path = "../table" } tokio-postgres = "0.7" tokio-postgres-rustls = "0.10" tokio-test = "0.4" + +[build-dependencies] +build-data = "0.1.4" diff --git a/src/servers/build.rs b/src/servers/build.rs index e1e0ea9bb6..3803db9e29 100644 --- a/src/servers/build.rs +++ b/src/servers/build.rs @@ -13,6 +13,12 @@ // limitations under the License. fn main() { + build_data::set_RUSTC_VERSION(); + build_data::set_BUILD_HOSTNAME(); + build_data::set_GIT_BRANCH(); + build_data::set_GIT_COMMIT(); + build_data::set_SOURCE_TIMESTAMP(); + #[cfg(feature = "dashboard")] fetch_dashboard_assets(); } diff --git a/src/servers/src/http.rs b/src/servers/src/http.rs index 0b28ae0623..c1b809e5e5 100644 --- a/src/servers/src/http.rs +++ b/src/servers/src/http.rs @@ -512,6 +512,8 @@ impl HttpServer { routing::get(handler::health).post(handler::health), ); + router = router.route("/status", routing::get(handler::status)); + #[cfg(feature = "dashboard")] { if !self.options.disable_dashboard { diff --git a/src/servers/src/http/handler.rs b/src/servers/src/http/handler.rs index 04ae53aa18..d91a7d4b58 100644 --- a/src/servers/src/http/handler.rs +++ b/src/servers/src/http/handler.rs @@ -13,6 +13,7 @@ // limitations under the License. use std::collections::HashMap; +use std::env; use std::time::Instant; use aide::transform::TransformOperation; @@ -158,3 +159,26 @@ pub struct HealthResponse {} pub async fn health(Query(_params): Query) -> Json { Json(HealthResponse {}) } + +#[derive(Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct StatusResponse<'a> { + pub source_time: &'a str, + pub commit: &'a str, + pub branch: &'a str, + pub rustc_version: &'a str, + pub hostname: &'a str, + pub version: &'a str, +} + +/// Handler to expose information info about runtime, build, etc. +#[axum_macros::debug_handler] +pub async fn status() -> Json> { + Json(StatusResponse { + source_time: env!("SOURCE_TIMESTAMP"), + commit: env!("GIT_COMMIT"), + branch: env!("GIT_BRANCH"), + rustc_version: env!("RUSTC_VERSION"), + hostname: env!("BUILD_HOSTNAME"), + version: env!("CARGO_PKG_VERSION"), + }) +} diff --git a/src/servers/tests/http/http_handler_test.rs b/src/servers/tests/http/http_handler_test.rs index 4d3698c150..404df0ff75 100644 --- a/src/servers/tests/http/http_handler_test.rs +++ b/src/servers/tests/http/http_handler_test.rs @@ -365,3 +365,18 @@ async fn test_health() { expected_json_str ); } + +#[tokio::test] +async fn test_status() { + let expected_json = http_handler::StatusResponse { + source_time: env!("SOURCE_TIMESTAMP"), + commit: env!("GIT_COMMIT"), + branch: env!("GIT_BRANCH"), + rustc_version: env!("RUSTC_VERSION"), + hostname: env!("BUILD_HOSTNAME"), + version: env!("CARGO_PKG_VERSION"), + }; + + let Json(json) = http_handler::status().await; + assert_eq!(json, expected_json); +}