Skip to content

Commit

Permalink
chore: convert health checks & metrics to services
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Oct 29, 2024
1 parent 85262d6 commit d50c0bb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
10 changes: 3 additions & 7 deletions packages/common/health-checks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum HealthCheckError {
}

#[tracing::instrument(skip_all)]
pub fn spawn_standalone(config: Config) -> GlobalResult<()> {
pub async fn run_standalone(config: Config) -> GlobalResult<()> {
let config = Arc::new(config);
let host = config.config.server()?.rivet.health.host();
let port = config.config.server()?.rivet.health.port();
Expand All @@ -39,12 +39,8 @@ pub fn spawn_standalone(config: Config) -> GlobalResult<()> {

let server = Server::try_bind(&addr)?.serve(make_service);

tokio::spawn(async move {
tracing::info!(?host, ?port, "started health server");
if let Err(err) = server.await {
tracing::error!(?err, "health server error");
}
});
tracing::info!(?host, ?port, "started health server");
server.await?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion packages/common/metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ mod server;
pub use buckets::{BUCKETS, PROVISION_BUCKETS};
pub use prometheus;
pub use registry::REGISTRY;
pub use server::spawn_standalone;
pub use server::run_standalone;
10 changes: 3 additions & 7 deletions packages/common/metrics/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::net::SocketAddr;
// TODO: Record extra labels

#[tracing::instrument(skip_all)]
pub fn spawn_standalone(config: rivet_config::Config) -> GlobalResult<()> {
pub async fn run_standalone(config: rivet_config::Config) -> GlobalResult<()> {
let host = config.server()?.rivet.metrics.host();
let port = config.server()?.rivet.metrics.port();
let addr = SocketAddr::from((host, port));
Expand All @@ -19,12 +19,8 @@ pub fn spawn_standalone(config: rivet_config::Config) -> GlobalResult<()> {
Ok::<_, hyper::Error>(service_fn(serve_req))
}));

tokio::spawn(async move {
tracing::info!(?host, ?port, "started metrics server");
if let Err(err) = server.await {
tracing::error!(?err, "metrics server error");
}
});
tracing::info!(?host, ?port, "started metrics server");
server.await?;

Ok(())
}
Expand Down
33 changes: 21 additions & 12 deletions packages/common/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ pub enum ServiceKind {
Singleton,
Oneshot,
Cron,
/// Run no matter what.
Core,
}

impl ServiceKind {
fn behavior(&self) -> ServiceBehavior {
use ServiceKind::*;

match self {
Api | ApiInternal | Standalone | Singleton => ServiceBehavior::Service,
Api | ApiInternal | Standalone | Singleton | Core => ServiceBehavior::Service,
Oneshot | Cron => ServiceBehavior::Oneshot,
}
}
Expand All @@ -72,11 +74,27 @@ enum ServiceBehavior {
pub async fn start(
config: rivet_config::Config,
pools: rivet_pools::Pools,
services: Vec<Service>,
mut services: Vec<Service>,
) -> Result<()> {
tracing::info!(services = ?services.len(), "starting server");
// Inject metrics and health services
services.push(Service::new(
"health_checks",
ServiceKind::Core,
|config, pools| {
rivet_health_checks::run_standalone(rivet_health_checks::Config {
config,
pools: Some(pools),
})
},
));
services.push(Service::new(
"metrics",
ServiceKind::Core,
|config, _pools| rivet_metrics::run_standalone(config),
));

// Spawn services
tracing::info!(services = ?services.len(), "starting server");
let mut join_set = tokio::task::JoinSet::new();
for service in services {
tracing::info!(name = %service.name, kind = ?service.kind, "server starting service");
Expand Down Expand Up @@ -138,15 +156,6 @@ pub async fn start(
}
}

// Run health & metrics servers
rivet_health_checks::spawn_standalone(rivet_health_checks::Config {
config: config.clone(),
pools: Some(pools.clone()),
})
.map_err(|err| anyhow!("failed to spawn health checks: {err}"))?;
rivet_metrics::spawn_standalone(config.clone())
.map_err(|err| anyhow!("failed to spawn metrics: {err}"))?;

// Wait for services
join_set.join_all().await;
tracing::info!("all services finished");
Expand Down

0 comments on commit d50c0bb

Please sign in to comment.