forked from TraceMachina/nativelink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Breaking] Factor out health status checks to its own service (TraceM…
…achina#823) Health status checks are now a proper service and no longer enabled by default. Add `"health": {},` under your services config to enable.
- Loading branch information
1 parent
b431d5f
commit 42e5b55
Showing
22 changed files
with
159 additions
and
81 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 |
---|---|---|
|
@@ -108,7 +108,8 @@ | |
"cas_stores": { | ||
"main": "CAS_MAIN_STORE", | ||
} | ||
} | ||
}, | ||
"health": {}, | ||
} | ||
}] | ||
} |
This file contains 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 |
---|---|---|
|
@@ -87,7 +87,8 @@ | |
// are a frontend api. | ||
"worker_api": { | ||
"scheduler": "MAIN_SCHEDULER", | ||
} | ||
}, | ||
"health": {}, | ||
} | ||
}] | ||
} |
This file contains 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 |
---|---|---|
|
@@ -96,7 +96,8 @@ | |
"cas_stores": { | ||
"main": "CAS_MAIN_STORE", | ||
} | ||
} | ||
}, | ||
"health": {}, | ||
} | ||
}] | ||
} |
This file contains 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 |
---|---|---|
|
@@ -71,7 +71,8 @@ | |
// are a frontend api. | ||
"worker_api": { | ||
"scheduler": "MAIN_SCHEDULER", | ||
} | ||
}, | ||
"health": {}, | ||
} | ||
}] | ||
} |
This file contains 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 |
---|---|---|
|
@@ -108,7 +108,8 @@ | |
"cas_stores": { | ||
"main": "CAS_MAIN_STORE", | ||
} | ||
} | ||
}, | ||
"health": {}, | ||
} | ||
}] | ||
} |
This file contains 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 |
---|---|---|
|
@@ -72,7 +72,8 @@ | |
// are a frontend api. | ||
"worker_api": { | ||
"scheduler": "MAIN_SCHEDULER", | ||
} | ||
}, | ||
"health": {}, | ||
} | ||
}] | ||
} |
This file contains 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 |
---|---|---|
|
@@ -163,6 +163,7 @@ | |
"worker_api": { | ||
"scheduler": "MAIN_SCHEDULER", | ||
} | ||
} | ||
}, | ||
"health": {}, | ||
}] | ||
} |
This file contains 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 |
---|---|---|
|
@@ -146,7 +146,8 @@ | |
"cas_stores": { | ||
"main": "CAS_STORE" | ||
} | ||
} | ||
}, | ||
"health": {}, | ||
} | ||
}] | ||
} |
This file contains 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 |
---|---|---|
|
@@ -171,7 +171,8 @@ | |
// are a frontend api. | ||
"worker_api": { | ||
"scheduler": "MAIN_SCHEDULER" | ||
} | ||
}, | ||
"health": {}, | ||
} | ||
}] | ||
} |
This file contains 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 |
---|---|---|
|
@@ -112,7 +112,8 @@ | |
"services": { | ||
"experimental_prometheus": { | ||
"path": "/metrics" | ||
} | ||
}, | ||
"health": {}, | ||
} | ||
}] | ||
} |
This file contains 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 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 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 |
---|---|---|
|
@@ -162,7 +162,8 @@ | |
// are a frontend api. | ||
"worker_api": { | ||
"scheduler": "MAIN_SCHEDULER", | ||
} | ||
}, | ||
"health": {}, | ||
} | ||
}] | ||
} |
This file contains 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 |
---|---|---|
|
@@ -162,7 +162,8 @@ | |
"cas_stores": { | ||
"main": "CAS_MAIN_STORE", | ||
} | ||
} | ||
}, | ||
"health": {}, | ||
} | ||
}] | ||
} |
This file contains 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 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 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 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,82 @@ | ||
// Copyright 2024 The NativeLink Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
use futures::StreamExt; | ||
use hyper::header::{HeaderValue, CONTENT_TYPE}; | ||
use hyper::{Body, Request, Response, StatusCode}; | ||
use nativelink_util::health_utils::{ | ||
HealthRegistry, HealthStatus, HealthStatusDescription, HealthStatusReporter, | ||
}; | ||
use tower::Service; | ||
|
||
/// Content type header value for JSON. | ||
const JSON_CONTENT_TYPE: &str = "application/json; charset=utf-8"; | ||
|
||
#[derive(Clone)] | ||
pub struct HealthServer { | ||
health_registry: HealthRegistry, | ||
} | ||
|
||
impl HealthServer { | ||
pub fn new(health_registry: HealthRegistry) -> Self { | ||
Self { health_registry } | ||
} | ||
} | ||
|
||
impl Service<Request<hyper::Body>> for HealthServer { | ||
type Response = Response<hyper::Body>; | ||
type Error = std::convert::Infallible; | ||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>; | ||
|
||
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
Poll::Ready(Ok(())) | ||
} | ||
|
||
fn call(&mut self, _req: Request<Body>) -> Self::Future { | ||
let health_registry = self.health_registry.clone(); | ||
Box::pin(async move { | ||
let health_status_descriptions: Vec<HealthStatusDescription> = | ||
health_registry.health_status_report().collect().await; | ||
match serde_json5::to_string(&health_status_descriptions) { | ||
Ok(body) => { | ||
let contains_failed_report = | ||
health_status_descriptions.iter().any(|description| { | ||
matches!(description.status, HealthStatus::Failed { .. }) | ||
}); | ||
let status_code = if contains_failed_report { | ||
StatusCode::SERVICE_UNAVAILABLE | ||
} else { | ||
StatusCode::OK | ||
}; | ||
|
||
Ok(Response::builder() | ||
.status(status_code) | ||
.header(CONTENT_TYPE, HeaderValue::from_static(JSON_CONTENT_TYPE)) | ||
.body(Body::from(body)) | ||
.unwrap()) | ||
} | ||
|
||
Err(e) => Ok(Response::builder() | ||
.status(StatusCode::INTERNAL_SERVER_ERROR) | ||
.header(CONTENT_TYPE, HeaderValue::from_static(JSON_CONTENT_TYPE)) | ||
.body(Body::from(format!("Internal Failure: {e:?}"))) | ||
.unwrap()), | ||
} | ||
}) | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.