AlexanderThaller / prometheus_exporter

Helper libary to export prometheus metrics using tiny_http and rust-prometheus.
MIT License
28 stars 10 forks source link

prometheus_exporter starts many OS threads #27

Closed vi closed 2 years ago

vi commented 2 years ago

I assume gathering metrics is a rare, relatively lightweight and not very performance-sensitive task, so just one additional thread is enough for it. But metric exporter seems to start multiple additional threads, wasting PIDs and other system resources just for rare occasional replies.

AlexanderThaller commented 2 years ago

This crate was created with ease of use in mind not necessarily to be the most resource efficient.

As far as I can see there should be only one additional thread spawned when starting the server. It could be that the tiny_http dependency spawns more threads. I can look into that.

Could you provide a code sample that show the behavior you describe?

vi commented 2 years ago
fn main() {
    prometheus_exporter::start("0.0.0.0:9184".parse().expect(
        "failed to parse binding",
    ))
    .expect("failed to start prometheus exporter");
    println!("Hello, world!");
    unsafe { libc::pause(); }
}
[package]
name = "pet"
version = "0.1.0"
edition = "2021"

[dependencies]
libc = "0.2.126"
prometheus_exporter = { version = "0.8.4", default-features = false }
$ strace -fce clone target/debug/pet
strace: Process 18393 attached
strace: Process 18394 attached
Hello, world!
strace: Process 18395 attached
strace: Process 18396 attached
strace: Process 18397 attached
strace: Process 18398 attached
^Cstrace: Process 18392 detached
strace: Process 18393 detached
strace: Process 18394 detached
strace: Process 18395 detached
strace: Process 18396 detached
strace: Process 18397 detached
strace: Process 18398 detached
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
100.00    0.000777         129         6           clone
------ ----------- ----------- --------- --------- ----------------
100.00    0.000777         129         6           total
AlexanderThaller commented 2 years ago

Thanks for the followup!

Doing the same with the tiny_http server I get the following:

use tiny_http::Server;

fn main() {
    let _server = Server::http("0.0.0.0:8000").unwrap();

    println!("Hello, world!");
    unsafe {
        libc::pause();
    }
}
[package]
name = "bla"
version = "0.1.0"
edition = "2021"

# see more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tiny_http = "0.11.0"
libc = "0.2.126"
» strace -fce clone target/debug/bla
strace: Process 99540 attached
Hello, world!
strace: Process 99541 attached
strace: Process 99542 attached
strace: Process 99543 attached
strace: Process 99544 attached
^Cstrace: Process 99539 detached
strace: Process 99540 detached
strace: Process 99541 detached
strace: Process 99542 detached
strace: Process 99543 detached
strace: Process 99544 detached
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
100.00    0.000147          29         5           clone
------ ----------- ----------- --------- --------- ----------------
100.00    0.000147          29         5           total

So I would think that those threads come from tiny_http maybe you can bring this up there?

Maybe looking into switching to using tokio/hyper (https://docs.rs/tokio/latest/tokio/attr.main.html#current-thread-runtime) instead of tiny_http would be an option. It would have some tradeoffs in the usability as an async runtime would get involved but I have to see. Back when I started this crate the async landscape was a bit of a mess so I didn't want to bother with it but that certainly has changed.

Let me know what you think!

AlexanderThaller commented 2 years ago

I guess this is related https://github.com/tiny-http/tiny-http/issues/205

AlexanderThaller commented 2 years ago

Gonna close this issue and track the tokio/hyper stuff here https://github.com/AlexanderThaller/prometheus_exporter/issues/29. Hope thats ok.