Skip to content

feat(metrics): Implement exemplar metrics - #239

Draft
ethanolchik wants to merge 29 commits into
cloudflare:mainfrom
ethanolchik:metrics/exemplar-metrics
Draft

feat(metrics): Implement exemplar metrics#239
ethanolchik wants to merge 29 commits into
cloudflare:mainfrom
ethanolchik:metrics/exemplar-metrics

Conversation

@ethanolchik

Copy link
Copy Markdown
Contributor

Overview

Adds first-party exemplar-aware metric types for counters, classic histograms, and native histograms while leaving existing metric types unchanged.

Public API

  • CounterWithExemplar
  • HistogramWithExemplars
  • NativeHistogramWithExemplars
  • Read-only Exemplar accessors for labels and sampled values are exposed through CounterWithExemplar::get.

These types integrate with Family, registration, collection, and the existing protobuf and OpenMetrics encoders.

Recording Semantics

  • Counters retain the exemplar from the latest labeled increment. An unlabelled increment clears it.
  • Classic histograms retain the latest labeled observation for each bucket. Unlabelled observations do not clear existing exemplars.
  • Native histograms retain the latest labeled observation and capture its timestamp.
  • Clones share metric and exemplar storage.
  • Family series labels remain separate from exemplar labels.
    Recording does not require exemplar labels to implement Serialize; serialisation occurs when the metric is encoded.

Encoding

Counter and classic-histogram exemplars are emitted in both protobuf and OpenMetrics text.
Native-histogram exemplars:

  • Include the timestamp required by the Prometheus protobuf model.
  • Are preserved in protobuf output.
  • Are not emitted in text because native histograms require protobuf exposition.

Empty counter and classic-histogram exemplar label sets remain valid and are emitted in text as {}.

Labels And Validation

Exemplar labels use the existing label serialiser and support:

  • Serialisable structs
  • Unit label sets
  • Legacy sequences of name-value pairs

Invalid, duplicate, or unserialisable exemplar labels drop only the exemplar. The metric sample remains available, and a non-fatal diagnostic is emitted.

Native-histogram exemplars without the required timestamp are removed with a non-fatal diagnostic; the native histogram itself is still emitted.

@ethanolchik
ethanolchik force-pushed the metrics/exemplar-metrics branch from 364b7a6 to 723b5a3 Compare July 24, 2026 14:43
… names

Legacy metric and label names must match Prometheus' name grammars. Names outside these grammars cause Prometheus to reject the scrape, and the grammars exclude non-ASCII characters.

Foundations now quotes label names and uses OpenMetrics' quoted metric-name syntax when required. This allows UTF-8 and other nonstandard names without affecting legacy compatibility.
Add registerable counter, classic histogram, and native histogram
types that retain exemplars alongside observations.

Support typed and legacy label sets, Family usage, OpenMetrics text,
and Prometheus protobuf encoding. Validate exemplars during collection
and report serialisation failures as non-fatal diagnostics.
@ethanolchik
ethanolchik force-pushed the metrics/exemplar-metrics branch from 723b5a3 to ea4b084 Compare July 27, 2026 16:55
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