關於Prometheus Prometheus是一套開源的監控系統,它將所有信息都存儲為時間序列數據;因此實現一種Profiling監控方式,實時分析系統運行的狀態、執行時間、調用次數等,以找到系統的熱點,為性能優化提供依據。 監控方式 程式代碼收集運行數據寫入到redis,通過API介面開放給Pr ...
關於Prometheus
Prometheus是一套開源的監控系統,它將所有信息都存儲為時間序列數據;因此實現一種Profiling監控方式,實時分析系統運行的狀態、執行時間、調用次數等,以找到系統的熱點,為性能優化提供依據。 監控方式
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監控界面。
