可以弄清楚如何更改Prometheus内容类型标头

发布于 2025-01-24 07:37:43 字数 633 浏览 4 评论 0原文

因此,我的指标全部出现在我的端点上的一行中,而不是按照每个度量的新行。 我使用千分尺,春季,普罗米修斯和斯卡拉。

我的控制器:

@RequestMapping(Array(""))
class MetricsController @Inject() (prometheusRegistry: PrometheusMeterRegistry) {

  @RequestMapping(value = Array("/metrics"), method = Array(RequestMethod.GET))
  def metricses(): String = {
    prometheusRegistry.scrape()
  }

}

改变我编写自己的指标的方式是否足够?

我尝试添加scrape(textformat.content_type_004),但这并没有改变。 它与HTTP响应标头有关吗? 是否可以补充:

   .putHeader(HttpHeaders.CONTENT_TYPE, TextFormat.CONTENT_TYPE_004)
   .end(registry.scrape());

如果是这样,我该怎么办?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没企图 2025-01-31 07:37:43

Prometheus(或其他兼容的后端)会向您发送Accept您不应该忽略的标题(请阅读有关内容谈判的有关),但是如果您想忽略它:

@GetMapping(path = "/metrics", produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseBody String metrics() {
    return registry.scrape();
}

如果您不想忽略它, textformat具有choosecontenttype您可以使用Accept标题来获取内容类型的方法:

@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));
}

或者您还可以设置内容谈判: https://www.baeldung.com/spring-mvc-content -nogotiation-json-xml

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文