从 API 调用 3rd 方系统获取数据并将其显示在 Prometheus 中

Get data from API call to 3rd party system and display it in Promethues

我是 Prometheus 的新手,正在尝试使用 python 库编写自定义导出器。为此,我使用 prometheus_client.

我的最终目标是监控我的保管库节点。我有许多 API 可以用来为我的保管库节点收集指标。最后,我希望我的 promethues dahsboard 显示如下内容:

vault_total_conection <some-number>
vault_total_secrets <some-number>

等等。

我从 https://github.com/prometheus/client_python 获得的基本 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())

现在我已经搞定了 API 保险库的设置。我有一个联系保险库的功能和 returns 一个浮点数。

def extract_metric_from_vault():
    // some code
    return float_number

所以在上面的代码中定义了这个函数。我很难理解的是如何将其与 promethue 客户端集成。我想使用 Gauge,因为我知道值会高或低。

所以我尝试做一些事情:

TEST_VALUE   = Gauge('vault_total_conection', 'Description of gauge')
TEST_VALUE.extract_metric_from_vault()

但这显然行不通。

我收到错误:

Traceback (most recent call last):
  File "main.yaml", line 8, in <module>
    TEST_VALUE.extract_metric_from_vault()
AttributeError: 'Gauge' object has no attribute 'extract_metric_from_vault'

所以有人可以指导我需要什么来连接这里的点。我想使用函数从 API 调用中提取一些值并将其显示在普罗米修斯中。

AFAIK 你需要在 main()

中调用你的函数
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(8000)
# Generate some requests.
while True:
    extract_metric_from_vault()

然后,你需要gauge的值

TEST_VALUE.set(<extracted value>)