promotheus无法从自定义rest端点读取度量

dvtswwa3  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(513)

我正试图让promotheus通过一个定制的spring引导端点获取公开的度量。我在文件里有指标

  1. # HELP cpu_usage_total The total amount of CPU.
  2. # TYPE cpu_usage_total gauge.
  3. cpu_usage_total 0.24950100481510162
  4. # HELP memory_usage_total The total amount of MEMORY.
  5. # TYPE memory_usage_total gauge.
  6. memory_usage_total 30.0

我创建了一个restful端点来读取这个文件并在端口8080上公开它的内容。以下是我迄今为止尝试的:

  1. @GetMapping(value = "/metrics")
  2. public void metrics(HttpServletResponse response) throws IOException {
  3. File file = new File("/var/log/logparsing");
  4. InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
  5. MediaType mediaType = new MediaType("text", "plain", StandardCharsets.UTF_8);
  6. InputStream myStream = new FileInputStream(file);
  7. // Set the content type and attachment header.
  8. response.setContentType("text/plain; version=0.0.4;charset=utf-8");
  9. response.setCharacterEncoding("utf-8");
  10. // Copy the stream to the response's output stream.
  11. IOUtils.copy(myStream, response.getOutputStream());
  12. response.flushBuffer();

我的promotheus.yaml配置文件:

  1. global:
  2. scrape_interval: 15s # By default, scrape targets every 15 seconds.
  3. external_labels:
  4. monitor: 'codelab-monitor'
  5. scrape_configs:
  6. - job_name: 'prometheus'
  7. scrape_interval: 5s
  8. metrics_path: '/metrics'
  9. static_configs:
  10. - targets: ['logparsing:8080']

我从prometheus文档中了解到,服务器需要这种格式的数据。我尽我所能地尊重它,但普罗莫斯不接受。
任何帮助都将不胜感激,谢谢。ps:我不能使用prometheus的java客户端,它需要这样做。

sigwle7e

sigwle7e1#

我复制了你的环境,似乎你的指标是无效的,因为 \r 行尾的字符。
我先得到这个:

然后我删除了评论:
没有评论

我已经把所有的 \r 文件中的字符,这是有效的。

附言:一些编辑补充道 \r 自动地,我用notepad++来编辑文件。

相关问题