Skip to content

Commit

Permalink
Add content type to the operator metric endpoints (#10908)
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Scholz <[email protected]>
  • Loading branch information
scholzj authored Dec 5, 2024
1 parent 4820a6b commit 4364690
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ private static Future<HttpServer> startHealthServer(Vertx vertx, MetricsProvider
request.response().setStatusCode(204).end();
} else if (request.path().equals("/metrics")) {
PrometheusMeterRegistry metrics = (PrometheusMeterRegistry) metricsProvider.meterRegistry();
request.response().setStatusCode(200)
request.response()
.putHeader("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
.setStatusCode(200)
.end(metrics.scrape());
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
* Jetty based web server used for health checks and metrics
Expand Down Expand Up @@ -158,12 +159,13 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
class MetricsHandler extends AbstractHandler {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/plain");

if (prometheusMeterRegistry != null) {
response.setContentType("text/plain; version=0.0.4");
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
response.setStatus(HttpServletResponse.SC_OK);
prometheusMeterRegistry.scrape(response.getWriter());
} else {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
response.getWriter().println("Prometheus metrics are not enabled");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public void testAllGood() throws IOException, InterruptedException, URISyntaxExc
request = HttpRequest.newBuilder().uri(new URI("http://localhost:" + port + "/metrics")).GET().build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
assertThat(response.statusCode(), is(200));
assertThat(response.headers().map().get("Content-Type").size(), is(1));
assertThat(response.headers().map().get("Content-Type").get(0), is("text/plain; version=0.0.4;charset=utf-8"));
assertThat(response.body(), containsString("my_metric_total 1.0"));
} finally {
server.stop();
Expand Down

0 comments on commit 4364690

Please sign in to comment.