prometheus / prometheus/client_python
How to measure time spent by collector.collect?
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 4.4k
- 派生
- 876
- 平均合并
- 8 天 4 小时
- 30 天内合并 PR
- 1
描述
Having a 3rd-party application which can not expose any metrics by itself I wrote my own exporter which takes data from the app and exposes metrics. The next idea was to measure and expose to prometheus an exact time it takes for all of the collectors to get actual data from the app. But two different approaches - with collector.collect() decorator and measurements taken directly inside of collector.collect() yields different results, and I don't understand what the decorator is actually measures.
Could you please explain this difference or maybe I'm missing something?
import time
from typing import Iterable
from prometheus_client import make_wsgi_app, Gauge
from prometheus_client.core import REGISTRY, GaugeMetricFamily, Metric
from prometheus_client.registry import Collector
from wsgiref.simple_server import make_server
COLLECTOR_TIME = Gauge('collector_spent_secods', 'Time spent by collector to get data', ['collector'])
class MetricsCollector(Collector):
@COLLECTOR_TIME.labels('decorator').time()
def collect(self) -> Iterable[Metric]:
st = time.perf_counter()
gauge = GaugeMetricFamily(
name='some_metric_seconds',
documentation='Some seconds metric',
labels=['somename']
)
time.sleep(1)
gauge.add_metric(['foo'], 3.0)
COLLECTOR_TIME.labels('inside').set(time.perf_counter() - st)
yield gauge
if __name__ == "__main__":
REGISTRY.register(MetricsCollector())
app = make_wsgi_app(REGISTRY)
host = '0.0.0.0'
port = 6543
httpd = make_server(host, port, app)
httpd.serve_forever()
$ curl localhost:6543 2>/dev/null | egrep -v '^#' | grep 'collector_spent_secods'
collector_spent_secods{collector="decorator"} 7.499998901039362e-06
collector_spent_secods{collector="inside"} 1.0012022000155412
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从所示的 Collector.collect 生成器和 Gauge.time() 装饰器开始,然后将装饰器的计时与 collect() 内部的代码进行比较。解释两次测量结果为何不同,并记录该行为,以便用户能够正确测量 collector 的收集时间。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- prometheus, python
- 领域
- observability
- Issue 类型
- 文档
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100