Prometheus

什么是Prometheus?

  Prometheus是一个开源系统监控和警报工具包,具有强大的数据模型和查询语言。它适用于大规模分布式系统

  Prometheus 将其指标收集并存储为时间序列数据,即指标信息与记录时的时间戳一起存储,以及称为标签的可选键值对

 

  下图说明了 Prometheus 的架构及其一些生态系统组件:

  Prometheus 直接或通过短期作业的中间推送网关从仪表化作业中获取指标。它在本地存储所有抓取的样本,并对这些数据运行规则,以聚合和记录现有数据的新时间序列或生成警报。Grafana或其他 API 使用者可用于可视化收集的数据

 

  Prometheus主要特点:

    多维数据模型,其中包含由指标名称和键/值对标识的时间序列数据

    PromQL,一种灵活的查询语言 利用这个维度

    不依赖分布式存储;单个服务器节点是自治的

    时间序列收集通过 HTTP 上的拉模型进行

    推送时间序列通过中间网关支持

    通过服务发现或静态配置发现目标

    多种图形和仪表板支持模式

    

  什么是指标?

    指标是外行术语中的数值测量

    术语时间序列是指随时间变化的记录。用户想要测量的内容因应用程序而异。对于 Web 服务器来说,它可能是请求时间;对于数据库,它可以是活动连接或活动查询的数量等

    指标在理解应用程序为何以某种方式运行方面发挥着重要作用。假设您正在运行一个 Web 应用程序并发现它很慢。要了解您的应用程序发生了什么,您将需要一些信息。例如,当请求数量较多时,应用程序可能会变慢。如果您有请求计数指标,则可以确定原因并增加处理负载的服务器数量

 

使用:

  Java项目集成Prometheus

  集成Prometheus到Java项目通常涉及以下几个步骤:

  ①、引入Prometheus客户端库:

    在你的Java项目中引入Prometheus的Java客户端库,这个库提供了一些用于收集和暴露监控指标的工具

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient</artifactId>
    <version>0.11.0</version>
</dependency>

  ②、定义监控指标:

    在代码中定义需要监控的指标,例如计数器、直方图、摘要等。Prometheus使用这些指标来收集应用程序的性能数据

import io.prometheus.client.Counter;
import io.prometheus.client.Histogram;

public class YourMetrics {
    // 定义计数器
    static final Counter requestsTotal = Counter.build()
        .name("your_app_requests_total")
        .help("Total number of requests.")
        .register();

    // 定义直方图
    static final Histogram requestDuration = Histogram.build()
        .name("your_app_request_duration_seconds")
        .help("Request duration in seconds.")
        .register();
}

  ③、暴露监控端点:

    将监控指标暴露给Prometheus,以便它可以定期抓取这些数据。这通常通过使用HTTP端点实现

import io.prometheus.client.exporter.MetricsServlet;
import io.prometheus.client.hotspot.DefaultExports;

public class YourMetricsServer {
    public static void main(String[] args) {
        // 初始化默认的JVM监控指标
        DefaultExports.initialize();

        // 启动MetricsServlet,将监控指标暴露在 /metrics 路径
        new HTTPServer(8080);
    }
}

  ④、启动应用程序:  

    启动你的Java应用程序,确保监控指标可以通过 /metrics 路径访问

  ⑤、配置Prometheus服务器:

    在Prometheus服务器的配置文件中添加你的Java应用程序的地址,以便Prometheus可以定期抓取指标。

scrape_configs:
  - job_name: 'your_java_app'
    static_configs:
      - targets: ['your_java_app_host:8080']

  ⑥、启动Prometheus服务器:

    启动Prometheus服务器并访问其界面,你应该能够看到你的Java应用程序的监控指标

 

 

  Spring项目集成Prometheus

  ①、添加依赖:在Spring Boot项目中,你可以通过添加micrometer-registry-prometheus依赖来集成Prometheus。这个依赖包含了Micrometer的Prometheus注册器

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

  ②、配置Prometheus Endpoint:在application.properties或application.yml中添加以下配置,启用Spring Boot Actuator的 /actuator/prometheus 端点

management:
  endpoints:
    web:
      exposure:
        include: prometheus

  如果你使用的是Spring Boot 2.0及以上版本,也可以使用以下配置:

management:
  endpoints:
    web:
      exposure:
        include: prometheus
  metrics:
    export:
      prometheus:
        enabled: true

  ③、启动应用程序:

    启动Spring Boot应用程序。现在,Prometheus监控指标将通过 /actuator/prometheus 端点暴露

  ④、配置Prometheus服务器:在Prometheus服务器的配置文件中添加你的Spring Boot应用程序的地址,以便Prometheus可以定期抓取指标    

scrape_configs:
  - job_name: 'your_spring_app'
    static_configs:
      - targets: ['your_spring_app_host:your_spring_app_port']

  ⑤、启动Prometheus服务器

    启动Prometheus服务器并访问其界面,你应该能够看到你的Spring Boot应用程序的监控指标

    注意:确保端口是正确配置的,以便Prometheus可以成功抓取指标。默认情况下,Spring Boot Actuator的 /actuator/prometheus 端点使用management.server.port配置的端口。如果你更改了端口,请相应地调整Prometheus配置。

 

 

官网:https://prometheus.io/

官方文档:https://prometheus.io/docs/introduction/overview/

 

END.

posted @ 2023-08-16 17:19  杨岂  阅读(29)  评论(0编辑  收藏  举报