Prometheus Python 客户端库

Prometheus Python client library

我开始使用 Prometheus 来获取我构建的服务的趋势数据。我正在尝试使用 Python 客户端库,但我不清楚如何使用它。

根据 "Getting started" 文档,有一个 prometheus.yml 文件指向您要监控的应用程序,Python 客户端库有此代码作为示例。

from prometheus_client import start_http_server, Summary
import random
import time

# 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__':
    # Start up the server to expose the metrics.
    start_http_server(8000)
    # Generate some requests.
    while True:
        process_request(random.random())

看起来它启动了自己的服务器,并不意味着与我的服务中的代码交织在一起。

所以我的问题是,我如何使用 Prometheus 客户端来告诉 Prometheus 要从我的服务中监控哪些功能?

您应该将示例中的 Summary 等指标添加到您自己的代码中,它们将在端口 8000 上公开。