使用python測(cè)試prometheus的實(shí)現(xiàn)
為了更直觀的了解prometheus如何工作,本文使用prometheus的python庫(kù)來(lái)做一些相應(yīng)的測(cè)試。
python庫(kù)的github地址是https://github.com/prometheus
根據(jù)提示,使用pip安裝prometheus_client
pip3 install prometheus_client
然后根據(jù)文檔中的示例文件并簡(jiǎn)單修改,運(yùn)行一個(gè)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 ( ))
在后臺(tái)運(yùn)行client
pytho3 prometheus_python_client.py 8080 &
此時(shí)可以訪問本機(jī)的8080端口,可以看到相應(yīng)的metric
curl 127.0.0.1:8080/metrics
得到如圖所示結(jié)果
為了能監(jiān)控到這個(gè)端口為8080的目標(biāo),需要在prometheus的配置文件prometheus.yml進(jìn)行一些修改
在scrape_configs塊部分加上一個(gè)新的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頁(yè)面,在Expression中輸入一個(gè)python client的metric并執(zhí)行
可以看到對(duì)應(yīng)的結(jié)果正如在scrape_configs中所配置的相一致。
到此這篇關(guān)于使用python測(cè)試prometheus的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python測(cè)試prometheus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python操作excel文件并輸出txt文件的實(shí)例
今天小編就為大家分享一篇python操作excel文件并輸出txt文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-07-07Selenium向iframe富文本框輸入內(nèi)容過程圖解
這篇文章主要介紹了Selenium向iframe富文本框輸入內(nèi)容過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Python實(shí)現(xiàn)使用dir獲取類的方法列表
今天小編就為大家分享一篇Python實(shí)現(xiàn)使用dir獲取類的方法列表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-12-12Python NumPy實(shí)現(xiàn)數(shù)組搜索示例詳解
NumPy是一個(gè)開源的Python科學(xué)計(jì)算庫(kù),使用NumPy可以很自然地使用數(shù)組和矩陣,這篇文章主要介紹了使用NumPy實(shí)現(xiàn)數(shù)組搜索,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-05-05Pandas?DataFrame列快速轉(zhuǎn)換為列表(3秒學(xué)會(huì)!)
這篇文章主要給大家介紹了關(guān)于Pandas?DataFrame列如何快速轉(zhuǎn)換為列表的相關(guān)資料,在Python的pandas庫(kù)中可以使用DataFrame的tolist()方法將DataFrame轉(zhuǎn)化為列表,需要的朋友可以參考下2023-10-10Python 保存矩陣為Excel的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Python 保存矩陣為Excel的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-01-01Python操作MySQL數(shù)據(jù)庫(kù)的入門指南
MySQL是一種關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),廣泛應(yīng)用于各種應(yīng)用程序和網(wǎng)站,在本篇技術(shù)博客中,我們將探討如何使用Python操作MySQL數(shù)據(jù)庫(kù),需要的可以收藏一下2023-06-06