文章首發(fā)于公眾號《程序員果果》
地址 : https://mp.weixin.qq.com/s/BjZyNcWEgflJMnjfceiluw為大洼等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及大洼網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為成都做網(wǎng)站、成都網(wǎng)站制作、大洼網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
Prometheus 是一套開源的系統(tǒng)監(jiān)控報警框架。它啟發(fā)于 Google 的 borgmon 監(jiān)控系統(tǒng),由工作在 SoundCloud 的 google 前員工在 2012 年創(chuàng)建,作為社區(qū)開源項目進行開發(fā),并于 2015 年正式發(fā)布。
作為新一代的監(jiān)控框架,Prometheus 具有以下特點:
Prometheus 生態(tài)圈中包含了多個組件,其中許多組件是可選的:
下圖為 Prometheus 官方文檔中的架構圖:

從上圖可以看出,Prometheus 的主要模塊包括:Prometheus server, exporters, Pushgateway, PromQL, Alertmanager 以及圖形界面。
其大概的工作流程是:
下面將對 Prometheus 中的數(shù)據(jù)模型(時間序列),metric 類型,instance 和 jobs等概念進行介紹。
Prometheus 中存儲的數(shù)據(jù)為時間序列,是由 metric 的名字和一系列的標簽(鍵值對)唯一標識的,不同的標簽則代表不同的時間序列。
Prometheus客戶端庫提供了四種核心Metrics類型。
在Prometheus術語中,你可以scrape(刮擦)的端點稱為 實例,通常對應于單個進程。一組同種類型的 instances(主要用于保證可擴展性和可靠性),例如:具有四個復制instances(實例)的API服務器job作業(yè):
當Prometheus scrape(刮擦)目標時,它會自動在scrape的時間序列上附加一些標簽,用來識別scrape的目標。
對于每次實例 scrape(刮取,Prometheus都會在以下時間序列中存儲樣本:
up時間序列對于實例可用性監(jiān)視非常有用。
你可以在官網(wǎng) https://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狀態(tài) ,說明 安裝成功。
Prometheus 的配置文件 prometheus.yml 內(nèi)容如下:
# 全局設置,可以被覆蓋
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選項控制普羅米修斯評估規(guī)則的頻率。Prometheus 使用規(guī)則創(chuàng)建新的時間序列并生成警報。
該rule_files塊指定我們希望 Prometheus 加載的任何規(guī)則的位置。現(xiàn)在我們沒有規(guī)則。
最后一個塊scrape_configs控制 Prometheus 監(jiān)視的資源。由于 Prometheus 還將自己的數(shù)據(jù)公開為HTTP端點,因此它可以抓取并監(jiān)控自身的健康狀況。在默認配置中有一個名為 prometheus 的job,它抓取 prometheus 服務器 公開的時間序列數(shù)據(jù)。該作業(yè)包含一個靜態(tài)配置的目標,即端口9090上的本地主機。返回的時間序列數(shù)據(jù)將詳細說明Prometheus服務器的狀態(tài)和性能。
為了演示 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之間的數(shù)字:
# 例如將錯誤率設置為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 中。
創(chuàng)建一個 prometheus.yml 文件 內(nèi)容如下:
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 ,發(fā)現(xiàn)已經(jīng)出現(xiàn)了 target “http-simulator” ,并且為UP狀態(tài)。

查詢http請求數(shù)
http_requests_total{job="http-simulator"}
查詢成功login請求數(shù)
http_requests_total{job="http-simulator", status="200", endpoint="/login"}
查詢成功請求數(shù),以endpoint區(qū)分
http_requests_total{job="http-simulator", status="200"}查詢總成功請求數(shù)
sum(http_requests_total{job="http-simulator", status="200"})查詢成功請求率,以endpoint區(qū)分
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 系列的第一篇, 后續(xù)還會有Prometheus與其他圖形界面的集成,與 springboot 應用的集成等 。
https://prometheus.io/docs/introduction/overview/
https://www.ibm.com/developerworks/cn/cloud/library/cl-lo-prometheus-getting-started-and-practice/index.html

本文名稱:Prometheus入門
文章鏈接:http://www.chinadenli.net/article10/pisggo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、外貿(mào)網(wǎng)站建設、網(wǎng)站維護、手機網(wǎng)站建設、App設計、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)