網(wǎng)站首頁 編程語言 正文
為了更直觀的了解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中所配置的相一致。
原文鏈接:https://blog.51cto.com/quietguoguo/4982239
相關推薦
- 2023-04-18 C#?TabControl手動觸發(fā)DrawItem的實現(xiàn)_C#教程
- 2022-07-26 arduino上傳程序出錯不成功常見的問題解決
- 2022-03-09 C語言直接插入排序算法介紹_C 語言
- 2022-01-30 uniapp H5刷新404問題解決 apache配置
- 2024-04-08 SpringBoot緩存注解@Cacheable、@CacheEvict和@CachePut
- 2022-07-07 Python如何在列表尾部添加元素_python
- 2022-02-20 Flutter TextFormField 監(jiān)聽focus、blur事件
- 2022-07-03 c語言中的二級指針做函數(shù)參數(shù)說明_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支