用于基于异步应用程序的prometheus python客户端库

aioprometheus的Python项目详细描述


https://travis-ci.org/claws/aioprometheus.svg?branch=masterhttps://img.shields.io/pypi/v/aioprometheus.svg

氨甲氧磷

aioprometheus是一个基于异步的prometheus python客户端库 应用。它提供指标收集和服务能力, 支持多种数据格式并将度量值推送到网关。

项目文档可以在 ReadTheDocs

安装

$ pip install aioprometheus

示例

下面的示例显示正在创建的单个计数器度量收集器 并通过http端点公开。

#!/usr/bin/env python"""
This example demonstrates how a single Counter metric collector can be created
and exposed via a HTTP endpoint.
"""importasyncioimportsocketfromaioprometheusimportCounter,Serviceif__name__=="__main__":asyncdefmain(svr:Service)->None:events_counter=Counter("events","Number of events.",const_labels={"host":socket.gethostname()})svr.register(events_counter)awaitsvr.start(addr="127.0.0.1",port=5000)print(f"Serving prometheus metrics on: {svr.metrics_url}")# Now start another coroutine to periodically update a metric to# simulate the application making some progress.asyncdefupdater(c:Counter):whileTrue:c.inc({"kind":"timer_expiry"})awaitasyncio.sleep(1.0)awaitupdater(events_counter)loop=asyncio.get_event_loop()svr=Service()try:loop.run_until_complete(main(svr))exceptKeyboardInterrupt:passfinally:loop.run_until_complete(svr.stop())loop.close()

在这个简单的例子中,计数器度量跟踪 更新程序协同程序执行的while循环迭代。现实主义 应用一个度量可以跟踪请求的数量等。

遵循典型的asyncio用法,首先实例化事件循环 然后实例化度量服务。度量服务负责 用于管理度量收集器和响应度量请求。

服务接受各种参数,如要绑定的接口和端口 到。在服务中使用收集器注册表来保存度量 将由服务公开的收集器。服务将创建一个新的 如果未传入收集器注册表。

将创建计数器度量并向服务注册。服务是 开始,然后开始一个协程来定期更新度量 模拟进度。

可以使用以下命令运行示例脚本:

(venv) $cd examples
(venv) $ python simple-example.py
Serving prometheus metrics on: http://127.0.0.1:5000/metrics

在另一个终端中,使用curl命令行工具获取度量 以验证它们能被普罗米修斯服务器检索到。

默认情况下,度量将以计划文本格式返回。

$ curl http://127.0.0.1:5000/metrics
# HELP events Number of events.
# TYPE events counter
events{host="alpha",kind="timer_expiry"} 33

类似地,您可以请求二进制格式的度量,尽管这将很困难 在命令行上读取。

$ curl http://127.0.0.1:5000/metrics -H "ACCEPT: application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited"

metrics服务还响应发送到其/路由的请求。这个 响应是简单的html。此路由可以用作kubernetes/healthz 样式健康指示器,因为它不会在服务中产生任何开销 序列化完整的度量响应。

$ curl http://127.0.0.1:5000/
<html><body><a href='/metrics'>metrics</a></body></html>

aiopromtheus包提供了许多方便的decorator 有助于更新度量的功能。

这里examples目录包含许多示例,说明如何使用 Aioprometheus套餐。可能会对app-example.py文件感兴趣 因为它提供了一个更具代表性的应用程序示例 上面显示的示例。

examples/frameworks目录中的示例显示了aiopromtheus如何 在现有的AIOHTTP、QUART和VIBORA应用程序中使用,而不是使用 创建一个单独的aiopromtheus.service端点来处理度量。这个 Vibora示例如下所示。

#!/usr/bin/env python"""
Sometimes you want to expose Prometheus metrics from within an existing web
service and don't want to start a separate Prometheus metrics server.

This example uses the aioprometheus package to add Prometheus instrumentation
to a Vibora application. In this example a registry and a counter metric is
instantiated. A '/metrics' route is added to the application and the render
function from aioprometheus is called to format the metrics into the
appropriate format.
"""fromaioprometheusimportrender,Counter,RegistryfromviboraimportVibora,Request,Responseapp=Vibora(__name__)app.registry=Registry()app.events_counter=Counter("events","Number of events.")app.registry.register(app.events_counter)@app.route("/")asyncdefhello(request:Request):app.events_counter.inc({"path":"/"})returnResponse(b"hello")@app.route("/metrics")asyncdefhandle_metrics(request:Request):"""
    Negotiate a response format by inspecting the ACCEPTS headers and selecting
    the most efficient format. Render metrics in the registry into the chosen
    format and return a response.
    """content,http_headers=render(app.registry,[request.headers.get("accept")])returnResponse(content,headers=http_headers)app.run()

许可证

aiopromtheus是根据麻省理工学院的许可证发布的。

aiopromtheus源于(现在已弃用) prometheus python包装 在麻省理工学院的许可下被释放。继续使用麻省理工学院 许可证,并包含来自 prometheus python按照原始许可证的指示进行项目。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在C中嵌入jar文件#   java如何快速计算2^x?   java 安卓另一个类sendSMS和调用Main活动错误   java将两个列表组合起来,并在匹配后设置值   java定制Zuul异常   java在webapp中检测用户区域的可靠方法是什么?   java多线程访问ArrayList   使用Java将xml Post请求有效负载发送到SOAP Web服务   反射在不知道包名的情况下加载特定文件夹中的所有类   java Eclipse不启动:退出代码13   java为什么我需要在ListIterator中调用两次previous(),以便进行“反向”迭代?   java为什么同步在以下代码中不起作用?   使用JSF和PrimeFaces的java自适应轮询   Java通过仅使用iText限制比例和缩小比例来转换图像   swing Java repaint()在侦听器中的行为与在其余代码中的行为不同