prometheus/prometheus
Native histograms
Native histograms are a sparse, exponential-bucket histogram representation that fit one observation into a single sample. They replace the classical _bucket / _count / _sum triple-series scheme and dramatically reduce cardinality for high-resolution histograms.
Touchpoints
- Type:
model/histogram/histogram.go(integer counts) andfloat_histogram.go(float counts). - Encoding:
tsdb/chunkenc/histogram.goandfloat_histogram.go. - Wire format:
prompb/types.proto(RW1 + RW2) andprompb/io/prometheus/write/v2/types.proto. - Scrape:
model/textparse/protobufparse.gois the recommended path (Prometheus protobuf format). OpenMetrics 1.0 also supports them via_bucket-> NHCB conversion. - Engine:
promql/quantile.go,promql/info.go,promql/engine.go— most operators handle*histogram.Histogramand*histogram.FloatHistogram. - API:
web/api/v1/api.goreturns histograms in thevalue/valuesfield as[ts, hist]with the histogram serialised in the JSON codec.
Anatomy of a native histogram sample
Histogram {
Count: total observations
Sum: sum of observations
Schema: int that controls bucket boundary spacing
ZeroThreshold: |value| below this counted in ZeroCount
ZeroCount: count for the zero bucket
PositiveSpans: sparse bucket layout
PositiveBuckets: per-span counts (delta-encoded)
NegativeSpans: ...
NegativeBuckets: ...
}Schema = 0 corresponds to ratio 2; schema = 8 to ratio 2^(2^-8) ≈ 1.0027 — finer schemas have more buckets but more compression headroom.
NHCB (custom buckets)
NHCB stores classical histogram boundaries (le="...") using the native histogram's encoding. The translator in util/convertnhcb/ consumes a stream of classical histogram samples per series and emits NHCB samples without buckets. The wire format is unchanged.
Operators
PromQL operators that act on histograms:
histogram_quantile(q, hist)—promql/quantile.go.histogram_count(hist),histogram_sum(hist),histogram_avg(hist),histogram_stddev(hist),histogram_stdvar(hist).histogram_fraction(lower, upper, hist).- Arithmetic:
+,-,*,/between histograms (where defined). - 3.11 added
</and>/(bucket trimming) andhistogram_quantiles(...)for variadic quantile.
Schema migrations
A series can change schema mid-stream (a target's --enable-feature=native-histograms flag flips, for example). The chunk encoder handles this by closing the current chunk and opening a new one with the new schema.
Storage size
Native histograms use one chunk per series, the same as float series. A typical scrape interval of 15s keeps about 240 samples per chunk. Memory consumption is roughly (n_buckets * 2 bytes) + overhead per sample.
Entry points for modification
- New operator: add to
promql/functions.go(registered viaparser/functions.go); make sure bothHistogramandFloatHistogrampaths are covered. - Schema change: must be additive — older Prometheus must still read the encoded chunk. Bump
tsdb/CHANGELOG.md. - Compactor/queries: check
tsdb/chunkenc/histogram_meta.gofor layout validation.
See Chunk encodings and PromQL engine.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.