Skip to content

expfmt: keep the escaping parameter in ResponseFormat for all formats - #1046

Open
ismailtrm wants to merge 1 commit into
prometheus:mainfrom
ismailtrm:expfmt-response-format-escaping
Open

ismailtrm wants to merge 1 commit into
prometheus:mainfrom
ismailtrm:expfmt-response-format-escaping

Conversation

@ismailtrm

Copy link
Copy Markdown

Follow-up to #1035 (comment).

ResponseFormat keeps the escaping parameter only for text/plain; version=1.0.0. For protobuf and OpenMetrics it returns the bare format, so NewDecoder(body, ResponseFormat(h)) falls back to legacy name validation and rejects UTF-8 names that NewEncoder wrote with escaping=allow-utf-8:

Content-Type: application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=allow-utf-8
ResponseFormat: application/openmetrics-text; version=1.0.0; charset=utf-8
Decode: text format parsing error in line 1: invalid metric name "http.server.request.duration"

Prometheus asks for escaping=allow-utf-8 on OpenMetrics 1.0.0 in its default Accept header, so a client that passes that header on and decodes the response this way runs into it.

This reads the parameter the same way for protobuf, text 1.0.0 and OpenMetrics 1.0.0 / 2.0.0, using the value from mime.ParseMediaType so quoted values work too. Without the parameter the returned format is unchanged. For OpenMetrics 2.0 this also means that an explicit scheme other than allow-utf-8 is now honoured instead of dropped. Protobuf is included because NegotiateAccept and the encoder already emit the parameter for it.

Added ResponseFormat table cases and an encode/decode test with a UTF-8 name. The new tests fail without the change, and go test ./... and golangci-lint pass with it.

@roidelapluie

ResponseFormat kept the escaping parameter of the Content-Type only for
text/plain; version=1.0.0. For protobuf and OpenMetrics responses it
returned the bare format, so NewDecoder fell back to legacy name
validation and rejected UTF-8 metric names that NewEncoder had written
with escaping=allow-utf-8. Prometheus sends escaping=allow-utf-8 for
OpenMetrics 1.0.0 in its default Accept header, so such responses are
common.

Read the escaping parameter the same way for all formats that support it,
using the value parsed by mime.ParseMediaType so that quoted values work.
Without the parameter the returned format is unchanged.

Signed-off-by: Ismail Tarim <ismailtarim7@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 13, 2026 15:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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