'Can't figure out how to change Prometheus content type header
So my metrics all appear in one line at my end-point, not in new line per metric. I use micrometer, spring, prometheus and scala.
My controller:
@RequestMapping(Array(""))
class MetricsController @Inject() (prometheusRegistry: PrometheusMeterRegistry) {
@RequestMapping(value = Array("/metrics"), method = Array(RequestMethod.GET))
def metricses(): String = {
prometheusRegistry.scrape()
}
}
Should it be enough to change the way I write the metrics them selves?
I have tried to add scrape(TextFormat.CONTENT_TYPE_004)
but that changed nothing.
Does it have to do with the HTTP response header?
Would it work to add:
.putHeader(HttpHeaders.CONTENT_TYPE, TextFormat.CONTENT_TYPE_004)
.end(registry.scrape());
If so how would I do that in my case?
Thanks
Solution 1:[1]
Prometheus (or other compatible backends) will send you an Accept
header that you should not ignore (please read about content negotiation) but if you want to ignore it:
@GetMapping(path = "/metrics", produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseBody String metrics() {
return registry.scrape();
}
If you don't want to ignore it, TextFormat
has a chooseContentType
method that you can utilize to get the content type based on the Accept
header:
@GetMapping(path = "/metrics")
@ResponseBody ResponseEntity<String> metrics(@RequestHeader("accept") String accept) {
String contentType = TextFormat.chooseContentType(accept);
return ResponseEntity
.ok()
.contentType(MediaType.valueOf(contentType))
.body(registry.scrape(contentType));
}
Or you can also set-up content negotiation: https://www.baeldung.com/spring-mvc-content-negotiation-json-xml
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jonatan Ivanov |