Skip to content

feat(metrics): introduce foundations-metrics with scalar metrics#232

Open
ethanolchik wants to merge 8 commits into
cloudflare:mainfrom
ethanolchik:metrics/scalar-metrics
Open

feat(metrics): introduce foundations-metrics with scalar metrics#232
ethanolchik wants to merge 8 commits into
cloudflare:mainfrom
ethanolchik:metrics/scalar-metrics

Conversation

@ethanolchik

@ethanolchik ethanolchik commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Overview

This pull request introduces the new foundations-metrics crate, which provides the initial scalar metric types and converts their values into the Prometheus protobuf data model.

It builds on foundations-metrics-registry, which continues to own the stable process-global registry and protobuf types. This separation allows metric implementations to evolve without destabilising the registry shared across Foundations versions.

New Crate: foundations-metrics

  • Added the crate's public API, module structure, and documentation.
  • Re-exported register, EncodeMetric, IntoMetrics, MetricFamily, and RegistrationMetadata, allowing callers to use the higher-level crate without importing registry APIs separately.

Scalar Metrics

  • Implemented Counter with u64 and f64 support.
  • Implemented Gauge with i64 (since prometheus_client v0.19.0), u64, and f64 support.
  • Implemented RangeGauge, which exports the current, minimum, and maximum values observed since the previous collection.
  • Implemented GaugeGuard, which increments a gauge when created and decrements it when dropped.
  • Scalar metrics own their atomic storage rather than wrapping prometheus-client. The spec allowed either approach; owned storage keeps the scalar API independent of upstream implementation details and defers the dependency until it is needed for native histograms, though it is still included in foundations-metrics/Cargo.toml.
  • Cloned metrics share their underlying atomic storage through Arc, preserving inexpensive handle semantics.
  • Built-in storage intentionally focuses on the 64-bit numeric types currently used by Foundations. The public CounterAtomic and GaugeAtomic traits allow additional storage implementations to be added without changing the metric API.

Registration and Encoding

  • Introduced NamedMetric, which associates metric storage with its exported name and help text. Metric handles intentionally contain only state because the metrics macro already owns registration metadata.
  • Added the internal EncodeMetricValue trait. Storage types encode protobuf families with relative names, while NamedMetric applies the registered base name and help text.
  • Scalar metrics emit an empty relative suffix. RangeGauge emits "", _min, and _max, allowing it to compose with NamedMetric and future labelled families without special handling.
  • Counter families are encoded without automatically appending _total, preserving existing Foundations output behaviour.

Workspace Updates

  • Registered foundations-metrics as a workspace member and workspace dependency.
  • Updated foundations-metrics-registry to export the protobuf Counter and Gauge message types.

ethanolchik and others added 8 commits July 13, 2026 14:37
Add shared atomic Counter storage with u64 and f64 support.

- Introduce EncodeMetricValue for storage encoding
- Add NamedMetric for registered names and help text
Expose the underlying atomic storage to preserve compatibility with the legacy Counter API.
- Replace the prometheus-client gauge wrapper with Foundations-owned storage and atomic traits. - Share floating-point atomic updates between counters and gauges.
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