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

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

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


# HELP cpu_usage_total The total amount of CPU.

# TYPE cpu_usage_total gauge.

cpu_usage_total 0.24950100481510162

# HELP memory_usage_total The total amount of MEMORY.

# TYPE memory_usage_total gauge.

memory_usage_total 30.0

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

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

我的promotheus.yaml配置文件:

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  external_labels:
   monitor: 'codelab-monitor'
scrape_configs:
   - job_name: 'prometheus'
   scrape_interval: 5s
   metrics_path: '/metrics' 

   static_configs:
    - targets: ['logparsing:8080']

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

sigwle7e

sigwle7e1#

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

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

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

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

相关问题