tikv / rust-prometheus

Prometheus instrumentation library for Rust applications
Apache License 2.0
1.05k stars 182 forks source link

U64 and U128 Guage Types #470

Open roshaans opened 1 year ago

roshaans commented 1 year ago

There are currently two types of Guage types supported by this library. IntGauge supports i64 values and gauge supports f64 values. It would be great if there was a Guage type that supported u64 values as well as u128 values.

We currently cast our u64 value into an i64 type so we can use the IntGuage type. This approach can possibly throw an exception when the u64 value goes out of bounds and we would ideally like to avoid that.

We considered using the simple guage type which supports f64 values but there seems to be a lot more performance overhead in using that according to the docs here.

Regarding the need for a u128 type guage, we would like to store a timestamp in nanoseconds which requires this, but it would be better to prioritize the u64 type first.

asomers commented 1 year ago

I have the same problem, though in my case I actually did overflow an i64. A u64 gauge would be perfect for me. Or, failing that, if IntCounter had a set method, that would work too.

lucab commented 1 year ago

All the Int* types offer some basic additional sugar, but by their own design they can't cover all possibilities. And handling all the possible cases with custom types will result in a unmaintainable matrix.

If your usecases do not fit in what the i64 helpers provide, then I'd recommend to use the usual f64 methods instead as they are actually meant to cover a larger set of cases.

That said, maybe there is a way to accommodate the u64 without adding a lot of new dedicated types, given that the AtomicF64 in this crate internally uses an atomic u64.

asomers commented 1 year ago

For our my case, f64 would be even worse. I have a counter, and I need to measure the difference between adjacent values, so f64 would result in a big precision loss.

lucab commented 1 year ago

But that's not something that this type of instrumentation can provide, nor a problem that we can solve in this library. At the bottom of all this, values only exist as f64 and Prometheus doesn't do integers.

asomers commented 1 year ago

So does this crate already convert i64 types info f64 internally?

lucab commented 1 year ago

Yes: https://github.com/tikv/rust-prometheus/blob/8b462b194565d35e84def2d27ca8efd4d395a7c9/src/value.rs#L87-L106

But, once again, f64 is really how the whole Prometheus universe works.

asomers commented 1 year ago

Lame. Well, that's what I'll do now. I'll just use the f64 Gauge type.