Skip to content

[surfacers.prometheus] Keep distributions as one data point - #1516

Merged
manugarg merged 4 commits into
mainfrom
prom-structured-store
Oct 3, 2026
Merged

manugarg merged 4 commits into
mainfrom
prom-structured-store

Conversation

@manugarg

@manugarg manugarg commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Third part of native histogram support (#1480). This is a refactor of the Prometheus surfacer's in-memory store. The /metrics output 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, _count and _bucket data points when it was recorded. Now it's kept as one data point holding the distribution, and expanded into those lines when /metrics is 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 existing isTimestamped() to decide on timestamps. This replaces the three dataWriter closures that were set up in New(). It builds the output by appending to a preallocated 64KB buffer, written out in about 32KB chunks, instead of calling fmt.Fprintf for each line.
  • recordMetric() has one path for new and already known metric names.

Output changes

  • Stale buckets are gone. If a distribution's bucket set changes between updates, the old code kept writing the le series that were no longer there, with their old values, until they expired. That could make +Inf smaller 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.
  • Bucket order. Bucket lines of a histogram are always written in bucket order and from the same snapshot. Before, a new bucket (native distributions) was appended at the end of the series list.
  • Distribution and scalar with the same name and labels: the last write now wins, instead of both being written. The old output was already invalid in this case (a plain sample under # TYPE x histogram).

Things to know

  • The surfacer now keeps a distribution's data until scrape time, so a distribution must not change after it's written to the surfacers. All probes write a clone; there's a comment about it in record().
  • The "deleted N stale metric series" log line now counts a histogram as one series.
  • Scalar values are still stored as rendered strings, so float formatting is untouched.

Testing

New golden test, TestWriteDataGolden, compares the complete /metrics output, 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 in testdata/ were generated with the code before this change.

New benchmark, BenchmarkWriteData, scrapes about 4,400 lines (100 targets):

time/scrape bytes/scrape allocs/scrape
main 350µs 122KB 7,816
first commit (expansion at scrape time, fmt.Fprintf) 650µs 532KB 15,917
this PR 105µs 66KB 1

Also ran cloudprober locally with one explicit-bucket and one native-bucket latency distribution and checked the scraped output.

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
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.
@manugarg
manugarg merged commit f58293a into main Oct 3, 2026
13 checks passed
@manugarg
manugarg deleted the prom-structured-store branch October 3, 2026 01:45
@manugarg manugarg added this to the v0.14.7 milestone Oct 4, 2026
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.

1 participant