prometheus python客户端和工具库
pyprometheus的Python项目详细描述
Prometheus的非官方Python 2和3客户端
功能
- 支持四种度量:counter、gauge、summary(不带分位数)和histogram。
- InMemoryStorage(不要用于多处理应用程序)
- uwsgi存储-进程之间共享度量
- uwagi flush storage-通过flush调用与uwsgi sharedera同步度量
- 时间装饰器
- 时间上下文管理器
安装
要使用pyprometheus,请使用pip或简易安装:
pip install pyprometheus
或
easy_install pyprometheus
如何检测代码
仪表
量表是一个度量单位,表示一个可以任意上下移动的数值
from pyprometheus import Gauge from pyprometheus import BaseRegistry, LocalMemoryStorage storage = LocalMemoryStorage() registry = CollectorRegistry(storage=storage) gauge = Gauge("job_in_progress", "Description", registry=registry) gauge.inc(10) gauge.dec(5) gauge.set(21.1)
实用程序:
gauge.set_to_current_time() # Set to current unixtime # Increment when entered, decrement when exited. @gauge.track_in_progress() def f(): pass with gauge.track_in_progress(): pass with gauge.time(): time.sleep(10)
计数器
计数器是一个累积度量,它表示一个仅上升的数值
from pyprometheus import Counter from pyprometheus import BaseRegistry, LocalMemoryStorage storage = LocalMemoryStorage() registry = CollectorRegistry(storage=storage) counter = Counter("requests_total", "Description", registry=registry) counter.inc(10)
小结
与柱状图类似,总结样本观察(通常是请求持续时间和响应大小)
from pyprometheus import Summary from pyprometheus import BaseRegistry, LocalMemoryStorage storage = LocalMemoryStorage() registry = CollectorRegistry(storage=storage) s = Summary("requests_duration_seconds", "Description", registry=registry) s.observe(0.100)
计时代码实用程序:
@gauge.time() def func(): time.sleep(10) with gauge.time(): time.sleep(10)
直方图
直方图对观察结果(通常是请求持续时间或响应大小)进行采样,并在可配置的存储桶中对其进行计数。它还提供所有观测值的总和。
from pyprometheus import Summary from pyprometheus import BaseRegistry, LocalMemoryStorage storage = LocalMemoryStorage() registry = CollectorRegistry(storage=storage) histogram = Histogram("requests_duration_seconds", "Description", registry=registry) histogram.observe(1.1)
计时代码实用程序:
@histogram.time() def func(): time.sleep(10) with histogram.time(): time.sleep(10)
标签
所有度量都可以有标签,允许对相关时间序列进行分组。
示例:
from pyprometheus import Counter c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint']) c.labels('get', '/').inc() c.labels('post', '/submit').inc()
或标记为关键字参数:
from pyprometheus import Counter c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint']) c.labels(method='get', endpoint='/').inc() c.labels(method='post', endpoint='/submit').inc()
存储量
当前库支持2个存储:LocalMemoryStorage和UWSGIStorage
每个注册表都必须有指向存储的链接:
from pyprometheus import BaseRegistry, LocalMemoryStorage storage = LocalMemoryStorage() registry = CollectorRegistry(storage=storage)
使用本地内存存储
将样本存储到应用程序内存的简单存储器。它可以和线一起使用
from pyprometheus import BaseRegistry, LocalMemoryStorage storage = LocalMemoryStorag()
使用uwsgistorage
uwsgistorage允许使用uwsgi sharedarea在进程之间同步度量。:
from pyprometheus.contrib.uwsgi_features import UWSGICollector, UWSGIStorage SHAREDAREA_ID = 0 storage = UWSGIStorage(SHAREDAREA_ID)
还需要配置UWSGI sharedead页面
导出
库中有一些帮助程序可导出度量值
文本格式
您可以将注册表转换为文本格式:
from pyprometheus import BaseRegistry, LocalMemoryStorage from pyprometheus.utils.exposition import registry_to_text from pyprometheus import Gauge storage = LocalMemoryStorage() registry = CollectorRegistry(storage=storage) g = Gauge('raid_status', '1 if raid array is okay', registry=registry) g.set(1) print(registry_to_text(registry))
文本文件导出
这对于监视cronjobs或编写cronjobs以公开有关计算机系统的度量很有用。
from pyprometheus import BaseRegistry, LocalMemoryStorage from pyprometheus.utils.exposition import registry_to_text, write_to_textfile from pyprometheus import Gauge storage = LocalMemoryStorage() registry = CollectorRegistry(storage=storage) g = Gauge('raid_status', '1 if raid array is okay', registry=registry) g.set(1) write_to_textfile(registry, "/path/to/file/metrics.prom")
您可以将text file collector配置为使用生成的文件。
待办事项
我们计划执行的一些功能:
- []添加mmap存储
- []为异步框架添加功能
- []优化UWSGI存储字节板
- []添加分位数
示例项目
我们创建example project来显示在实际项目中使用pyprometheus的热度。
贡献
forkhttps://github.com/Lispython/pyprometheus/,创建提交并将请求拉到develop。