Skip to content

Add OpenMetrics parser#904

Open
martincostello wants to merge 4 commits intoprometheus:mainfrom
martincostello:gh-prometheus/8932
Open

Add OpenMetrics parser#904
martincostello wants to merge 4 commits intoprometheus:mainfrom
martincostello:gh-prometheus/8932

Conversation

@martincostello
Copy link
Copy Markdown

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.

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>
Fix lint warnings.

Signed-off-by: martincostello <martin@martincostello.com>
@martincostello martincostello marked this pull request as ready for review May 3, 2026 18:58
Copilot AI review requested due to automatic review settings May 3, 2026 18:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ParseError to include the originating format (text vs openmetrics) and improve error messages.
  • Introduce a new OpenMetricsParser with 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

  • NewDecoder selects a validation scheme based on format.ToEscapingScheme(), but the OpenMetrics decode path ignores that scheme entirely (openMetricsDecoder always uses a zero-value OpenMetricsParser). This makes decoding behavior inconsistent with the comment above and with the text decoder (which uses NewTextParser(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.

Comment thread expfmt/decode.go Outdated
Comment thread expfmt/openmetrics_parse.go
Comment thread expfmt/openmetrics_parse.go
Comment thread expfmt/openmetrics_parse.go Outdated
Comment thread expfmt/openmetrics_parse.go
Comment thread expfmt/text_parse.go Outdated
Comment thread expfmt/decode.go
Address code review comments.

Signed-off-by: martincostello <martin@martincostello.com>
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>
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.

2 participants