使用python測試prometheus的實現(xiàn)
為了更直觀的了解prometheus如何工作,本文使用prometheus的python庫來做一些相應的測試。
python庫的github地址是https://github.com/prometheus
根據(jù)提示,使用pip安裝prometheus_client
pip3 install prometheus_client
然后根據(jù)文檔中的示例文件并簡單修改,運行一個client
文件命名為prometheus_python_client.py
from prometheus_client import start_http_server, Summary import random import time import sys # Create a metric to track time spent and requests made. REQUEST_TIME = Summary ('request_processing_seconds', 'Time spent processing request') # Decorate function with metric. @REQUEST_TIME.time ( ) def process_request(t): ? ? """A dummy function that takes some time.""" ? ? time.sleep (t) if __name__ == '__main__': ? ? try: ? ? ? ? if sys.argv[1].isdigit(): ? ? ? ? ? ? port = sys.argv[1] ? ? ? ? else: ? ? ? ? ? ? port = 8080 ? ? except: ? ? ? ? port = 8080 ? ? # Start up the server to expose the metrics. ? ? start_http_server (8080) ? ? # Generate some requests. ? ? while True: ? ? ? ? process_request (random.random ( ))
在后臺運行client
pytho3 prometheus_python_client.py 8080 &
此時可以訪問本機的8080端口,可以看到相應的metric
curl 127.0.0.1:8080/metrics
得到如圖所示結果
為了能監(jiān)控到這個端口為8080的目標,需要在prometheus的配置文件prometheus.yml進行一些修改
在scrape_configs塊部分加上一個新的job
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"] ? - job_name: 'python-client' ? ? scrape_interval: 5s ? ? static_configs: ? ? ? - targets: ['localhost:8080'] ? ? ? ? labels: ? ? ? ? ? group: 'python-client-group'
重啟prometheus,并訪問其web頁面,在Expression中輸入一個python client的metric并執(zhí)行
可以看到對應的結果正如在scrape_configs中所配置的相一致。
到此這篇關于使用python測試prometheus的實現(xiàn)的文章就介紹到這了,更多相關python測試prometheus內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python NumPy實現(xiàn)數(shù)組搜索示例詳解
NumPy是一個開源的Python科學計算庫,使用NumPy可以很自然地使用數(shù)組和矩陣,這篇文章主要介紹了使用NumPy實現(xiàn)數(shù)組搜索,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習吧2023-05-05Pandas?DataFrame列快速轉換為列表(3秒學會!)
這篇文章主要給大家介紹了關于Pandas?DataFrame列如何快速轉換為列表的相關資料,在Python的pandas庫中可以使用DataFrame的tolist()方法將DataFrame轉化為列表,需要的朋友可以參考下2023-10-10Python操作MySQL數(shù)據(jù)庫的入門指南
MySQL是一種關系型數(shù)據(jù)庫管理系統(tǒng),廣泛應用于各種應用程序和網(wǎng)站,在本篇技術博客中,我們將探討如何使用Python操作MySQL數(shù)據(jù)庫,需要的可以收藏一下2023-06-06