forked from ZcashFoundation/zebra
-
Notifications
You must be signed in to change notification settings - Fork 1
Add health endpoint #77
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
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
23334a3
Add health endpoint
seniakalma 77d6dc4
Add health endpoint clean
seniakalma 7efbbc6
Add health endpoint cleaning
seniakalma 0d805d2
Fix typo
seniakalma b8ed0a2
Merge branch 'zsa-integration-demo' into arseni-add-healthcheck-endpoint
seniakalma 3475252
Update zebrad/Cargo.toml
seniakalma ebbfc49
Update zebrad/src/components.rs
seniakalma e24527e
Update zebrad/src/components/health.rs
seniakalma a87ffef
Update zebrad/src/components/health.rs
seniakalma a64ab67
Update zebrad/src/components/health.rs
seniakalma cf909a7
Update zebrad/src/components/health.rs
seniakalma 1a84606
Update zebrad/src/components/health.rs
seniakalma ecb49c8
Update zebrad/src/components/health.rs
seniakalma 8758fe1
Update zebrad/src/components/health.rs
seniakalma 2567195
Update Cargo.toml
seniakalma 28e73fa
Update health.rs
seniakalma f3e490f
Fix format
seniakalma 3757425
Fix clippy error
seniakalma ad83b10
Fix fmt
seniakalma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| //! A simple HTTP health endpoint for Zebra. | ||
| use abscissa_core::{Component, FrameworkError}; | ||
| use hyper::body::Incoming; | ||
| use hyper::server::conn::http1; | ||
| use hyper::service::service_fn; | ||
| use hyper::{Method, Request, Response, StatusCode}; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::convert::Infallible; | ||
| use std::net::SocketAddr; | ||
| use tracing::{error, info}; | ||
| /// Abscissa component which runs a health endpoint. | ||
| #[derive(Debug, Component)] | ||
| pub struct HealthEndpoint {} | ||
| impl HealthEndpoint { | ||
| /// Create the component. | ||
| pub fn new(config: &Config) -> Result<Self, FrameworkError> { | ||
| if let Some(addr) = config.endpoint_addr { | ||
| info!("Trying to open health endpoint at {}...", addr); | ||
| // Start the health endpoint server in a separate thread to avoid Tokio runtime issues | ||
| std::thread::spawn(move || match tokio::runtime::Runtime::new() { | ||
| Ok(rt) => { | ||
| rt.block_on(async { | ||
| if let Err(e) = Self::run_server(addr).await { | ||
| error!("Health endpoint server failed: {}", e); | ||
| } | ||
| }); | ||
| } | ||
| Err(e) => { | ||
| error!("Failed to create Tokio runtime for health endpoint: {}", e); | ||
| } | ||
| }); | ||
| info!("Opened health endpoint at {}", addr); | ||
| } | ||
| Ok(Self {}) | ||
| } | ||
| async fn run_server(addr: SocketAddr) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
| let listener = tokio::net::TcpListener::bind(addr).await?; | ||
|
|
||
| loop { | ||
| let (stream, _) = listener.accept().await?; | ||
| let io = hyper_util::rt::TokioIo::new(stream); | ||
|
|
||
| tokio::spawn(async move { | ||
| if let Err(err) = http1::Builder::new() | ||
| .serve_connection(io, service_fn(Self::handle_request)) | ||
| .await | ||
| { | ||
| error!("Failed to serve connection: {}", err); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| async fn handle_request(req: Request<Incoming>) -> Result<Response<String>, Infallible> { | ||
| match (req.method(), req.uri().path()) { | ||
| (&Method::GET, "/health") => { | ||
| let health_info = HealthInfo { | ||
| status: "healthy".to_string(), | ||
| version: env!("CARGO_PKG_VERSION").to_string(), | ||
| git_tag: option_env!("GIT_TAG").unwrap_or("unknown").to_string(), | ||
| git_commit: option_env!("GIT_COMMIT_FULL") | ||
| .unwrap_or("unknown") | ||
| .to_string(), | ||
| timestamp: chrono::Utc::now().to_rfc3339(), | ||
| }; | ||
| let response_body = | ||
| serde_json::to_string_pretty(&health_info).unwrap_or_else(|_| { | ||
| "{\"error\": \"Failed to serialize health info\"}".to_string() | ||
| }); | ||
| Ok(Response::builder() | ||
| .status(StatusCode::OK) | ||
| .header("Content-Type", "application/json") | ||
| .body(response_body) | ||
| .expect("response should build successfully")) | ||
| } | ||
| (_, "/health") => Ok(Response::builder() | ||
| .status(StatusCode::METHOD_NOT_ALLOWED) | ||
| .header("Allow", "GET") | ||
| .header("Content-Type", "application/json") | ||
| .body("{\"error\": \"Method Not Allowed\"}".to_string()) | ||
| .expect("response should build successfully")), | ||
| _ => Ok(Response::builder() | ||
| .status(StatusCode::NOT_FOUND) | ||
| .header("Content-Type", "application/json") | ||
| .body("{\"error\": \"Not Found\"}".to_string()) | ||
| .expect("response should build successfully")), | ||
| } | ||
| } | ||
| } | ||
| /// Health information response. | ||
| #[derive(Debug, Serialize)] | ||
| struct HealthInfo { | ||
| status: String, | ||
| version: String, | ||
| git_tag: String, | ||
| git_commit: String, | ||
| timestamp: String, | ||
| } | ||
| /// Health endpoint configuration section. | ||
| #[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)] | ||
| pub struct Config { | ||
| /// The address to bind the health endpoint to | ||
| pub endpoint_addr: Option<SocketAddr>, | ||
| } | ||
| impl Default for Config { | ||
| fn default() -> Self { | ||
| Self { | ||
| endpoint_addr: Some(SocketAddr::from(([127, 0, 0, 1], 8080))), | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Consider restoring
serde_jsonunder[dev-dependencies](while keeping it in[dependencies]withoptional = true), becausecargo testwithout--features=health-endpointcurrently fails with unresolved imports in tests that useserde_jsondirectly, e.g.: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.
V