metrics-rs / metrics

A metrics ecosystem for Rust.
MIT License
1.06k stars 145 forks source link

Metrics registration regression in `main` #471

Closed jpds closed 3 months ago

jpds commented 3 months ago

I have this minimum reproducer code:

use metrics::gauge;
use metrics_exporter_prometheus::PrometheusBuilder;
use std::thread;
use std::time::Duration;

fn main() {
    let builder = PrometheusBuilder::new()
        .install()
        .expect("failed to install Prometheus recorder");

    gauge!("test").set(42);

    loop {
        thread::sleep(Duration::from_millis(10000));
    }
}

When I use the released version:

[dependencies]
metrics = "0.22.3"
#metrics = { git = "https://github.com/metrics-rs/metrics", branch = "main" }
metrics-exporter-prometheus = "0.14.0"

The metric is displayed:

$ curl localhost:9000/metrics
# TYPE test gauge
test 42

However, if I swap out the released version for main#a143ef60 - nothing is returned.

jpds commented 3 months ago

On a closer inspection - v0.22.3 is main#a143ef60 - no idea why the HTTP endpoint isn't returning any data with the reproducer.

Edit: works with:

[dependencies]
#metrics = "0.22.3"
metrics = { git = "https://github.com/metrics-rs/metrics", branch = "main" }
metrics-exporter-prometheus = { git = "https://github.com/metrics-rs/metrics", branch = "main" }

No idea what's happening there.

tobz commented 3 months ago

Sorry, I'm a little confused.

So it works if you use metrics@v0.22.3 and metrics-exporter-prometheus@v0.14.0, and it works if you use metrics@main and metrics-exporter-prometheus@main... what's the combination of metrics/metrics-exporter-prometheus that doesn't work?

jpds commented 3 months ago

It doesn't work with metrics@main, metrics-exporter-prometheus@v0.14.0.

tobz commented 3 months ago

OK, that makes sense.

Due to the global static used to hold the global recorder reference, you can't mix and match versions of metrics. In this case, you're registering metrics via metrics@main, and metrics-exporter-prometheus@v0.14.0 is installing itself to metrics@v0.22.x. This means it never has a chance to see the registered metric because it's pointed at an entirely different global static.