Skip to content

Set Content-Type for demo plugin metrics endpoint - #271

Merged
popduke merged 2 commits into
apache:mainfrom
tinybad:agent/fix-prometheus-metrics-content-type
Aug 10, 2026
Merged

Set Content-Type for demo plugin metrics endpoint#271
popduke merged 2 commits into
apache:mainfrom
tinybad:agent/fix-prometheus-metrics-content-type

Conversation

@tinybad

@tinybad tinybad commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

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:

non-compliant scrape target sending blank content-type and no fallback_scrape_protocol specified for target

The response body contained valid metrics. The scrape failed because the demo
plugin did not set a Content-Type response header, and Prometheus did not have
a fallback_scrape_protocol configured 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 unrecognized
Content-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-8

The header is set using TextFormat.CONTENT_TYPE_004, which matches the output
format produced by PrometheusMeterRegistry.scrape().

The response body is also encoded explicitly as UTF-8. The encoded byte array
is reused when calculating Content-Length and 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 older
Prometheus versions, including Prometheus 2.x.

Testing

Built the demo plugin with:

./mvnw -pl build/build-plugin-demo -am package -DskipTests

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

@popduke popduke 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.

Thx for fixing

@popduke
popduke marked this pull request as ready for review August 10, 2026 07:38
@popduke
popduke merged commit a92c5a5 into apache:main Aug 10, 2026
4 checks passed
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