使用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í)可以訪問(wèn)本機(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,并訪問(wèn)其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 cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)通過(guò)公共鍵對(duì)字典列表排序算法示例
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)通過(guò)公共鍵對(duì)字典列表排序算法,結(jié)合實(shí)例形式分析了Python基于operator模塊中的itemgetter()函數(shù)對(duì)字典進(jìn)行排序的相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
Python實(shí)現(xiàn)8個(gè)概率分布公式的方法詳解
在本文中,我們將介紹一些常見(jiàn)的分布(均勻分布、高斯分布、對(duì)數(shù)正態(tài)分布等)并通過(guò)Python代碼進(jìn)行可視化以直觀地顯示它們,感興趣的可以學(xué)習(xí)一下2022-05-05
Python的json模塊中json.load()和json.loads()的區(qū)別
這篇文章主要介紹了Python的json模塊中json.load()和json.loads()的區(qū)別,json.load用于從一個(gè)文件對(duì)象中讀取JSON數(shù)據(jù)并將其解析為Python對(duì)象,而json.loads用于解析一個(gè)JSON格式的字符串并將其轉(zhuǎn)換為Python對(duì)象,根據(jù)你的具體需求選擇使用哪個(gè)方法,需要的朋友可以參考下2024-12-12
基于Python+Tkinter實(shí)現(xiàn)音樂(lè)播放器
Tkinter 是 Python 中用于創(chuàng)建圖形用戶界面 (GUI) 的標(biāo)準(zhǔn)庫(kù)之一,它是一個(gè)簡(jiǎn)單而強(qiáng)大的工具,適用于創(chuàng)建各種類型的窗口應(yīng)用程序,本文給大家介紹了如何基于Python+Tkinter實(shí)現(xiàn)音樂(lè)播放器,需要的朋友可以參考下2025-03-03

