prometheus / client_rust

Prometheus / OpenMetrics client library in Rust
Apache License 2.0
484 stars 80 forks source link

Way to unregister "MetricFamily" from registry #204

Closed tyrone-wu closed 4 months ago

tyrone-wu commented 4 months ago

Hello! πŸ‘‹

I have a usecase that can be split into 2 separate issues (let me know and I can split it πŸ‘), but I'll have them here for now to describe my overall usecase.

I'm creating a library with sub-systems that each manage their own registry, and are logically grouped together to form a root registry. For each sub-system, they hold some state that is used to register/unregister metric families.

~For the first part of my usecase, I've tried using sub_registry_with_prefix() and the Collector trait, however, they both have these limitations:~

~If possible, I'd like for my sub-systems to either "own" their sub_registry so that they can individually manage their own metrics. Perhaps something similar to Family, so Vec<Rc<RwLock<Registry>>> or Vec<Rc<RefCell<Registry>>> for sub_registries?~

For the second part of my usecase, this is simply adding a way to unregister metric families, something like Registry::unregister(&mut self, name: &str, unit: Option<Unit>)?

If these changes make sense to be apart of the library, I'd be happy to open a PR for these. 🐱 And sorry if what I described above is messy, I can elaborate if need be.

tyrone-wu commented 4 months ago

Actually #154 solves the first part of my usecase since I can just stitch together the registries of my sub-systems. πŸ‘

The only part left is just a way to unregister a metric family.

tyrone-wu commented 4 months ago

Alright, so I have sort of a working progress. Removing it from metrics list is pretty straightforward, however, there's the issue how to approach cleaning up leftover Arc references to Family.

My initial idea was to have priv_register() return a Weak<dyn Metric>, however I'm learning that adding a Clone trait bound to Metric doesn't make it object-safe. I won't lie, I'm not rusty enough to completely understand all the ins and outs of these internal things. 😡