-
Notifications
You must be signed in to change notification settings - Fork 29
feat(toolkit): add REST healthcheck infrastructure #4167
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,19 @@ | ||
| use axum::{ | ||
| extract::Extension, | ||
| http::StatusCode, | ||
| response::{Html, Json}, | ||
| response::{Html, IntoResponse, Json, Response}, | ||
| routing::{MethodRouter, get}, | ||
| }; | ||
| use chrono::{SecondsFormat, Utc}; | ||
| use serde_json::{Value, json}; | ||
| use serde_json::json; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
| use toolkit::RestHealthcheckRegistry; | ||
|
|
||
| /// Per-check timeout (from `ApiGatewayConfig::healthcheck_timeout_ms`), injected as an | ||
| /// `Extension`. A newtype avoids colliding with any other `Duration` extension. | ||
| #[derive(Clone, Copy)] | ||
| pub struct HealthcheckTimeout(pub Duration); | ||
|
|
||
| /// Returns a 501 Not Implemented handler for operations without implementations | ||
| #[allow(dead_code)] | ||
|
|
@@ -20,11 +29,38 @@ pub fn placeholder_handler_501() -> MethodRouter { | |
| }) | ||
| } | ||
|
|
||
| pub async fn health_check() -> Json<Value> { | ||
| Json(json!({ | ||
| "status": "healthy", | ||
| "timestamp": Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true) | ||
| })) | ||
| /// `GET /health`: runs all checks, returns JSON with per-component detail. Requires | ||
| /// auth (detail not for public callers). 200 (Healthy/Degraded), 503 (Unhealthy). | ||
| pub async fn health_detail( | ||
| Extension(registry): Extension<Arc<RestHealthcheckRegistry>>, | ||
| Extension(timeout): Extension<HealthcheckTimeout>, | ||
| ) -> Response { | ||
| let report = registry.report(timeout.0).await; | ||
| let status_code = status_for_report(report.is_ready()); | ||
| let body = Json(json!({ | ||
| "status": report.status, | ||
| "timestamp": Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true), | ||
| "components": report.components, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [HIGH] TOOLKIT-SEC-002 — The Fix: Either require authentication for the detail
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| })); | ||
| (status_code, body).into_response() | ||
| } | ||
|
|
||
| /// `GET /readyz`: runs all checks, public, status code only (no detail). | ||
| /// 200 (Healthy/Degraded), 503 (Unhealthy). | ||
| pub async fn readyz_check( | ||
| Extension(registry): Extension<Arc<RestHealthcheckRegistry>>, | ||
| Extension(timeout): Extension<HealthcheckTimeout>, | ||
| ) -> StatusCode { | ||
| let report = registry.report(timeout.0).await; | ||
| status_for_report(report.is_ready()) | ||
| } | ||
|
|
||
| fn status_for_report(report_ready: bool) -> StatusCode { | ||
| if report_ready { | ||
| StatusCode::OK | ||
| } else { | ||
| StatusCode::SERVICE_UNAVAILABLE | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(feature = "embed_elements"))] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RUST-API-001(MEDIUM)Issue: apply_prefix_nesting now takes four parameters including Arc RestHealthcheckRegistry and Option Arc dyn AuthNResolverClient, tightly coupling prefix nesting with health-route construction and auth wiring.
Fix: Extract health-route creation and auth middleware into separate call-site steps; keep apply_prefix_nesting focused on router nesting only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed