Repository navigation
[surfacers.prometheus] Keep distributions as one data point - #1516
Merged
Merged
Conversation
So far we expanded a distribution into _sum, _count and _bucket data points while recording it. Keep the distribution as a single data point instead, and expand it while writing the /metrics output. This doesn't change the /metrics output. It's in preparation for exporting native histograms (#1480), where a histogram is a single series. Add a golden test for the complete /metrics output. Golden files were generated before this change.
Build the output by appending to a buffer, and write it out in chunks of about 32KB. This avoids allocating for each line: fmt.Fprintf boxes its arguments, and histogram lines needed their keys built at scrape time. With BenchmarkWriteData (about 4,400 lines), writeData allocations go from 15,917 to 20 per scrape, and time from 650us to 118us. Before the previous commit, it was 7,816 allocations and 350us.
manugarg
marked this pull request as ready for review
October 3, 2026 01:22
…stributions - Preallocate a 64KB buffer for writing /metrics output. This makes it a single allocation per scrape. - Note that recorded distributions must not change afterwards, as we now keep their data until scrape time. - Simplify the golden test's EventMetrics helper.
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.
Third part of native histogram support (#1480). This is a refactor of the Prometheus surfacer's in-memory store. The
/metricsoutput doesn't change, except in the cases listed under "Output changes". It prepares for protobuf exposition, where a native histogram goes out as a single series.So far, a distribution was expanded into separate
_sum,_countand_bucketdata points when it was recorded. Now it's kept as one data point holding the distribution, and expanded into those lines when/metricsis written.record()stores a distribution with one call; the loop that built a key per bucket is gone.writeData()expands histograms while writing, and uses the existingisTimestamped()to decide on timestamps. This replaces the threedataWriterclosures that were set up inNew(). It builds the output by appending to a preallocated 64KB buffer, written out in about 32KB chunks, instead of callingfmt.Fprintffor each line.recordMetric()has one path for new and already known metric names.Output changes
leseries that were no longer there, with their old values, until they expired. That could make+Infsmaller than a lower bucket. Now only the current buckets are written. This happens with native distributions when exported as gauges, since a delta drops empty buckets.# TYPE x histogram).Things to know
record().Testing
New golden test,
TestWriteDataGolden, compares the complete/metricsoutput, including line order, in all three timestamp modes. It covers counters, floats, maps, strings, gauges and histograms, with multiple label sets, a series without labels, and an update to an existing series. The golden files intestdata/were generated with the code before this change.New benchmark,
BenchmarkWriteData, scrapes about 4,400 lines (100 targets):fmt.Fprintf)Also ran cloudprober locally with one explicit-bucket and one native-bucket latency distribution and checked the scraped output.