Skip to content

feat(metrics): Add native histograms and Prometheus protobuf exposition - #856

Open
ethanolchik wants to merge 1 commit into
prometheus:mainfrom
ethanolchik:feat/native-histograms
Open

feat(metrics): Add native histograms and Prometheus protobuf exposition#856
ethanolchik wants to merge 1 commit into
prometheus:mainfrom
ethanolchik:feat/native-histograms

Conversation

@ethanolchik

Copy link
Copy Markdown

Applications currently cannot expose native histogram samples from this client. This adds opt-in native collection to Histogram and the Prometheus protobuf exposition needed to scrape it.

Fixes #576.

const registry = new client.Registry(
  client.Registry.PROMETHEUS_PROTOBUF_CONTENT_TYPE,
);
const histogram = new client.Histogram({
  name: 'request_duration_seconds',
  help: 'Request duration',
  nativeHistogramBucketFactor: 1.1,
  buckets: [],
  registers: [registry],
});

Existing histogram configurations retain classic behavior. Native histograms can retain explicit classic buckets for migration, or use buckets: [] for native-only protobuf output. Protobuf registry methods return a Buffer, represented as Uint8Array in the public TypeScript declarations.

The implementation includes:

  • Standard exponential schemas -4 through 8, positive and negative sparse buckets, a configurable zero bucket, and native exemplars. The default budget of 160 populated buckets reduces resolution as needed and remains a soft limit at schema -4.
  • Native JSON snapshots and sum, first, and omit aggregation through registries, clusters, and workers, including reconciliation of different schemas and zero thresholds.
  • Protobuf encoding for native histograms and existing metric types. protobufjs/light uses the checked-in lib/metrics.json descriptor; lib/metrics.proto and npm run generate-protobuf make its source and regeneration available.
  • Public types, regression tests, configuration and migration documentation, a runnable HTTP example, and changelog entries.

Validation performed locally:

  • 761 tests in 34 suites passed on Node 22.23.2, 24.21.0, and 26.4.0. The configured Bun CI command also passed.
  • The Linux check workflow passed through act on Node 24.21.0: ESLint, Prettier, and TypeScript.
  • npm run benchmarks completed on macOS/Node 26.4.0. A focused registry comparison against upstream, with increased sampling, found no significant regression above a 5% threshold; default-label cases measured roughly 2–4% overhead.
  • Manual interoperability checks with Prometheus 3.12.0 covered native counts, sums, quantiles, signed and empty histograms, worker aggregation, classic coexistence, and other metric types.
  • Protobuf descriptor regeneration matched the checked-in file, and the package dry run included the runtime schema and implementation files.

The existing benchmark suite covers classic metrics. Dedicated native workload benchmarks and application-specific rollout validation remain follow-up work. Applications select the protobuf response format themselves; HTTP Accept negotiation is outside this change.

Developed with AI assistance (Codex), also disclosed in the commit trailer.

Extend Histogram with opt-in exponential native buckets, exemplars, and
resolution reduction. Preserve native snapshots through registry, worker,
and cluster aggregation.

Add protobuf exposition for all existing metric types, binary registry
return types, schema generation, documentation, and an HTTP example.

Fixes prometheus#576

Assisted-by: Codex
Signed-off-by: Ethan Olchik <eolchik@cloudflare.com>
@ethanolchik ethanolchik changed the title Add native histograms and Prometheus protobuf exposition feat(metrics): Add native histograms and Prometheus protobuf exposition Sep 10, 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.

Support for native histograms

1 participant