從零開始學習Prometheus監控報警系統

Prometheus簡介

Prometheus是一個開源的監控報警系統,它最初由SoundCloud開發。

2016年,Prometheus被納入了由谷歌發起的Linux基金會旗下的雲原生基金會( Cloud Native Computing Foundation),並成為僅次於Kubernetes的第二大開源項目。自此,它成為了一個獨立的開源項目,獨立於任何公司進行維護。

Prometheus擁有非常活躍的開發人員和用戶社區,目前在GitHub上已擁有三萬多的Star。

Prometheus特點

  • 提供多維度數據模型,使用指標名稱和鍵值對標識的時間序列數據
  • 提供靈活的PromQL查詢方式,還提供了HTTP查詢接口,可以很方便地結合Grafana等組件展示數據。
  • 不依賴外部存儲,支持單節點的本地存儲。通過Prometheus自帶的時序數據庫,可以完成每秒百萬及的數據存儲,如果需要存儲大量歷史數據,還可以對接第三方的時序數據庫。
  • 時間序列收集通過HTTP的拉取方式進行,並提供了開放的指標數據標準。
  • 支持向中間網關推送時序數據,可以更加靈活地適用於多種監控場景。
  • 支持通過動態服務發現和靜態文件配置獲取監控對象,目前已支持Kubernetes、Etcd、Consul等多種服務發現機制。
  • 支持多種模式的圖形展示和儀錶盤。
  • 大多數Prometheus的組件都是使用Go語言編寫的,這使得它們很容易以二進制文件的形式構建和部署。

歡迎關注微信公眾號:萬貓學社,每周一分享Java技術乾貨。

Prometheus架構

Prometheus生態圈由多個組件構成,其中許多組件是可選的:

  • Prometheus Server:用於收集、存儲和查詢時間序列數據。通過靜態配置文件管理監控目標,也可以配合使用動態服務發現的方式動態管理監控目標,並從這些監控目標中獲取數據。它將採集到的數據按照時間序列的方式存儲在本地磁盤當中或者外部的時序數據庫中,可通過PromQL語言對數據的查詢以及分析。
  • Client Library:為被監控的應用生成相應的指標(Metric)數據並暴露給Prometheus Server。當Prometheus Server 來拉取時,直接返回實時狀態的指標數據。
  • Push Gateway:主要用於短期存在的Jobs。由於這類Jobs存在時間較短,可能在Prometheus Server來拉取數據之前就消失了。所以,Jobs可以直接向Push Gateway推送它們的指標數據,然後Prometheus Server再從Push Gateway拉取。
  • Exporters:用於暴露已有的第三方服務的指標數據通過HTTP服務的形式暴露給Prometheus Server,比如HAProxy、StatsD、Graphite等等。Prometheus Server通過訪問該Exporter提供的Endpoint,即可獲取到需要採集的監控數據。
  • Alertmanager:從Prometheus Server接收到告警后,會進行去除重複數據,分組,並路由到對收的接受方式,發出報警。Alertmanager的告警方式非常靈活,支持通過郵件、slack或釘釘等多種途徑發出告警。
  • 一些其他的組件。

下面這張圖展示了Prometheus的架構和各個組件是如何交互和協作的:

其大概的工作流程是:

  1. Prometheus Server直接從HTTP接口或者Push Gateway拉取指標(Metric)數據。
  2. Prometheus Server在本地存儲所有採集的指標(Metric)數據,並在這些數據上運行規則,從現有數據中聚合和記錄新的時間序列,或者生成告警。
  3. Alertmanager根據配置文件,對接收到的告警進行處理,發出報警。
  4. 在Grafana或其他API客戶端中,可視化收集的數據。

Prometheus數據模型

Prometheus會將所有採集到的監控數據以時間序列的方式保存在內存數據庫中,並且定時保存到硬盤上。每一條數據由以下三部分組成:

  • 指標(Metric):由指標名稱和描述當前數據特徵的標籤組成。
  • 時間戳(Timestamp):一個精確到毫秒的時間戳。
  • 數據值(Value):一個float64的浮點型數據表示當前數據的值。

其中,指標(Metric)通過如下格式標識:

<指標名稱>{<標籤名稱>=<標籤值>, ...}

指標名稱(Metric Name)可以反映被監控數據的含義。指標名稱只能由ASCII字符、数字、下劃線以及冒號組成並必須符合正則表達式[a-zA-Z_:][a-zA-Z0-9_:]*
標籤(Label)反映了當前數據的特徵維度,通過這些維度Prometheus可以對數據進行過濾,聚合等操作。標籤的名稱只能由ASCII字符、数字以及下劃線組成並滿足正則表達式[a-zA-Z_][a-zA-Z0-9_]*

比如:

prometheus_http_requests_total{code="200",handler="/metrics"}

歡迎關注微信公眾號:萬貓學社,每周一分享Java技術乾貨。

指標類型

Prometheus定義了4種不同的指標類型(Metric Type):

  • Counter(計數器)
  • Gauge(儀錶盤)
  • Histogram(直方圖)
  • Summary(摘要)

Counter(計數器)

Counter類型和計數器一樣,只增不減(除非系統發生重置),一般在定義Counter類型指標的名稱時推薦使用_total作為後綴。

比如,Prometheus Server中prometheus_http_requests_total, 表示Prometheus處理的HTTP請求總數:

# HELP prometheus_http_requests_total Counter of HTTP requests.
# TYPE prometheus_http_requests_total counter
prometheus_http_requests_total{code="200",handler="/api/v1/label/:name/values"} 3
prometheus_http_requests_total{code="200",handler="/api/v1/query"} 5
prometheus_http_requests_total{code="200",handler="/api/v1/query_range"} 15
prometheus_http_requests_total{code="200",handler="/graph"} 3
prometheus_http_requests_total{code="200",handler="/metrics"} 23
prometheus_http_requests_total{code="200",handler="/static/*filepath"} 18
prometheus_http_requests_total{code="302",handler="/"} 1

Gauge(儀錶盤)

Gauge類型側重於反應系統的某一個瞬時的值,這類指標的數據可增可減。

比如,Prometheus Server中go_threads, 表示Prometheus當前go線程的數量:

# HELP go_threads Number of OS threads created.
# TYPE go_threads gauge
go_threads 13

Histogram(直方圖)

Histogram類型由 _bucket{le=” “}, _bucket{le=”+Inf”}, _sum, _count 組成,主要用於表示一段時間範圍內對數據進行採樣,並能夠對其指定區間以及總數進行統計,通常它採集的數據展示為直方圖。

比如,Prometheus Server中prometheus_http_response_size_bytes:

# HELP prometheus_http_response_size_bytes Histogram of response size for HTTP requests.
# TYPE prometheus_http_response_size_bytes histogram
prometheus_http_response_size_bytes_bucket{handler="/",le="100"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1000"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="10000"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="100000"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+06"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+07"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+08"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="1e+09"} 1
prometheus_http_response_size_bytes_bucket{handler="/",le="+Inf"} 1
prometheus_http_response_size_bytes_sum{handler="/"} 29
prometheus_http_response_size_bytes_count{handler="/"} 1

Summary(摘要)

Summary類型由 {quantile=”<φ>”}, _sum, _count 組成,主要用於表示一段時間內數據採樣結果,它直接存儲了分位數據,而不是根據統計區間計算出來的。

比如,Prometheus Server中prometheus_target_interval_length_seconds:

# HELP prometheus_target_interval_length_seconds Actual intervals between scrapes.
# TYPE prometheus_target_interval_length_seconds summary
prometheus_target_interval_length_seconds{interval="15s",quantile="0.01"} 14.9986249
prometheus_target_interval_length_seconds{interval="15s",quantile="0.05"} 14.998999
prometheus_target_interval_length_seconds{interval="15s",quantile="0.5"} 15.0000428
prometheus_target_interval_length_seconds{interval="15s",quantile="0.9"} 15.0012009
prometheus_target_interval_length_seconds{interval="15s",quantile="0.99"} 15.0016468
prometheus_target_interval_length_seconds_sum{interval="15s"} 315.0013755
prometheus_target_interval_length_seconds_count{interval="15s"} 21

歡迎關注微信公眾號:萬貓學社,每周一分享Java技術乾貨。

安裝Prometheus Server

從官方網站(https://prometheus.io/download/)上找到最新版本的Prometheus Sevrer軟件包,如下圖:
根據自己的系統下載對應的壓縮包,這裏以Windows為例,下載prometheus-2.19.0.windows-amd64.tar.gz。

解壓后當前目錄會包含默認的Prometheus配置文件promethes.yml:

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']

暫且不做修改,雙擊prometheus.exe即可啟動,如下圖:

訪問http://localhost:9090/graph,就可以看到Prometheus自身的監控數據:

尾聲

Prometheus的大致介紹已經告一段落了,但是只是萬里長征的第一步,Prometheus的更多強大功能和使用方法還等待我們去挖掘。

微信公眾號:萬貓學社

微信掃描二維碼

獲得更多Java技術乾貨

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

網頁設計公司推薦不同的風格,搶佔消費者視覺第一線

※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

※自行創業缺乏曝光? 網頁設計幫您第一時間規劃公司的形象門面

南投搬家公司費用需注意的眉眉角角,別等搬了再說!

新北清潔公司,居家、辦公、裝潢細清專業服務

※教你寫出一流的銷售文案?

您可能也會喜歡…