脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 使用python测试prometheus的实现

使用python测试prometheus的实现

2022-09-15 11:01quietguoguo Python

本文主要介绍了使用python测试prometheus的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

为了更直观的了解prometheus如何工作,本文使用prometheus的python库来做一些相应的测试。

python库的github地址是https://github.com/prometheus

根据提示,使用pip安装prometheus_client

?
1
pip3 install prometheus_client

然后根据文档中的示例文件并简单修改,运行一个client

文件命名为prometheus_python_client.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

?
1
pytho3 prometheus_python_client.py 8080 &

此时可以访问本机的8080端口,可以看到相应的metric

?
1
curl 127.0.0.1:8080/metrics

得到如图所示结果

使用python测试prometheus的实现

为了能监控到这个端口为8080的目标,需要在prometheus的配置文件prometheus.yml进行一些修改

在scrape_configs块部分加上一个新的job

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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并执行

可以看到对应的结果正如在scrape_configs中所配置的相一致。

使用python测试prometheus的实现

到此这篇关于使用python测试prometheus的实现的文章就介绍到这了,更多相关python测试prometheus内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.51cto.com/quietguoguo/4982239

延伸 · 阅读

精彩推荐
  • Pythonpython制作花瓣网美女图片爬虫

    python制作花瓣网美女图片爬虫

    本文通过python 来实现这样一个简单的爬虫功能,把我们想要的图片爬取到本地,需要的朋友可以参考下...

    脚本之家5522020-08-01
  • PythonPython3 能振兴 Python的原因分析

    Python3 能振兴 Python的原因分析

    我从Stephen A. Goss那读到关于了《Python 3正在毁灭Python》。这篇文章有不少精彩的论点,但我却并不认为Python 3是在毁灭Python,也不认为整个局面对Python一点也...

    脚本之家4892020-05-16
  • Python详解Python nose单元测试框架的安装与使用

    详解Python nose单元测试框架的安装与使用

    本篇文章主要介绍了详解Python nose单元测试框架的安装与使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    天外归云6552020-12-26
  • Python关于探究python中sys.argv时遇到的问题详解

    关于探究python中sys.argv时遇到的问题详解

    这篇文章主要给大家介绍了python里sys.argv时遇到问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需...

    凡平199711912021-09-08
  • Python将Emacs打造成强大的Python代码编辑工具

    将Emacs打造成强大的Python代码编辑工具

    这篇文章主要介绍了将Emacs打造成强大的Python代码编辑工具的方法,人们常说Vim是编辑器之神而Emacs是神的编辑器:)需要的朋友可以参考下...

    CoderZh2712020-08-03
  • Pythonpycharm重置设置,恢复默认设置的方法

    pycharm重置设置,恢复默认设置的方法

    今天小编就为大家分享一篇pycharm重置设置,恢复默认设置的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    jonnyan22672021-04-10
  • PythonOpenCV-PS扩散毛玻璃效果的实现代码

    OpenCV-PS扩散毛玻璃效果的实现代码

    PS的扩散效果可以产生类似毛玻璃质感的效果,使画面有些毛毛的感觉。接下来通过本文给大家分享OpenCV-PS扩散毛玻璃效果的实现代码,一起看看吧...

    翟天保Steven11772021-12-29
  • Pythonpython编程webpy框架模板之def with学习

    python编程webpy框架模板之def with学习

    这篇文章主要为大家介绍了python编程web.py框架模板之def with的学习有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步...

    枫少文11682022-03-04