java - Promotheus 无法从自定义 Rest Endpoint 读取指标
问题描述
我正在尝试让 Promotheus 获取通过自定义 Spring Boot 端点公开的指标。我在文件中有指标
# 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']
从我从普罗米修斯文档中读到的内容是,服务器需要这种格式 的数据。我尽量尊重它,但 promotheus 并没有接受它。
任何帮助将不胜感激,谢谢。PS:Prometheus的java客户端不能用,需要这样。