nginx prometheus客户端中的推到网关功能

cmssoen2  于 2023-03-22  发布在  Nginx
关注(0)|答案(1)|浏览(125)

我想通过push_to_gateway函数将指标推送到Prometheus服务器,但我在Prometheus服务器上设置了基本身份验证。如何在push_to_gateway函数中发送用户名和密码?

camsedfj

camsedfj1#

这是官方案例,你可以试着用它来测试
此型号https://pypi.org/project/prometheus-client/
pip install prometheus-client

from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
from prometheus_client.exposition import basic_auth_handler

def my_auth_handler(url, method, timeout, headers, data):
    username = 'foobar'
    password = 'secret123'
    return basic_auth_handler(url, method, timeout, headers, data, username, password)
registry = CollectorRegistry()
g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finished', registry=registry)
g.set_to_current_time()
push_to_gateway('localhost:9091', job='batchA', registry=registry, handler=my_auth_handler)

相关问题