feat(metrics): add protobuf and OpenMetrics text encoders and collection - #237
Merged
Merged
Conversation
ethanolchik
force-pushed
the
encoders-and-collect
branch
from
July 24, 2026 11:51
8e54c49 to
1a7e8f5
Compare
ethanolchik
force-pushed
the
encoders-and-collect
branch
2 times, most recently
from
July 24, 2026 14:43
b420c26 to
6a04141
Compare
ethanolchik
force-pushed
the
encoders-and-collect
branch
from
July 27, 2026 16:54
6a04141 to
1746521
Compare
ethanolchik
marked this pull request as ready for review
July 28, 2026 15:13
…omplete, non-empty names
ethanolchik
force-pushed
the
encoders-and-collect
branch
from
July 30, 2026 10:49
8e37dcf to
2622a74
Compare
fisherdarling
approved these changes
Jul 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR completes the path from registered metrics to Prometheus-compatible wire output:
MetricFamilymodel.The public API now exposes
collect,CollectionOptions,ServiceNameFormat,encode_to_protobuf, andencode_to_text.Collection
collectiterates over the global registry and encodes each registered metric into one or moreMetricFamilyvalues.CollectionOptionscontrols whether optional metrics are included and how a service name is represented:MetricPrefixprefixes metric family names, except metrics registered as unprefixed.LabelWithNameadds the service name as a label to every row.The resulting
Vec<MetricFamily>provides a common boundary between metric implementations and wire encoders.Encoding
encode_to_protobufserialises metric families as concatenated, length-delimited Prometheus protobuf messages. This preserves fields that OpenMetrics text cannot fully represent, including native histogram data.encode_to_textsupports:Info metrics have no dedicated metric type; they are ordinary gauge families valued 1 that carry their fields as labels. Tests pin this representation in both formats (the family name is unchanged (for example, build_info), the protobuf type is gauge, and text emits
# TYPE build_info gauge) so that the info-metric layer added in a later PR reproduces the legacy wire output exactly. This PR adds no way to define or report an info metric.The text encoder emits
# HELP,# TYPE, and# UNITmetadata and terminates output with# EOF. Empty# HELPlines are omitted from output.Histogram encoding:
f64::MAXbucket to+Inf.+Infbucket when missing.The encoder also handles metric and exemplar timestamps, escapes help text and label values, and emits
NaN,+Inf, and-Infusing OpenMetrics syntax.SummaryandGaugeHistogramfamilies are fully supported. Protobuf passes them through unchanged. In text, summaries emit theirquantilerows plus_sumand_count; gauge histograms emit_bucketrows plus_gsumand_gcount.For text output, families without a non-empty name or recognised type, and rows missing data for their declared type, are skipped and reported through the non-fatal collection error hook.