Prometheus 是一套開源的系統監控報警框架。它啓發於 Google 的 borgmon 監控系統,由工做在 SoundCloud 的 google 前員工在 2012 年建立,做爲社區開源項目進行開發,並於 2015 年正式發佈。html
做爲新一代的監控框架,Prometheus 具備如下特色:node
Prometheus 生態圈中包含了多個組件,其中許多組件是可選的:web
下圖爲 Prometheus 官方文檔中的架構圖:正則表達式
從上圖能夠看出,Prometheus 的主要模塊包括:Prometheus server, exporters, Pushgateway, PromQL, Alertmanager 以及圖形界面。spring
其大概的工做流程是:docker
下面將對 Prometheus 中的數據模型(時間序列),metric 類型,instance 和 jobs等概念進行介紹。shell
Prometheus 中存儲的數據爲時間序列,是由 metric 的名字和一系列的標籤(鍵值對)惟一標識的,不一樣的標籤則表明不一樣的時間序列。json
Prometheus客戶端庫提供了四種核心Metrics類型。api
在Prometheus術語中,你能夠scrape(刮擦)的端點稱爲 實例,一般對應於單個進程。一組同種類型的 instances(主要用於保證可擴展性和可靠性),例如:具備四個複製instances(實例)的API服務器job做業:瀏覽器
當Prometheus scrape(刮擦)目標時,它會自動在scrape的時間序列上附加一些標籤,用來識別scrape的目標。
對於每次實例 scrape(刮取,Prometheus都會在如下時間序列中存儲樣本:
up時間序列對於實例可用性監視很是有用。
你能夠在官網 prometheus.io/download/ 下載 安裝包,解壓後使用。爲了方便,我使用docker 鏡像的方式 運行Prometheus。
docker run --name prometheus -d -p 9090:9090 prom/prometheus
複製代碼
瀏覽器輸入http://localhost:9090 ,訪問 Prometheus 的 Web UI:
點擊菜單欄 「Status」 下的 Targets ,界面以下:
能夠看大Prometheus 自身 metrics 處於UP狀態 ,說明 安裝成功。
Prometheus 的配置文件 prometheus.yml 內容以下:
# 全局設置,能夠被覆蓋
global:
scrape_interval: 15s
evaluation_interval: 15s
rule_files:
# - "first.rules"
# - "second.rules"
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['localhost:9090']
複製代碼
該global
塊控制 Prometheus 的全局配置。咱們有兩種選擇。第一個,scrape_interval
控制Prometheus 刮擦目標的頻率。你能夠爲單個目標覆蓋此值。在這種狀況下,全局設置是每15秒刮一次。該evaluation_interval
選項控制普羅米修斯評估規則的頻率。Prometheus 使用規則建立新的時間序列並生成警報。
該rule_files
塊指定咱們但願 Prometheus 加載的任何規則的位置。如今咱們沒有規則。
最後一個塊scrape_configs
控制 Prometheus 監視的資源。因爲 Prometheus 還將本身的數據公開爲HTTP端點,所以它能夠抓取並監控自身的健康情況。在默認配置中有一個名爲 prometheus 的job,它抓取 prometheus 服務器 公開的時間序列數據。該做業包含一個靜態配置的目標,即端口9090上的本地主機。返回的時間序列數據將詳細說明Prometheus服務器的狀態和性能。
爲了演示 Prometheus 的簡單使用,這裏運行一個 Prometheus HTTP 度量模擬器。模擬一個簡單的HTTP微服務,生成Prometheus Metrics,經過 docker 運行。
docker run -p 8080:8080 pierrevincent/prom-http-simulator:0.1
複製代碼
它在/metrics端點下公開如下Prometheus指標:
能夠開啓流量高峯模式,更改流量高峯模式能夠經過如下方式完成:
# ON
curl -X POST http://127.0.0.1:8080/spike/on
# OFF
curl -X POST http://127.0.0.1:8080/spike/off
# RANDOM
curl -X POST http://127.0.0.1:8080/spike/random
複製代碼
錯誤率默認爲1%。它能夠更改成0到100之間的數字:
# 例如將錯誤率設置爲50%
curl -H 'Content-Type: application/json' -X PUT -d '{"error_rate": 50}' http://127.0.0.1:8080/error_rate
複製代碼
須要將 HTTP 度量模擬器 的 metrics端點 配置到 Prometheus的配置文件 prometheus.yml 中。
建立一個 prometheus.yml 文件 內容以下:
global:
scrape_interval: 5s
evaluation_interval: 5s
scrape_timeout: 5s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'http-simulator'
metrics_path: /metrics
static_configs:
- targets: ['172.16.1.232:8080']
複製代碼
經過docker up
命令替換 容器中的配置文件:
docker cp prometheus.yml prometheus:/etc/prometheus/
複製代碼
重啓容器:
docker restart prometheus
複製代碼
訪問 http://localhost:9090/targets ,發現已經出現了 target 「http-simulator」 ,而且爲UP狀態。
查詢http請求數
http_requests_total{job="http-simulator"}
複製代碼
查詢成功login請求數
http_requests_total{job="http-simulator", status="200", endpoint="/login"}
複製代碼
查詢成功請求數,以endpoint區分
http_requests_total{job="http-simulator", status="200"}
複製代碼
查詢總成功請求數
sum(http_requests_total{job="http-simulator", status="200"})
複製代碼
查詢成功請求率,以endpoint區分
rate(http_requests_total{job="http-simulator", status="200"}[5m])
複製代碼
查詢總成功請求率
sum(rate(http_requests_total{job="http-simulator", status="200"}[5m]))
複製代碼
查詢http-simulator延遲分佈
http_request_duration_milliseconds_bucket{job="http-simulator"}
複製代碼
查詢成功login延遲分佈
http_request_duration_milliseconds_bucket{job="http-simulator", status="200", endpoint="/login"}
複製代碼
不超過200ms延遲的成功login請求佔比
sum(http_request_duration_milliseconds_bucket{job="http-simulator", status="200", endpoint="/login", le="200"}) / sum(http_request_duration_milliseconds_count{job="http-simulator", status="200", endpoint="/login"})
複製代碼
成功login請求延遲的99百分位
histogram_quantile(0.99, rate(http_request_duration_milliseconds_bucket{job="http-simulator", status="200", endpoint="/login"}[5m]))
複製代碼
上面給出的這些查詢表達式,在 prometheus 的 查詢界面上自行測試下 ,這裏就不一一測試了,
本篇對 Prometheus 的組成,架構和基本概念進行了介紹,並實例演示了 Prometheus 的查詢表達式的應用。本篇是 Prometheus 系列的第一篇, 後續還會有Prometheus與其餘圖形界面的集成,與 springboot 應用的集成等 。
prometheus.io/docs/introd… www.ibm.com/developerwo…