-
Notifications
You must be signed in to change notification settings - Fork 23
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
feat(maker): Monitor and expose status of services connected #1180
Merged
+260
−24
Merged
Changes from all commits
Commits
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 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,138 @@ | ||
use anyhow::bail; | ||
use anyhow::Context; | ||
use anyhow::Result; | ||
use reqwest::Client; | ||
use reqwest::StatusCode; | ||
use reqwest::Url; | ||
use serde::Serialize; | ||
use std::time::Duration; | ||
use tokio::sync::watch; | ||
|
||
/// Health status of a service | ||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize)] | ||
pub enum ServiceStatus { | ||
#[default] | ||
Unknown, | ||
Online, | ||
Offline, | ||
} | ||
|
||
/// Health monitoring for the node | ||
/// | ||
/// Simple endpoint querying is handled by provided configuration, for more complex health checks | ||
/// the transmitters are exposed to be plugged in the services that need to publish their health | ||
/// status. | ||
|
||
#[derive(Clone)] | ||
pub struct Health { | ||
/// Coordinator HTTP API status | ||
coordinator_rx: watch::Receiver<ServiceStatus>, | ||
/// Orderbook websocket stream status | ||
orderbook_rx: watch::Receiver<ServiceStatus>, | ||
/// Bitmex pricefeed stream status | ||
bitmex_pricefeed_rx: watch::Receiver<ServiceStatus>, | ||
} | ||
|
||
/// Transmitters that need to be plugged in the services that need to publish their health status. | ||
pub struct Tx { | ||
pub orderbook: watch::Sender<ServiceStatus>, | ||
pub coordinator: watch::Sender<ServiceStatus>, | ||
pub bitmex_pricefeed: watch::Sender<ServiceStatus>, | ||
} | ||
|
||
/// Struct returned by maker's health endpoint. | ||
#[derive(Debug, Serialize)] | ||
pub struct OverallMakerHealth { | ||
coordinator: ServiceStatus, | ||
orderbook: ServiceStatus, | ||
bitmex_pricefeed: ServiceStatus, | ||
} | ||
|
||
impl OverallMakerHealth { | ||
pub fn is_healthy(&self) -> bool { | ||
self.coordinator == ServiceStatus::Online | ||
&& self.bitmex_pricefeed == ServiceStatus::Online | ||
&& self.orderbook == ServiceStatus::Online | ||
} | ||
} | ||
|
||
impl Health { | ||
pub fn new() -> (Self, Tx) { | ||
let (orderbook_tx, orderbook_rx) = watch::channel(ServiceStatus::Unknown); | ||
let (coordinator_tx, coordinator_rx) = watch::channel(ServiceStatus::Unknown); | ||
let (bitmex_pricefeed_tx, bitmex_pricefeed_rx) = watch::channel(ServiceStatus::Unknown); | ||
|
||
( | ||
Self { | ||
coordinator_rx, | ||
orderbook_rx, | ||
bitmex_pricefeed_rx, | ||
}, | ||
Tx { | ||
orderbook: orderbook_tx, | ||
coordinator: coordinator_tx, | ||
bitmex_pricefeed: bitmex_pricefeed_tx, | ||
}, | ||
) | ||
} | ||
|
||
pub fn get_health(&self) -> Result<OverallMakerHealth> { | ||
let health_info = OverallMakerHealth { | ||
coordinator: self.get_coordinator_status(), | ||
orderbook: self.get_orderbook_status(), | ||
bitmex_pricefeed: self.get_bitmex_pricefeed_status(), | ||
}; | ||
|
||
match health_info.is_healthy() { | ||
true => Ok(health_info), | ||
false => { | ||
bail!("Status: ERROR\n + {health_info:?}"); | ||
} | ||
} | ||
} | ||
|
||
pub fn get_coordinator_status(&self) -> ServiceStatus { | ||
*self.coordinator_rx.borrow() | ||
} | ||
|
||
pub fn get_orderbook_status(&self) -> ServiceStatus { | ||
*self.orderbook_rx.borrow() | ||
} | ||
|
||
pub fn get_bitmex_pricefeed_status(&self) -> ServiceStatus { | ||
*self.bitmex_pricefeed_rx.borrow() | ||
} | ||
} | ||
|
||
/// Simple way of checking if a service is online or offline | ||
pub async fn check_health_endpoint( | ||
client: &Client, | ||
endpoint: Url, | ||
tx: watch::Sender<ServiceStatus>, | ||
interval: Duration, | ||
) { | ||
loop { | ||
let status = if check_endpoint_availability(client, endpoint.clone()) | ||
.await | ||
.is_ok() | ||
{ | ||
ServiceStatus::Online | ||
} else { | ||
ServiceStatus::Offline | ||
}; | ||
|
||
tx.send(status).expect("Receiver not to be dropped"); | ||
tokio::time::sleep(interval).await; | ||
} | ||
} | ||
|
||
async fn check_endpoint_availability(client: &Client, endpoint: Url) -> Result<StatusCode> { | ||
tracing::trace!(%endpoint, "Sending request to check health"); | ||
let response = client | ||
.get(endpoint) | ||
.send() | ||
.await | ||
.context("could not send request")? | ||
.error_for_status()?; | ||
Ok(response.status()) | ||
} |
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
Oops, something went wrong.
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.
For whom is this new endpoint?
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.
it's useful in development, testing, and manual checking if we want to quickly see the state.