prometheus-net

Prometheus-net : callback to update a gauge when /metrics endpoint is called?


I'm not sure I got correctly how prometheus-net should be used but having a gauge that is time related I'd need to be able to update its value when data is being requested via the /metrics endpoint. I thought I might use a timer to updated the gauge but the ideal thing would be to have a callback exposed by prometheus-net to update what need updating just before data is returned.


Solution

  • If anyone needs it, prometheus-net provides the AddBeforeCollectCallback callback for collecting or updating your metrics, gauges, etc. just before Prometheus collects data from you.

    From prometheus-net documentation

    In some scenarios you may want to only collect data when it is requested by Prometheus. To easily implement this scenario prometheus-net enables you to register a callback before every collection occurs. Register your callback using Metrics.DefaultRegistry.AddBeforeCollectCallback().