From d04500d98f3f761896d5afbe80f3cc412f2e3733 Mon Sep 17 00:00:00 2001 From: Matthew Wraith Date: Mon, 29 Jun 2026 11:39:33 -0700 Subject: [PATCH] Add reset function to Histogram Adds a `reset` to `System.Metrics.Prometheus.Metric.Histogram` that zeroes the bucket counts, sum, and observation count while leaving the bucket upper bounds unchanged. This mirrors `set 0` on `Counter`/`Gauge` and is useful for resetting metrics between tests when they are defined as top-level values. The reset uses `atomicModifyIORef'` for consistency with `observe`, so it is safe against concurrent observations. Closes #55 Co-Authored-By: Claude Opus 4.8 (1M context) --- CHANGELOG.md | 8 +++++++ prometheus.cabal | 2 +- .../Metrics/Prometheus/Metric/Histogram.hs | 22 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a68b44673..57954c745 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,12 @@ +## 2.4.0 + +* Add a `reset` function to the + `System.Metrics.Prometheus.Metric.Histogram` module, which zeroes the + bucket counts, sum, and observation count while leaving the bucket bounds + unchanged. This is useful for resetting metrics between tests. + [#55](https://github.com/bitnomial/prometheus/issues/55) + ## 2.3.1 * Relax the `http-client-tls` upper bound to allow `0.4.*`. diff --git a/prometheus.cabal b/prometheus.cabal index 052e6c68a..db57a7874 100644 --- a/prometheus.cabal +++ b/prometheus.cabal @@ -1,5 +1,5 @@ name: prometheus -version: 2.3.1 +version: 2.4.0 synopsis: Prometheus Haskell Client homepage: http://github.com/bitnomial/prometheus bug-reports: http://github.com/bitnomial/prometheus/issues diff --git a/src/System/Metrics/Prometheus/Metric/Histogram.hs b/src/System/Metrics/Prometheus/Metric/Histogram.hs index 39a49c26a..d1db1649f 100644 --- a/src/System/Metrics/Prometheus/Metric/Histogram.hs +++ b/src/System/Metrics/Prometheus/Metric/Histogram.hs @@ -9,6 +9,7 @@ module System.Metrics.Prometheus.Metric.Histogram ( observe, sample, observeAndSample, + reset, ) where import Control.Applicative ((<$>)) @@ -71,3 +72,24 @@ updateBuckets x = Map.mapWithKey updateBucket sample :: Histogram -> IO HistogramSample sample = readIORef . unHistogram + + +-- | Reset the histogram back to its initial state: every bucket count, the +-- sum, and the observation count return to 0. The bucket upper bounds are left +-- unchanged. +-- +-- This is /not/ part of the Prometheus client library specification, which +-- only defines @observe@ for histograms. It is intended for resetting metrics +-- between tests when they are defined as top-level values. On a live, scraped +-- metric a reset looks like a counter reset (e.g. a process restart) to +-- Prometheus, which its @rate@/@increase@ functions handle, so prefer +-- 'observe' there and reserve 'reset' for test isolation. +reset :: Histogram -> IO () +reset (Histogram ref) = atomicModifyIORef' ref $ \histData -> + ( histData + { histBuckets = 0 <$ histBuckets histData + , histSum = 0 + , histCount = 0 + } + , () + )