Skip to content

Add reset function to Histogram#61

Merged
wraithm merged 1 commit into
masterfrom
reset-histograms
Jun 29, 2026
Merged

Add reset function to Histogram#61
wraithm merged 1 commit into
masterfrom
reset-histograms

Conversation

@wraithm

@wraithm wraithm commented Jun 29, 2026

Copy link
Copy Markdown
Member

Adds a reset function to System.Metrics.Prometheus.Metric.Histogram, resolving #55.

Counter and Gauge can already be reset with set 0, but Histogram had no equivalent. reset zeroes the bucket counts, sum, and observation count while leaving the bucket upper bounds unchanged, putting the histogram back into the same state as a freshly-created one. The primary use case is resetting metrics between tests when they're defined as top-level values.

Notes

  • Uses atomicModifyIORef' (matching observe/observeAndSample) so it's safe against concurrent observations — the prototype in the issue used the non-atomic modifyIORef'.
  • Bucket bounds (the le keys, including +Inf) are preserved; only the accumulated values are zeroed, which keeps it within the Prometheus client-spec rule that buckets must not change after creation.
  • reset is not part of the Prometheus client library spec (which only mandates observe for histograms). On a live scraped metric a reset reads as a counter reset / process restart to Prometheus, which rate/increase handle, but it's intended for test isolation. The Haddock spells this out.

Test plan

  • Observe into a histogram, call reset, then sample, and confirm all bucket counts, histSum, and histCount are 0 while the bucket keys are unchanged.

🤖 Generated with Claude Code

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) <noreply@anthropic.com>
@wraithm wraithm merged commit b53b328 into master Jun 29, 2026
15 checks passed
@wraithm wraithm deleted the reset-histograms branch June 29, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants