Set Content-Type for demo plugin metrics endpoint - #271
Merged
popduke merged 2 commits intoAug 10, 2026
Conversation
popduke
marked this pull request as ready for review
August 10, 2026 07:38
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.
Background
I ran into this issue after Prometheus was upgraded in an environment at work.
Prometheus could reach the demo plugin metrics endpoint, but the scrape target
was marked as down with the following error:
The response body contained valid metrics. The scrape failed because the demo
plugin did not set a
Content-Typeresponse header, and Prometheus did not havea
fallback_scrape_protocolconfigured for the target.Starting with Prometheus 3.0, scrape targets must return a valid and supported
Content-Type. Prometheus 2.x previously treated a missing or unrecognizedContent-Type as Prometheus text format by default.
Reference:
https://prometheus.io/docs/prometheus/latest/migration/#scrape-protocols
Change
Set the metrics response header to the Prometheus text exposition format:
Content-Type: text/plain; version=0.0.4; charset=utf-8The header is set using
TextFormat.CONTENT_TYPE_004, which matches the outputformat produced by
PrometheusMeterRegistry.scrape().The response body is also encoded explicitly as UTF-8. The encoded byte array
is reused when calculating
Content-Lengthand writing the response body,avoiding repeated conversion with the platform default charset.
Compatibility
Prometheus text format 0.0.4 has been supported since Prometheus 0.4.0.
This change fixes metrics scraping with Prometheus 3.0 and later without
requiring
fallback_scrape_protocol, while remaining compatible with olderPrometheus versions, including Prometheus 2.x.
Testing
Built the demo plugin with:
Started the plugin on an isolated port and confirmed that the metrics endpoint
returns HTTP 200 with the expected header:
Content-Type: text/plain; version=0.0.4; charset=utf-8