metrics-rs / metrics

A metrics ecosystem for Rust.
MIT License
1.13k stars 158 forks source link

Explore migrating `Recency` to use timestamps directly. #279

Open tobz opened 2 years ago

tobz commented 2 years ago

It struck me that it may be possible to utilize timestamps directly in the metric primitives for Recency instead of needing to track a generation counter and then also store the last seen times for metrics.

Pros:

Cons:

Using quanta could be an answer to the "measuring current time might be slow" bit, doubly so using the "recent time" feature... but it could conflict with applications that used that feature for their own purposes. If their updates were too coarse, or got stopped for some reason, recency tracking could break.

While querying the current time might take 10-15ns in the very best case, incrementing an atomic counter can be close to half of that. Additionally, we would still need to atomically store the time which means we would be both querying the time and atomically storing it. The concept itself is not entirely unworkable given that it has tangible improvements to the clarity of the code, but we might need a more sophisticated approach to reduce the overhead.