client_rust icon indicating copy to clipboard operation
client_rust copied to clipboard

Undeterministic order in output

Open lstrojny opened this issue 3 years ago • 13 comments

The output or encode() is non-deterministic when creating multiple metrics of the same type.

So basically:

pub fn repro() -> String {
    let mut registry = <prometheus_client::registry::Registry>::default();

    let gauge = Family::<MyLabels, Gauge<f64, AtomicU64>>::default();
    registry.register("test", "help", gauge.clone());

    gauge
        .get_or_create(&MyLabels {
            label: "one".into(),
        })
        .set(0.1);
    gauge
        .get_or_create(&MyLabels {
            label: "two".into(),
        })
        .set(0.2);

    let mut buffer = String::new();

    encode(&mut buffer, &registry).unwrap();

    buffer
}

This will sometimes return this:

HELP test help.
# TYPE test gauge
test{label="one"} 0.1
test{label="two"} 0.2
# EOF

And sometimes this:

HELP test help.
# TYPE test gauge
test{label="two"} 0.2
test{label="one"} 0.1
# EOF

I would expect the metrics to be somehow sorted deterministically.

lstrojny avatar Jan 10 '23 14:01 lstrojny

As a schort follow up: I went on to check how to do custom implementations of RouterDelegate, and it seems that a call to notifyListeners() after aborting the pop() would resolve this issue.

giorgiogross avatar Jun 26 '23 09:06 giorgiogross