Repository navigation
[surfacers.prometheus] Add protobuf exposition, with native histograms - #1517
Merged
Merged
Conversation
Serve /metrics in the Prometheus protobuf format when the scraper prefers it in the Accept header. Prometheus does that when it's configured to scrape native histograms (scrape_native_histograms: true). Otherwise we keep serving the text format as before. Distributions with native buckets are exported as native histograms in the protobuf format. In the text format, they now have only _sum, _count and the +Inf bucket, as native histograms can have hundreds of buckets. Scalar values in the protobuf format are parsed from the values we write in the text format, so that both formats report the same values. Uses github.com/prometheus/client_model for the protobuf messages. Part of #1480.
Storing label pairs with each data point cost 9,000 more allocations per 500 EventMetrics recorded (21,400 vs 12,400), and 161 more bytes per series (372 vs 211), whether or not anybody scraped the protobuf format. Keep the `name="value"` label strings that record() builds anyway, and build label pairs from them while writing the protobuf format. Also, don't allocate a data point on the heap for each update. Recording is now 10,800 allocations per 500 EventMetrics, and a series takes 259 bytes.
… series While writing the protobuf format, allocate label pairs, metric and value messages for all the data points of a metric in one go, and buckets for all the buckets of a histogram in one go. Earlier we allocated each message, and each name, value and number in it, separately. For 1,700 series, a protobuf scrape goes from 30,212 allocations and 1.05ms to 2,094 allocations and 0.70ms.
manugarg
force-pushed
the
prom-protobuf
branch
from
October 3, 2026 07:04
2ccccaa to
afe9b33
Compare
- Native histograms: allocate for all the spans of a histogram in one go, and preallocate keys and deltas. A protobuf scrape of 400 native histograms (50 buckets, 9 spans each) goes from 14,410 allocations to 3,610. - Accept header: ignore the media types we can't serve, e.g. OpenMetrics, so that we pick the better of protobuf and text. - Skip a distribution recorded under a metric name that's not a histogram, same as we skip a number under a histogram's name. Prometheus reads such a distribution as 0. - Don't allocate values for histogram metrics. - Add BenchmarkWriteProtobuf, for classic and native histograms.
manugarg
marked this pull request as ready for review
October 3, 2026 07:39
…lue" Keep data point labels as name, value pairs. So far we kept them as `name="value"` strings, the way they appear in the text format, and split them while writing the protobuf format. With that, we don't build a string for each label. We now also build the text format labels once for an EventMetrics, instead of joining them for each metric's data key. Recording 500 EventMetrics goes from 10,800 allocations and 490KB to 6,300 allocations and 335KB.
…o families A metric name can have both numbers and distributions. For example, all probes export latency, and it's a distribution only for the probes that have latency_distribution configured. In the protobuf format, we were writing only the data points that matched the metric's type, the type of the first value seen, and dropping the rest. So a protobuf scrape was missing latency for either the probes with latency_distribution or the probes without it. Write distributions and numbers as two metric families with the same name instead: a histogram one, and a counter, gauge or untyped one. Prometheus accepts that. Also split writeProtobuf, use Prometheus's actual Accept headers in the test, and fix a couple of comments.
Many Prometheus setups already prefer the protobuf format: it's what Prometheus asks for from all the targets once native histograms are enabled. With protobuf exposition, all of them would've started getting protobuf from cloudprober on upgrade, whether or not they use native histograms in cloudprober. Protobuf gives them nothing without native histograms, as the text format has the same data, and it costs us more: about 5 times the CPU of a text scrape, and 20 times the memory. It would also expose them to a new code path for no reason. So, serve the protobuf format only after the surfacer has seen a distribution with native buckets. Until then, serve the text format, as we always have, regardless of the Accept header.
This was referenced Oct 4, 2026
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.
Fourth part of native histogram support (#1480), after #1514, #1515 and #1516. With this, Prometheus can scrape native histograms from cloudprober directly.
/metricsserves the protobuf format (application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited) only if both of these are true:Acceptheader, as we always have. See "No change without native histograms" below.Acceptheader, honouringqvalues. Prometheus asks for it first whenscrape_native_histogramsis on.MetricFamilyper metric, in the same order as the text format. Counters, gauges, untyped metrics, maps and strings are all covered, with timestamps following the same rules as the text format.+Infbucket is implied.Cost
Measured with 100 targets (500 EventMetrics, 1,700 series, about 3,700 text lines):
The protobuf writer allocates label pairs, metric and value messages for all the series of a metric in one go, and buckets for a histogram in one go, instead of allocating each message and each field value separately. That took a protobuf scrape from 30,212 allocations to 2,108. Spans of a native histogram are allocated in one go as well.
BenchmarkWriteProtobufcovers both.Compared with client_golang
For reference, the official Go client (
client_golangv1.24.1,promhttphandler, compression off) on a workload of the same shape: 1,700 series with 4 labels, 400 of them histograms with 3 buckets.It's not the same code path on both sides: client_golang's numbers include its HTTP handler and registry checks, ours are for
writeDataandwriteProtobuf. The difference comes from how they work. client_golang keeps label pairs as protobuf messages for each series, builds a new protobuf message tree for all the series on each scrape (about 3 allocations per series), and renders the text format from that tree as well. We write the text format straight from the store, and build the protobuf tree with a few allocations per metric.No change without native histograms
Many Prometheus setups already ask all their targets for protobuf first: that's what enabling native histograms does, and it'll be the default in Prometheus 4. If we served protobuf whenever asked, all of them would start getting it from cloudprober on upgrade, with nothing to gain: without native histograms the text format has the same data, and protobuf costs us about 5 times the CPU and 20 times the memory per scrape (see the cost table). They'd also be on a new code path for no reason.
So a cloudprober that has no distribution with native buckets keeps serving the text format, exactly as before. The first native distribution the surfacer records turns protobuf on (logged once), and it stays on.
Output changes
_sum,_countand the+Infbucket. Native histograms can have hundreds of buckets; scrape protobuf to get them. A scraper that doesn't ask for protobuf gets no usable buckets for them, sohistogram_quantilewon't work there.native_bucketsis experimental, so this only affects people trying it out.TestWriteDataGoldenpasses unchanged.Things to know
latency, and it's a distribution only for the probes that havelatency_distribution. In protobuf, such a metric goes out as two metric families with the same name: a histogram one for the distributions, and one for the numbers. The second one's type is counter or gauge, or untyped if the first value seen for the name was a distribution. Prometheus 3.15 accepts this and stores all the series.Dependency
github.com/prometheus/client_modelv0.6.2, for the generated protobuf types only. This version was already in the module graph, so no other dependency changes. Noclient_golangorprometheus/common.Testing
Acceptparsing (with the headers Prometheus 3.15 sends with and without native histogram scraping, and text preferred over protobuf byq), span/delta encoding with gaps and negative indexes, the native histogram text output, the protobuf output (all the metric families and their types, and the messages of 7 of the 9 families), a metric name with both numbers and distributions, and scrapes through the HTTP handler: text for a protobuf request when there are no native histograms, and both formats when there are.latency: one withnative_buckets {}, one with explicit buckets, and one withoutlatency_distribution. Withscrape_native_histograms: true, the first was stored as one native histogram series, the second as classiclatency_bucketseries with the samelevalues as a text scrape, and the third as a plain series. A text scrape of the same got the same, except only_sum,_countand+Inffor the native one. No scrape errors or warnings.histogram_quantileworks on both kinds.