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

Push doesn't work inside tokio async block #342

Closed
adamchalmers opened this issue Sep 1, 2020 · 1 comment
Closed

Push doesn't work inside tokio async block #342

adamchalmers opened this issue Sep 1, 2020 · 1 comment

Comments

@adamchalmers
Copy link

Describe the bug
The prometheus::push_metrics function panics if called inside an async block (at least when using the Tokio runtime). It panics with the message 'Cannot drop a runtime in a context where blocking is not allowed. This happens when a runtime is dropped from within an asynchronous context.'

I believe this is due to the use of reqwest::blocking::Client in the implementation. One backwards-compatible solution would be adding a new prometheus::push_metrics_async function that uses reqwest::Client instead. I would be happy to open a PR to add this, if you like the suggestion.

To Reproduce
Minimal example to reproduce the behavior:

src/main.rs

#[macro_use]
extern crate prometheus;
use prometheus::{gather, push_metrics, Error};
use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let counter = register_counter!("example_counter", "Just a counter").unwrap();
    counter.inc();
    push_metrics("example", HashMap::new(), "localhost:9091", gather(), None)?;
    println!("Success");
    Ok(())
}

Cargo.toml

[package]
name = "prometheus-bug"
version = "0.1.0"
authors = ["Adam Chalmers <adam.s.chalmers@gmail.com>"]
edition = "2018"

[dependencies]
tokio = { version = "0.2.22", features = ["macros"] }

[dependencies.prometheus]
version = "0.10"
default-features = true
features = ["push", "process"]

Expected behavior
push_metrics() does not panic when called inside an async function using Tokio.

System information

  • CPU architecture: Intel i7
  • Distribution and kernel version: MacOS Mojave
adamchalmers added a commit to adamchalmers/rust-prometheus that referenced this issue Sep 3, 2020
adamchalmers added a commit to adamchalmers/rust-prometheus that referenced this issue Sep 18, 2020
adamchalmers added a commit to adamchalmers/rust-prometheus that referenced this issue Sep 18, 2020
adamchalmers added a commit to adamchalmers/rust-prometheus that referenced this issue Sep 18, 2020
adamchalmers added a commit to adamchalmers/rust-prometheus that referenced this issue Sep 18, 2020
adamchalmers added a commit to adamchalmers/rust-prometheus that referenced this issue Sep 18, 2020
adamchalmers added a commit to adamchalmers/rust-prometheus that referenced this issue Sep 22, 2020
adamchalmers added a commit to adamchalmers/rust-prometheus that referenced this issue Feb 20, 2021
adamchalmers added a commit to adamchalmers/rust-prometheus that referenced this issue Feb 21, 2021
Signed-off-by: Adam Chalmers <adam.s.chalmers@gmail.com>
@adamchalmers
Copy link
Author

Actually, this is fixed in Tokio 1.0, changing the Cargo.toml from the example to use

tokio = { version = "1", features = ["macros", "time", "rt-multi-thread"] }

will cause the program to terminate successfully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant