普罗米修斯监控一个简单的应用程序

promotheus monitoring a simple application

我正在尝试使用 prometheus 监控一个简单的应用程序,但不确定从哪里开始。

我创建了一个简单的测试函数,它将休眠 10 秒,并使用 prometheus - 摘要指标进行跟踪。

from prometheus_client import Summary
import time
from datetime import datetime

TF_CALL_SUMMARY = Summary("call_seconds", "Time spent inside test function")

def test():

    while True:
        t1 = datetime.now()
            time.sleep(10)      
            t2 = datetime.now()
            delta = t2 - t1 
        TF_CALL_SUMMARY.observe(delta.total_seconds())
        print delta

print 'start application'
test()
print 'end application'

现在这不是可以具有 /metric 端点​​的 Web 应用程序。

如何将此指标导出到我的 prometheus 服务器?

您需要将数据公开给 Promethueus:

from prometheus_client import start_http_server

if __name__ == '__main__':
    start_http_server(8000)

然后您需要 configure Prometheus 来抓取此数据源。在您的 Prometheus 配置文件中添加如下内容:

# Prometheus.yml
scrape_configs:
  - job: "python" 
    static_configs:
      - targets: ["localhost:8000"]

这里有一个 tutorial 详细说明了 Python 应用程序的检测,这对您有用。