Skip to content

[surfacers.prometheus] Add protobuf exposition, with native histograms - #1517

Merged
manugarg merged 7 commits into
mainfrom
prom-protobuf
Oct 4, 2026
Merged

manugarg merged 7 commits into
mainfrom
prom-protobuf

Conversation

@manugarg

@manugarg manugarg commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Fourth part of native histogram support (#1480), after #1514, #1515 and #1516. With this, Prometheus can scrape native histograms from cloudprober directly.

# Prometheus
scrape_configs:
  - job_name: cloudprober
    scrape_native_histograms: true
# cloudprober
latency_distribution { native_buckets {} }
  • When we serve protobuf — /metrics serves the protobuf format (application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited) only if both of these are true:
    • The surfacer has seen a native histogram. Until a distribution with native buckets shows up, we serve the text format regardless of the Accept header, as we always have. See "No change without native histograms" below.
    • The scraper prefers protobuf over text in the Accept header, honouring q values. Prometheus asks for it first when scrape_native_histograms is on.
  • Protobuf output — one length-delimited MetricFamily per 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.
    • Classic histograms have the same cumulative buckets as the text format; the +Inf bucket is implied.
    • Native histograms have schema, zero count, and spans and deltas for positive and negative buckets. A native histogram with no samples gets an empty span, like client_golang does, so Prometheus doesn't take it for a classic one.
  • Same values in both formats — scalar values in protobuf are parsed from the values we write in the text format, so floats that the text format rounds come out the same in both.
  • Labels — data points keep their labels as name, value pairs (shared by all the data points of an EventMetrics, plus one extra label for map and string metrics), and label pairs for protobuf are built from them at scrape time. So the store costs about the same whether or not anybody scrapes protobuf. The text format labels are now built once for an EventMetrics, instead of once for each of its metrics.

Cost

Measured with 100 targets (500 EventMetrics, 1,700 series, about 3,700 text lines):

main this PR
record 500 EventMetrics 12,400 allocs, 587KB 6,300 allocs, 335KB
memory per series 211 bytes 271 bytes
text scrape 105µs, 1 alloc 120µs, 1 alloc
protobuf scrape n/a 0.64ms, 2,108 allocs
protobuf scrape, 400 native histograms (50 buckets, 9 spans each) n/a 1.15ms, 3,612 allocs

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. BenchmarkWriteProtobuf covers both.

Compared with client_golang

For reference, the official Go client (client_golang v1.24.1, promhttp handler, compression off) on a workload of the same shape: 1,700 series with 4 labels, 400 of them histograms with 3 buckets.

client_golang this PR
text scrape 1.8ms, 14,450 allocs 0.12ms, 1 alloc
protobuf scrape 1.75ms, 12,079 allocs 0.64ms, 2,108 allocs

It's not the same code path on both sides: client_golang's numbers include its HTTP handler and registry checks, ours are for writeData and writeProtobuf. 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

  • Text format, native histograms only — they now have only _sum, _count and the +Inf bucket. 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, so histogram_quantile won't work there. native_buckets is experimental, so this only affects people trying it out.
  • No change for anything else in the text format; TestWriteDataGolden passes unchanged.

Things to know

  • Accept header — media types we can't serve (e.g. OpenMetrics) are ignored, so it's always the better of protobuf and text.
  • Protobuf is tied to data, not config — the surfacer doesn't see probe configs, so it goes by the distributions it receives. The format can switch from text to protobuf at runtime, when the first native distribution arrives. Text had everything until then, so nothing is lost.
  • Label values with backslashes, quotes or newlines — the text format has never escaped label values, so Prometheus unescapes backslashes in them, and fails the scrape on a quote or a newline. Protobuf carries the raw value, which is the correct one. So such a label value can differ between a text and a protobuf scrape.
  • Metric names with both numbers and distributions — this is common: all probes export latency, and it's a distribution only for the probes that have latency_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.
  • Gauge (delta) distributions go out as regular histograms, same as in the text format.

Dependency

github.com/prometheus/client_model v0.6.2, for the generated protobuf types only. This version was already in the module graph, so no other dependency changes. No client_golang or prometheus/common.

Testing

  • Unit tests for Accept parsing (with the headers Prometheus 3.15 sends with and without native histogram scraping, and text preferred over protobuf by q), 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.
  • Ran this build with Prometheus 3.15 and three probes that all export latency: one with native_buckets {}, one with explicit buckets, and one without latency_distribution. With scrape_native_histograms: true, the first was stored as one native histogram series, the second as classic latency_bucket series with the same le values as a text scrape, and the third as a plain series. A text scrape of the same got the same, except only _sum, _count and +Inf for the native one. No scrape errors or warnings. histogram_quantile works on both kinds.

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.
- 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
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.
@manugarg
manugarg merged commit 2bbee6e into main Oct 4, 2026
24 checks passed
@manugarg
manugarg deleted the prom-protobuf branch October 4, 2026 00:28
@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