【发布时间】:2021-06-08 22:07:49
【问题描述】:
我正在尝试让 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']
从我从 prometheus 文档中读到的内容是,服务器需要 format 中的数据。我尽量尊重它,但 promotheus 没有接受它。
任何帮助将不胜感激,谢谢。 PS:Prometheus的java客户端无法使用,需要这样操作。
【问题讨论】:
-
为什么将指标存储在静态文件中?指标应该发展。当您使用 Spring Boot 时,您可以在类路径中添加
spring-boot-actuator+micrometer-registry-prometheus,然后 spring-boot 将自动配置并公开一个包含所有这些指标的 /prometheus 端点。 -
我完全同意你的看法,这将是更好的方法,但我必须以这种方式实现它.. 我很确定它是可行的。
-
您能提供您的回复正文吗?和原文件一样吗?
-
是的,和文件里的一样。
-
您也可以分享您的 prometheus 配置文件,以便我们检查作业是否设置好。你的工作是否指向好的 IP:HOST/metrics ?
标签: java spring spring-boot prometheus