Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
spawn_blocking metrics collection
Browse files Browse the repository at this point in the history
  • Loading branch information
adria0 committed Jul 18, 2020
1 parent 14598b3 commit e49f538
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ stats = { path = "util/stats" }
term_size = "0.3"
textwrap = "0.11.0"
toml = "0.5.6"
tokio = { version = "0.2", features = ["blocking"] }
verification = { path = "ethcore/verification" }
prometheus = "0.9.0"

Expand Down
1 change: 1 addition & 0 deletions parity/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern crate openethereum;
extern crate parking_lot;
extern crate parity_daemonize;
extern crate ansi_term;
extern crate tokio;

#[cfg(windows)] extern crate winapi;
extern crate ethcore_logger;
Expand Down
28 changes: 14 additions & 14 deletions parity/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ struct State {
rpc_apis: Arc<rpc_apis::FullDependencies>,
}

fn handle_request(req: Request<Body>, state: &Arc<Mutex<State>>) -> Response<Body> {
async fn handle_request(req: Request<Body>, state: Arc<Mutex<State>>) -> Response<Body> {
let (parts, _body) = req.into_parts();
match (parts.method, parts.uri.path()) {
(Method::GET, "/metrics") => {
let start = Instant::now();
let mut reg = prometheus::Registry::new();

let state = state.lock();

state.rpc_apis.client.prometheus_metrics(&mut reg);
state.rpc_apis.sync.prometheus_metrics(&mut reg);

drop(state);
let mut reg = tokio::task::spawn_blocking(move || {
let mut reg = prometheus::Registry::new();
let state = state.lock();
state.rpc_apis.client.prometheus_metrics(&mut reg);
state.rpc_apis.sync.prometheus_metrics(&mut reg);
reg
}).await.expect("The prometheus collection has panicked");

let elapsed = start.elapsed();
let ms = (elapsed.as_secs() as i64)*1000 + (elapsed.subsec_millis() as i64);
Expand Down Expand Up @@ -98,12 +98,12 @@ pub fn start_prometheus_metrics(conf: &MetricsConfiguration, deps: &rpc::Depende
let state = Arc::new(Mutex::new(state));

deps.executor.spawn_std( async move {
let make_service = make_service_fn(move |_| {
let state = state.clone();
async move {
Ok::<_, hyper::Error>(service_fn(move |req| {
let response = handle_request(req,&state);
async move { Ok::<_, hyper::Error>(response) }
let make_service = make_service_fn(move |_| {
let state = state.clone();
async move {
Ok::<_, hyper::Error>(service_fn(move |req| {
let response = handle_request(req, state.clone());
async move { Ok::<_, hyper::Error>(response.await) }
}))
}
});
Expand Down

0 comments on commit e49f538

Please sign in to comment.