Skip to content
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: enable/disable metrics with env #123

Merged
merged 1 commit into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ fmt:
fmt_check:
cargo sort -c -w && cargo fmt --all -- --check && cargo clippy --all-targets --locked -- -D warnings

clean:
cargo clean

check:
cargo check --tests

Expand Down
27 changes: 17 additions & 10 deletions wheel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ pub async fn bootstrap_wheel(
wheel: Wheel,
workers: Vec<BoxedWorker>,
) -> Result<()> {
let enable_metrics = match std::env::var("RUNKV_METRICS") {
Err(_) => false,
Ok(val) => val.parse().unwrap(),
};

let addr_str = format!("{}:{}", config.host, config.port);

for mut worker in workers.into_iter() {
tokio::spawn(async move { worker.run().await });
}

let listen_addr = format!("{}:{}", config.prometheus.host, config.prometheus.port);
boot_prometheus_service(listen_addr);
if enable_metrics {
bootstrap_prometheus_service(config);
}

Server::builder()
.add_service(WheelServiceServer::new(wheel.clone()))
Expand All @@ -57,20 +63,21 @@ pub async fn bootstrap_wheel(
.map_err(Error::err)
}

pub fn boot_prometheus_service(listen_addr: String) {
pub fn bootstrap_prometheus_service(config: &WheelConfig) {
let listen_addr = format!("{}:{}", config.prometheus.host, config.prometheus.port);
let prometheus_service_addr = listen_addr.parse().unwrap();
tokio::spawn(async move {
info!(
"Prometheus listener for Prometheus is set up on http://{}",
listen_addr
);
let listen_prometheus_addr = listen_addr.parse().unwrap();
let serve_future =
hyper::Server::bind(&listen_prometheus_addr).serve(make_service_fn(|_| async {
if let Err(e) = hyper::Server::bind(&prometheus_service_addr)
.serve(make_service_fn(|_| async {
Ok::<_, hyper::Error>(service_fn(Wheel::prometheus_serve_req))
}));

if let Err(err) = serve_future.await {
tracing::error!("server error: {}", err);
}))
.await
{
tracing::error!("promethrus service error: {}", e);
}
});
}
Expand Down