Repository navigation
promhttp: vary negotiated metrics responses - #2142
Merged
Merged
Conversation
Signed-off-by: lowgame <128625014+lowgame@users.noreply.github.com>
Signed-off-by: lowgame <128625014+lowgame@users.noreply.github.com>
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The cache negotiation fix is correct, backward-compatible, and adequately tested.
Review effort: Balanced
Findings: None
What changed in this PR
Adds cache-correct Vary headers to negotiated Prometheus metrics responses.
Changes:
- Varies responses by
Acceptand, when enabled,Accept-Encoding. - Adds regression coverage for enabled and disabled compression.
| File | Description |
|---|---|
prometheus/promhttp/http.go |
Adds negotiated-response Vary headers. |
prometheus/promhttp/http_test.go |
Tests Vary behavior across compression modes. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
kakkoyun
approved these changes
Oct 3, 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.
@bwplotka
Reproduction
promhttp.HandlerFornegotiates its response media type fromAcceptand, when compression is enabled, its content encoding fromAccept-Encoding. However, successful responses did not include aVaryheader. A shared HTTP cache could therefore reuse an OpenMetrics or compressed representation for a client that negotiated a different representation.The added
TestHandlerSetsVaryHeaderfailed on the previous implementation with an emptyVaryheader in both the compression-enabled and compression-disabled cases.Root cause
The handler set the negotiated
Content-Typeand optionalContent-Encoding, but did not describe the request headers used to select those values.Solution
Add
Vary: Acceptto negotiated metrics responses and addVary: Accept-Encodingwhen compression negotiation is enabled.Header.Addpreserves values installed by outer middleware. The regression test also verifies that disabling compression omitsAccept-EncodingfromVary.Tests
go test ./prometheus/promhttp -run '^TestHandlerSetsVaryHeader$' -count=1— passgo test ./prometheus/promhttp -count=1— passmake common-lint— pass, 0 issuesgo test ./...— affected package passes; the command retains pre-existing failures inprometheusandprometheus/collectorsbecause two expected Go runtime metrics are absent with the installed toolchain. The same focused failures reproduce at upstream commit589113c.