iT邦幫忙

2021 iThome 鐵人賽

DAY 20
0
Software Development

一個新鮮人如何完轉Spring boot與DevOps從0到101系列 第 20

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

上一篇
Spring boot 配置 Fluent bit 傳遞 Log
下一篇
配置 Promethues 與 Grafana
系列文
一個新鮮人如何完轉Spring boot與DevOps從0到10130
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言