metrics-rs / metrics

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

Add retain functions to Registry #461

Closed kathoum closed 4 months ago

kathoum commented 4 months ago

This adds public functions retain_counters, retain_gauges, retain_histograms, that work like HashMap::retain

kathoum commented 4 months ago

Design-wise, I'm not sure about the choice on the function signature: FnMut(&K, &mut S::Counter) -> bool or FnMut(&K, &S::Counter) -> bool

I went with &mut because there's already a write lock, so the code has exclusive access to the underlying S::Counter, and it shouldn't make much difference in the common case that Counter = Arc<impl CounterFn>.

But I understand if we prefer not allowing mut access to objects inside the storage.

tobz commented 4 months ago

Design-wise, I'm not sure about the choice on the function signature: FnMut(&K, &mut S::Counter) -> bool or FnMut(&K, &S::Counter) -> bool

Following the standard library, it might be best just to make it an immutable reference. Thinking more about it as well, mutable access really shouldn't even be a big deal since all metric types inevitably have to support concurrent immutable access for normal usage via Counter, etc.

kathoum commented 4 months ago

PR updated, changed the visitors to accept immutable references

tobz commented 4 months ago

Released in metrics-util@v0.16.3.

Thanks again for your contribution!