Prometheus 与 Spring boot

说到 Prometheus 不得提到监控,Prometheus能帮助我们指标数据采集、指标数据储存、指标分析与视觉化、告警等,它又可以针对系统的某些组件行监控

  • CPU
  • Load
  • Mempry
  • Swap
  • Disk IO
  • Process
    针对网路方面也可以获得以下资讯
  • 负载
  • 延迟
  • 速率
    Prometheus 可针对基础设施监控
  • 消息类
    • Kafka ...
  • Web 服务
    • Nginx ...
  • 资料库或缓存
    • PostgreSQL ...

Promethues 架构如下

  • 多为度资料模型
  • 灵活的查询语言(PromQL)
  • 基於 HTTP 的 Pull 方式收集时序资料
  • 可以透过 Push Gateway 进行资料推送
  • 可与 Grafana 等仪表板进行视觉化呈现
  • 能透过服务发现(Service discovery)或静态组态去获取监控的 Targets

PromQL:允许进行多种操作,包括 aggregation, slicing and dicing, prediction and joins

其元件包括以下

  • Prometheus Server
    • 收集与储存时间序列资料,并提供 PromQL 查询语言支援。
  • Client Library
    • 客户端函式库,提供语言开发来开发产生 Metrics 并曝露 Prometheus Server。当 Prometheus Server 来 Pull 时,直接返回即时状态的 Metrics。
  • Pushgateway
    • 主要用於临时性 Job 推送。这类 Job 存在期间较短,有可能 Prometheus 来 Pull 时就消失,因此透过一个闸道来推送。适合用於服务层面的 Metrics。
  • Exporter
    • 用来曝露已有第三方服务的 Metrics 给 Prometheus Server,即以 Client Library 开发的 HTTP server。
  • AlertManager
    • 接收来至 Prometheus Server 的 Alert event,并依据定义的 Notification 组态发送警报,ex: E-mail、Pagerduty、OpenGenie 与 Webhook 等等。

Prometheus 支持三种类型的抓取(scraping)

  • Exporters
    • 服务无提供,需另外提供规格
  • Instrumentation
    • 自身服务提供
  • Pushgateway
    • 短期任务或批次性其身命周期难以预估,很难透过 export 方式获取

SpringBoot 监控应用

在 Spring boot 中 Actuator 可以导出正在运行的应用程序的一些讯息像是健康、指标、讯息等。它使用 HTTP 端点或 JMX bean 使我们能够与其交互。我们可以如下配置

                 <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-actuator</artifactId>
                </dependency>

最後这些收集的指标我们可以透过 push gateway 方式将数据丢至 prometheus 上,在藉由 grafana 进行呈现,我们在 Spring boot 中可以引入以下两个框架实现。

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

最後在环境配置上我们可以如下配置,即可完成使用 prometheus 进行监控。明天将会带实验来模拟,并会针对 prometheus 配置进行一些说明

management.endpoints.web.exposure.include=prometheus # 可暴露的端点
management.metrics.export.prometheus=true
management.metrics.export.prometheus.pushgateway.base-url=${PUSHGATEWAYS_URI} # 推送 pushgateway,promethues 在 pull
management.metrics.export.prometheus.pushgateway.enabled=true
management.metrics.export.prometheus.pushgateway.push-rate=1m
management.metrics.export.prometheus.pushgateway.shutdown-operation=push

<<:  Day 8. 声明式渲染-跟Vue说Hello

>>:  [第五只羊] 迷雾森林建筑工事 IV 专案环境设定 READY GO

连续 30 天 玩玩看 ProtoPie - Day 10

今天要来进行使用者点击之後跳到下一页的行为,一样延续昨天使用的介面。 所以我们选择下方的按钮(Ge...

[ 卡卡 DAY 19 ] - React Native 用 react-native-webview 实现 webview 跟 html render

在 App 需求中 若页面需要通过 URL 渲染远端 HTML 页面 若页面资料提供的是 html...

[2021铁人赛 Day13] General Skills 10

引言 今天会讲两题,因为第二题是我们讲过的,就当作复习吧! 第一题继续延伸进位制,第二题是已经出现...

Day 28 - 重要的钥匙要藏好

越接近完赛越害怕自己今天到底发文了没!这几天早中晚都会反覆确认有没有发文,毕竟坚持30天写技术文章那...

[Angular] Day7. Content projection

本章节将要介绍如何使用 ng-content 将一个 Component 的内容投影到另一个 Com...