prometheus監控系統


關於Prometheus

Prometheus是一套開源的監控系統,它將所有信息都存儲為時間序列數據;因此實現一種Profiling監控方式,實時分析系統運行的狀態、執行時間、調用次數等,以找到系統的熱點,為性能優化提供依據。
監控方式
程序代碼收集運行數據寫入到redis,通過API接口開放給Prometheus,Prometheus定時去抓取接口上的數據,形成時間系數據存入到本地。同時通過promql與開放接口可視化數據到前端。
 
一、采集運行數據寫入到redis
在采集數據之前,先要明白Prometheus所支持的幾種數據類型
 
Counter,統計類數據
Counter作為一種計數類數據類型,常用於接口調用次數、請求總數、當前在線人數等等
 
Gauge,記錄對象或許事物的瞬時值
Gauge是最簡單的度量類型,只有一個簡單的返回值,他用來記錄一些對象或者事物的瞬時值,如CPU使用率、內存使用情況、磁盤空間等。
 
Histograms,直方圖
Histrogram是用來度量數據中值的分布情況,如程序執行時間:0-100ms、100-200ms、200-300ms、>300ms 的分布情況
 
在使用Prometheus監控之前,先要明白都需要監控哪些項,以及每一中項都應該采用什么數據類型來表示
 
明白以上類型之后,就可以開始將監控數據項寫入到redis。寫入的工具可采用開源的prometheus_client_php;github:https://github.com/Jimdo/prometheus_client_php 
下載后需要用composer部署其依賴,如果你不知道composer的使用:http://www.phpcomposer.com 
以下代碼記錄一個counter數據
 
require __DIR__ . '/../vendor/autoload.php';
use Prometheus\CollectorRegistry;
use Prometheus\Storage\Redis;

Redis::setDefaultOptions(array('host' => 'master.redis.reg'));
$adapter = new Prometheus\Storage\Redis();
$registry = new CollectorRegistry($adapter);

$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter->incBy(1, ['blue']);  //將統計結果增加1

 

 
采集的過程比較簡單,Gauge與Histograms與此類似
 
二、提供Prometheus訪問的API Gateway
 
prometheus_client_php工具已經准好了prometheus訪問的相關接口,我們至需要幾行簡單的代碼
 
require __DIR__ . '/../vendor/autoload.php';

use Prometheus\CollectorRegistry;
use Prometheus\RenderTextFormat;
use Prometheus\Storage\Redis;

Redis::setDefaultOptions(array('host' => 'master.redis.reg')); //對應寫入的redis
$adapter = new Prometheus\Storage\Redis();
$registry = new CollectorRegistry($adapter);
$renderer = new RenderTextFormat();
$result = $renderer->render($registry->getMetricFamilySamples());

header('Content-type: ' . RenderTextFormat::MIME_TYPE);
echo $result; //輸出結果給prometheus服務

 

 
將以上腳本部署,能通過HTTP訪問到,下一步即可提供給prometheus服務
 
三、安裝與配置Prometheus
安裝(略過)
配置Prometheus
打開prometheus.yml文件,在scrape_configs配置第二步中的API Gateway
 - job_name: 'prometheus2'
    metrics_path: '/metrics.php'
    static_configs:  
      - targets: ['localhost:9999']
 
配置完成后,啟動Prometheus
 
四、查看監控數據
Prometheus具有多種模式的可視化數據,可集成Grafana可視化工具。其內置PromDash界面,啟動后打開http://localhost:9090(默認9090端口)即可訪問內置的PromDash監控界面。
上圖是Prometheus的直方圖展現形式,頂部的下拉框是你所監控的數據項,點擊Execute點擊后查看所監控的數據。輸入框部分是是查詢的表達式,Prometheus支持豐富的查詢表達式,詳情參閱: https://prometheus.io/docs/querying/basics/  。
圖中的每一條線表示大於該桶的數據,紅色的數據線是所有桶的總數累計。另外Prometheus還提供了sum與cont的統計。Gauge與Counter數據類型也與此類似。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM