metrics-rs / metrics

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

How do I use from_arc()? #463

Closed vojtechkral closed 3 months ago

vojtechkral commented 4 months ago

Hi,

The Gauge and Counter types can be created with a from_arc() method. This seems useful, I have some state in my application that I'd like to share around in an Arc<AtomicU64> and use for logic, at the same time I need the number reported in metrics.

But I don't know what to do with a Gauge value once created - it doesn't seem to be possible to register it into a Recorder.

Can this be done? Thanks!

tobz commented 4 months ago

There is no way to insert a metric into Registry from an existing handle type (e.g. Counter), unfortunately.

You could potentially write a custom wrapper type that holds a Counter (or Gauge), etc, and an Arc<...> to your own data, and then implement the appropriate handle trait -- CounterFn, etc -- to call the same method on the underlying Counter as well as updating your Arc<...>.

vojtechkral commented 4 months ago

Ok. At the moment I just keep a second Arc<AtomicX> next to my Gauge.

It occurred to me the method may have been meant for tests for injecting counters/gauges without an actual recorder?

tobz commented 4 months ago

There's no reason those helper methods couldn't be used for testing, this is true... but it wasn't the primary reason to add them.

tobz commented 3 months ago

Going to close this since it seems like the issue was resolved.