Skip to content

feat(metrics): add support for classic, time, and native histograms.#236

Open
ethanolchik wants to merge 10 commits into
cloudflare:mainfrom
ethanolchik:metrics/histograms
Open

feat(metrics): add support for classic, time, and native histograms.#236
ethanolchik wants to merge 10 commits into
cloudflare:mainfrom
ethanolchik:metrics/histograms

Conversation

@ethanolchik

@ethanolchik ethanolchik commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Overview

Adds classic, time, and native histogram metric types to foundations-metrics, including protobuf encoding, constructors, family support, TimeHistogram snapshots, and timer behaviour.

Changes

Classic histograms

Adds an owned Histogram backed by shared, mutex-protected storage.

  • Histogram::new accepts inclusive bucket bounds and appends the legacy terminal f64::MAX bucket.
  • Cloned handles share observations.
  • observe updates the sample sum, sample count, and the first matching bucket.
  • Sorted buckets use a binary search while unsorted buckets retain the legacy linear-search behaviour.
  • NaN observations preserve legacy behaviour by updating sum/count without incrementing a bucket.
  • Protobuf output contains a histogram row with cumulative bucket counts, sample count, and sample sum.

Time histograms

Adds the lock-free TimeHistogram and HistogramTimer APIs previously provided by prometools.

  • Observations are stored atomically in nanoseconds and exported in seconds.
  • start_timer records elapsed time when the timer is stopped or dropped.
  • Timers support pause, resume, stop_and_record, and stop_and_discard.
  • A timer records at most once.
  • TimeHistogram::snapshot exposes the current sum, count, and non-cumulative buckets through HistogramSnapshot.

Histogram construction

Adds HistogramBuilder as a MetricConstructor for both Histogram and TimeHistogram.

This allows configured histograms to be created by Family and by generated metric constructors while preserving the existing #[ctor = HistogramBuilder { ... }] shape.

Native histograms

Adds NativeHistogram, backed by prometheus-client 0.25's native histogram implementation.

  • NativeHistogram::observe records values into sparse exponential buckets.
  • Cloned handles share the same storage.
  • NativeHistogramBuilder configures bucket growth factor, zero-bucket threshold, and the best-effort maximum number of sparse buckets.
  • The builder implements MetricConstructor, allowing native histograms to be used in labelled Family metrics.
  • Native histogram encoding delegates to prometheus-client because its sparse bucket state is private.
  • The resulting protobuf families are converted into the canonical foundations-metrics-registry model, including sample count/sum, schema, zero bucket, positive and negative spans, and bucket deltas.
  • Encoding errors are reported through the existing non-fatal collection diagnostic hook and skip only the affected histogram.

Public API

Exports the following from foundations-metrics:

  • Histogram
  • HistogramBuilder
  • HistogramSnapshot
  • HistogramTimer
  • TimeHistogram
  • NativeHistogram
  • NativeHistogramBuilder

The registry crate also re-exports the protobuf Bucket type needed by histogram encoders.

@ethanolchik
ethanolchik marked this pull request as draft July 20, 2026 17:25
Add owned fixed-bucket storage, direct protobuf encoding, and support for the legacy HistogramBuilder API (only classic histograms in this commit).
@ethanolchik
ethanolchik marked this pull request as ready for review July 21, 2026 15:32
Comment thread foundations-metrics/src/metrics/histogram.rs Outdated
Comment thread foundations-metrics/src/metrics/histogram.rs
Comment thread foundations-metrics/src/metrics/histogram.rs Outdated
Choose to pre-sort histogram buckets over a sorted boolean variable.

Also satisfies cargo clippy & cargo fmt
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.

2 participants