refactor(server): remove spawning of 2nd tokio runtime

Closes #4448.

Pull-Request: #4454.
This commit is contained in:
chirag-bgh 2023-09-13 08:15:50 +05:30 committed by GitHub
parent 411a0495c5
commit 4ea42c99eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 26 deletions

View File

@ -21,7 +21,7 @@
use hyper::http::StatusCode;
use hyper::service::Service;
use hyper::{Body, Method, Request, Response, Server};
use log::{error, info};
use log::info;
use prometheus_client::encoding::text::encode;
use prometheus_client::registry::Registry;
use std::future::Future;
@ -30,31 +30,22 @@ use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
const METRICS_CONTENT_TYPE: &str = "application/openmetrics-text;charset=utf-8;version=1.0.0";
pub(crate) async fn metrics_server(
registry: Registry,
metrics_path: String,
) -> Result<(), std::io::Error> {
) -> Result<(), hyper::Error> {
// Serve on localhost.
let addr = ([0, 0, 0, 0], 8888).into();
// Use the tokio runtime to run the hyper server.
let rt = tokio::runtime::Runtime::new()?;
rt.block_on(async {
let server =
Server::bind(&addr).serve(MakeMetricService::new(registry, metrics_path.clone()));
info!(
"Metrics server on http://{}{}",
server.local_addr(),
metrics_path
);
if let Err(e) = server.await {
error!("server error: {}", e);
}
Ok(())
})
let server = Server::bind(&addr).serve(MakeMetricService::new(registry, metrics_path.clone()));
info!(
"Metrics server on http://{}{}",
server.local_addr(),
metrics_path
);
server.await?;
Ok(())
}
pub(crate) struct MetricService {
reg: Arc<Mutex<Registry>>,
metrics_path: String,

View File

@ -1,6 +1,5 @@
use base64::Engine;
use clap::Parser;
use futures::executor::block_on;
use futures::future::Either;
use futures::stream::StreamExt;
use futures_timer::Delay;
@ -26,7 +25,6 @@ use std::io;
use std::path::PathBuf;
use std::str::FromStr;
use std::task::Poll;
use std::thread;
use std::time::Duration;
use zeroize::Zeroizing;
@ -141,11 +139,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
"A metric with a constant '1' value labeled by version",
build_info,
);
thread::spawn(move || {
block_on(http_service::metrics_server(
metric_registry,
opt.metrics_path,
))
tokio::spawn(async move {
if let Err(e) = http_service::metrics_server(metric_registry, opt.metrics_path).await {
log::error!("Metrics server failed: {e}");
}
});
let mut bootstrap_timer = Delay::new(BOOTSTRAP_INTERVAL);