Add OpenMetrics parser#904
Open
martincostello wants to merge 4 commits intoprometheus:mainfrom
Open
Conversation
Picking up from prometheus#710 to add an OpenMetrics parser to work towards support for OpenMetrics in promtool. Contributes to prometheus/prometheus#8932. Co-Authored-By: Yi <38248129+jyz0309@users.noreply.github.com> Signed-off-by: martincostello <martin@martincostello.com>
f6bc3c2 to
c8a254b
Compare
fd4e739 to
3349589
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Adds OpenMetrics text parsing support to expfmt to advance toward promtool OpenMetrics compatibility (building on the previously abandoned work referenced in #710).
Changes:
- Extend
ParseErrorto include the originating format (text vs openmetrics) and improve error messages. - Introduce a new
OpenMetricsParserwith comprehensive parser tests/benchmarks. - Add OpenMetrics handling to decoding and response Content-Type discrimination.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| expfmt/text_parse.go | Adds format-aware ParseError to distinguish text vs OpenMetrics parsing failures. |
| expfmt/openmetrics_parse.go | Implements the OpenMetrics text parser and associated parsing helpers/state machine. |
| expfmt/openmetrics_parse_test.go | Adds extensive OpenMetrics parsing and error-path tests plus benchmarks. |
| expfmt/decode.go | Adds OpenMetrics response format detection and a new OpenMetrics decoder implementation. |
| expfmt/decode_test.go | Adds decoder and header discrimination tests for OpenMetrics content types. |
Comments suppressed due to low confidence (1)
expfmt/decode.go:108
NewDecoderselects a validation scheme based onformat.ToEscapingScheme(), but the OpenMetrics decode path ignores that scheme entirely (openMetricsDecoderalways uses a zero-value OpenMetricsParser). This makes decoding behavior inconsistent with the comment above and with the text decoder (which usesNewTextParser(d.s)).
Consider threading the chosen model.ValidationScheme into OpenMetricsParser (and using it to validate quoted/UTF-8 names) so decoding honors escaping=... consistently.
func NewDecoder(r io.Reader, format Format) Decoder {
scheme := model.LegacyValidation
if format.ToEscapingScheme() == model.NoEscaping {
scheme = model.UTF8Validation
}
switch format.FormatType() {
case TypeProtoDelim:
return &protoDecoder{r: bufio.NewReader(r), s: scheme}
case TypeOpenMetrics:
return &openMetricsDecoder{r: r}
case TypeProtoText, TypeProtoCompact:
return &errDecoder{err: fmt.Errorf("format %s not supported for decoding", format)}
}
return &textDecoder{r: r, s: scheme}
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Address code review comments. Signed-off-by: martincostello <martin@martincostello.com>
This was referenced May 3, 2026
Keep OpenMetrics info samples such as `target_info` attached to the base MetricFamily declared by `# TYPE target info` rather than creating a separate `target_info` family. Add parser and decoder regression tests for the base-family info form. Signed-off-by: martincostello <martin@martincostello.com>
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.
Picking up from #710 to add an OpenMetrics parser to work towards support for OpenMetrics in promtool.
This PR was mostly written by Copilot by pointing it at the issue, using code from the abandoned PR above and addressing review comments.
I don't usually work with Go, so I've relied on the tests and Copilot for review.
One of the previous PR comments noted this was quite a large change and could be split up into smaller changes. Happy to do that based on feedback on what the sensible logical units are.
Contributes to prometheus/prometheus#8932.