Skip to content

[metrics] Add native (sparse exponential) buckets to Distribution - #1514

Merged
manugarg merged 4 commits into
mainfrom
native-hist-metrics
Oct 1, 2026
Merged

manugarg merged 4 commits into
mainfrom
native-hist-metrics

Conversation

@manugarg

@manugarg manugarg commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

First part of native histogram support (#1480). This adds Prometheus-style native buckets to metrics.Distribution. No surfacer exports native histograms yet; that comes in follow-up PRs (OTel exponential histograms, then protobuf exposition in the Prometheus surfacer).

latency_distribution {
  native_buckets {}   # schema: 3
}
  • Config — native_buckets { schema } is a third option in the Dist buckets oneof. Bucket boundaries are powers of 2^(2^-schema); schema defaults to 3 (about 9% wide buckets) and must be in -4..8.
  • Recording — bucket indexing follows client_golang (math.Frexp plus a bounds table), so values on a boundary land in the same bucket as they would there. The bounds table is copied into metrics/dist_native_bounds.go with the Prometheus license header. Only buckets with samples are stored. Exact zeros are counted in a separate zero bucket.
  • Add / SubtractCounter / Clone — native and regular distributions don't mix, and neither do native ones with a different schema. Subtract removes buckets that drop to zero.
  • String form — dist:sum:53.7|count:50|schema:3|zc:0|pb:-12=2,-9=1,0=3. zc is the zero count, and pb/nb are positive/negative buckets as <index>=<count>, left out when empty. ParseDistFromString reads it back, so file surfacer output and external probe payloads work. Bucket indexes outside what the schema can produce are rejected.
  • Data() — returns a regular-bucket view of the native buckets (-Inf, 0, then every bucket from the smallest to the largest filled one), so existing surfacers keep working unchanged. It also has a new Native field with the sparse buckets for the surfacers that will export them natively.
  • Stackdriver — skips native distributions and logs a warning once.
  • Cloudwatch — calls Data() once per distribution instead of once per bucket.
  • Status page — shows native(schema=3) in place of the lower bounds.

Things to know

  • native_buckets is marked experimental in the proto: the option and its fields can change.
  • With the Prometheus text format, a native distribution currently shows up as one le series per bucket in the filled range, and that set grows as new buckets get samples. The plan is to emit only _sum, _count and +Inf in text once protobuf exposition is in.
  • No zero_threshold option: the zero bucket holds exact zeros only. It can be added later if someone needs it.
  • Native buckets include their upper bound, while regular buckets include their lower bound. The Data() view ignores this; it only matters for values exactly on a boundary.
  • dist.pb.go was regenerated for this proto alone (protoc 33.5, protoc-gen-go v1.36.12).

Testing

Unit tests cover bucket boundaries for all schemas, add/subtract/reset, string round trip and malformed input, the Data() view and the stackdriver skip. Also ran cloudprober locally with an HTTP probe using native_buckets {} and checked the Prometheus surfacer, file surfacer and status page output.

Add Prometheus-style native histogram buckets as a third bucket type for
distributions, configured through "native_buckets" in the dist proto:

  latency_distribution { native_buckets {} }

Bucket boundaries are powers of 2^(2^-schema), and only the buckets that
have samples are stored. Bucket indexing and the bounds table are taken
from prometheus/client_golang.

Native buckets live inside metrics.Distribution, so Add, SubtractCounter,
Clone, String and ParseDistFromString keep working everywhere. Data()
returns a regular-bucket view of the native buckets for the surfacers
that don't handle them natively, along with the native buckets
themselves. The stackdriver surfacer skips native distributions.

This is the first part of native histogram support (#1480). Surfacers
don't export native histograms yet.
Comment thread metrics/proto/dist.proto Resolved
Comment thread metrics/dist_native_bounds.go Resolved
- Reject bucket keys that are outside the range that float64 values can
  map to. Keys come from outside through ParseDistFromString (probe
  payloads), and Data() builds a view that's as big as the range of the
  keys, so a single payload line could make it allocate GBs of memory.
- Drop zero_threshold. It did nothing at its default of 0, and the
  regular buckets view was not right for other values. Zero bucket now
  counts exact zeros only.
- Use one type, NativeBuckets, for the internal state and for
  DistributionData.Native.
- Check native compatibility before lower bounds, so that mixing
  regular and native distributions gives the right error.
- cloudwatch: call Data() once per distribution, not once per bucket.
- stackdriver: warn about skipped native distributions only once.
- Don't accept empty "pb:" and "nb:" tokens; we never write them.
@manugarg
manugarg force-pushed the native-hist-metrics branch from 07291e5 to 628401e Compare October 1, 2026 23:41
@manugarg
manugarg marked this pull request as ready for review October 1, 2026 23:52
@manugarg
manugarg merged commit 7e7ec53 into main Oct 1, 2026
13 checks passed
@manugarg
manugarg deleted the native-hist-metrics branch October 1, 2026 23:58
@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