使用python测试prometheus的实现

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

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

根据提示,使用pip安装prometheus_client

pip3 install prometheus_client

然后根据文档中的示例文件并简单修改,运行一个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

得到如图所示结果

为了能监控到这个端口为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并执行

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

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

(0)

相关推荐

  • Prometheus的安装和配置教程详解

    1. 从官网选择Prometheus版本进行下载 官网地址>> https://github.com/prometheus/prometheus/releases/ 2. 实验安排 在主机192.168.153.137上安装prometheus监控192.168.153.138上的mysql服务和主机状态 3. 上传软件包到137服务器并配置 3.1 将软件包解压到 /usr/local 目录下 tar xzf prometheus-2.24.1.linux-amd64.tar.gz -C /

  • springboot2.X整合prometheus监控的实例讲解

    springboot2.x暴露健康状况通过prometheus监控 加入依赖 <!--prometheus监控 https://prometheus.io/docs/introduction/overview/--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId>

  • Prometheus 整合 AlertManager的教程详解

    简介 Alertmanager 主要用于接收 Prometheus 发送的告警信息,它很容易做到告警信息的去重,降噪,分组,策略路由,是一款前卫的告警通知系统.它支持丰富的告警通知渠道,可以将告警信息转发到邮箱.企业微信.钉钉等.这一节讲解利用AlertManager,把接受到的告警信息,转发到邮箱. 实验 准备 启动 http-simulator 度量模拟器: docker run --name http-simulator -d -p 8080:8080 pierrevincent/prom

  • 使用Python编写Prometheus监控的方法

    要使用python编写Prometheus监控,需要你先开启Prometheus集群.可以参考//www.jb51.net/article/148895.htm 安装.在python中实现服务器端.在Prometheus中配置请求网址,Prometheus会定期向该网址发起申请获取你想要返回的数据. 使用Python和Flask编写Prometheus监控 Installation pip install flask pip install prometheus_client Metrics P

  • 一文秒懂Prometheus 介绍及工作原理

    1. Prometheus 介绍 Prometheus是一套开源的系统监控报警框,相比Nagios或者Zabbix拥有如下优点 1.1 易管理性 Prometheus: Prometheus核心部分只有一个单独的二进制文件,可直接在本地工作,不依赖于分布式存储. 1.2 业务数据相关性 Prometheus:监控服务的运行状态,基于Prometheus丰富的Client库,用户可以轻松的在应用程序中添加对Prometheus的支持,从而让用户可以获取服务和应用内部真正的运行状态. 1.3 高效:

  • prometheus监控springboot应用简单使用介绍详解

    对于springboot应用,需要以下几个步骤 springboot应用开启endpoint,添加actuator的以来和promethus的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> &

  • 使用python测试prometheus的实现

    为了更直观的了解prometheus如何工作,本文使用prometheus的python库来做一些相应的测试. python库的github地址是https://github.com/prometheus 根据提示,使用pip安装prometheus_client pip3 install prometheus_client 然后根据文档中的示例文件并简单修改,运行一个client 文件命名为prometheus_python_client.py from prometheus_client i

  • python测试mysql写入性能完整实例

    本文主要研究的是python测试mysql写入性能,分享了一则完整代码,具体介绍如下. 测试环境: (1) 阿里云服务器centos 6.5 (2) 2G内存 (3) 普通硬盘 (4) mysql 5.1.73 数据库存储引擎为 InnoDB (5) python 2.7 (6) 客户端模块 mysql.connector 测试方法: (1) 普通写入 (2) 批量写入 (3) 事务加批量写入 普通写入: def ordinary_insert(count): sql = "insert int

  • Python测试网络连通性示例【基于ping】

    本文实例讲述了Python测试网络连通性.分享给大家供大家参考,具体如下: Python代码 #!/usr/bin/python # -*- coding:GBK -*- """Document: network script, keep network always working, using python3""" import os import time PING_RESULT = 0 NETWORK_RESULT = 0 def Dis

  • 解决python测试opencv时imread导致的错误问题

    如下所示: import cv2 import numpy as np img = cv2.imread("1.jpg")//图片和代码在同个目录,改为相对路径,解决由imread引起的打不开图片或找不到图片的路径问题 cv2.imshow("1",img) cv2.waitKey(10000) D:\>my.py OpenCV Error: Assertion failed (size.width>0 && size.height>

  • 使用Python测试Ping主机IP和某端口是否开放的实例

    使用Python方法 比用各种命令方便,可以设置超时时间,到底通不通,端口是否开放一眼能看出来. 命令和返回 完整权限,可以ping通,端口开放,结果如下: 无root权限(省略了ping),端口开放,结果如下: 完整权限,可以ping通,远端端口关闭,结果如下: 完整权限,可以ping通,本地端口关闭,结果如下: 完整权限,不能ping通(端口自然也无法访问),结果如下: pnp.py代码 #!/usr/bin/python #name pnp.py #ping and port #codin

  • Python测试Kafka集群(pykafka)实例

    生产者代码: # -* coding:utf8 *- from pykafka import KafkaClient host = 'IP:9092, IP:9092, IP:9092' client = KafkaClient(hosts = host) print client.topics # 生产者 topicdocu = client.topics['my-topic'] producer = topicdocu.get_producer() for i in range(100):

  • Python测试线程应用程序过程解析

    这篇文章主要介绍了Python测试线程应用程序过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在本章中,我们将学习线程应用程序的测试.我们还将了解测试的重要性. 为什么要测试? 在我们深入讨论测试的重要性之前,我们需要知道测试的内容.一般来说,测试是一种了解某些东西是如何运作的技术.另一方面,特别是如果我们谈论计算机程序或软件,那么测试就是访问软件程序功能的技术. 在本节中,我们将讨论软件测试的重要性.在软件开发中,必须在向客户端发布软

  • Python 测试框架unittest和pytest的优劣

    一.Unittest Unittest是Python标准库中自带的单元测试框架,Unittest有时候也被称为PyUnit,就像JUnit是Java语言的标准单元测试框架一样,Unittest则是Python语言的标准单元测试框架. Unittest支持自动化测试,测试用例的初始化.关闭和测试用例的聚合等功能,它有一个很重要的特性:它是通过类(class)的方式,将测试用例组织在一起. 示例: 运行结果 注:unittest有一个关联模块unittest2,但unittest2仅适用于Pytho

  • Python测试框架:pytest学习笔记

     python通用测试框架大多数人用的是unittest+HTMLTestRunner,这段时间看到了pytest文档,发现这个框架和丰富的plugins很好用,所以来学习下pytest. pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个特点: 简单灵活,容易上手 支持参数化 能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试.接口自动化测试(pytest+requests) pytest具有很多第三方插件,并且可以自定义扩展

随机推荐