testutil: add native histogram assertion helpers#2020
Open
pedrampdd wants to merge 1 commit into
Open
Conversation
The text-based GatherAndCompare cannot represent native (exponential) histograms, so there was previously no way to assert on them in tests. Add four Gatherer-based helpers that select a series by name and optional labels and verify a native histogram: - GatherAndAssertNativeHistogramExists - GatherAndAssertNativeHistogramCount - GatherAndAssertNativeHistogramSum (within a tolerance) - GatherAndAssertNativeHistogramIntervalCount The interval helper reconstructs the sparse buckets from the spans and deltas and estimates the number of observations in a half-open (lower, upper] interval using the same log/linear interpolation as Prometheus' histogram_fraction. Signed-off-by: pedrampdd <eragon.pedy@gmail.com>
58da3de to
2e51e8f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1824.
cc @kakkoyun — picking up the proposal from the issue (no PR had been opened for it yet).
What
GatherAndComparecompares metrics via the text exposition format, which carries no representation of native (exponential) histogram buckets — only count and sum. So there is currently no way intestutilto assert on a native histogram's contents. This adds fourGatherer-based helpers:These cover the four capabilities the issue asks for: that a native histogram was gathered, its count, its sum, and the number of observations in an interval.
Design notes
GatherAndCompare/GatherAndCountfamily. (The issue floatedAssertNativeHistogram*; glad to rename if you prefer.)prometheus.Labelsto pick a series, so it works withHistogramVec.nilmeans "no filter", but it still errors if the name resolves to more than one series, so the selected series is unambiguous.(lower, upper]interval via cumulative rank with log2 interpolation for regular buckets and linear interpolation for the zero bucket — the same approach as PromQLhistogram_fraction. Because buckets are exponential it is an estimate, hence thetolerance; aligning the bounds to bucket edges gives an exact result. Total count, by contrast, is exact (no tolerance).Gatherer-only for now (noCollectAndAssert*siblings) to keep the surface small; easy to add the symmetry withCollectAndCompareif wanted.Tests
White-box tests build real native histograms via
NewHistogram/NewHistogramVecand assert through the public API, plus a stubGathererto exercise the float-histogram path (no public client API produces those). Covered: integer and float histograms;HistogramVeclabel selection, ambiguity, and no-match; classic-histogram and non-histogram rejection; zero observations; an observation at exactly0; negative observations; and both boundary-aligned (exact) and straddling (interpolated — quarter/half-bucket and negative-bucket) interval counts.go test -raceis green;gofmtandgo vetare clean.