如何在多进程应用程序中使用 start_http_server 公开指标
我在互联网上找到了很多gunicorn的例子,但我想使用start_http_server
我应该如何处理下面的代码才能使其正常工作?
from multiprocessing import Process
import time, os
from prometheus_client import start_http_server, multiprocess, CollectorRegistry, Counter
MY_COUNTER = Counter('my_counter', 'Description of my counter')
os.environ["PROMETHEUS_MULTIPROC_DIR"] = "tmp"
def f():
print("+1")
MY_COUNTER.inc()
if __name__ == '__main__':
start_http_server(8000)
p = Process(target=f, args=())
a = p.start()
p2 = Process(target=f, args=())
p2.start()
time.sleep(1)
print("collect")
registry = CollectorRegistry()
data = multiprocess.MultiProcessCollector(registry)
while True:
time.sleep(1)
我也在想同样的事情,解决方案就像你想象的那么简单。
更新了您的示例代码以使其正常工作:
from multiprocessing import Process
import shutil
import time, os
from prometheus_client import start_http_server, multiprocess, CollectorRegistry, Counter
COUNTER1 = Counter('counter1', 'Incremented by the first child process')
COUNTER2 = Counter('counter2', 'Incremented by the second child process')
COUNTER3 = Counter('counter3', 'Incremented by both child processes')
def f1():
while True:
time.sleep(1)
print("Child process 1")
COUNTER1.inc()
COUNTER3.inc()
def f2():
while True:
time.sleep(1)
print("Child process 2")
COUNTER2.inc()
COUNTER3.inc()
if __name__ == '__main__':
# ensure variable exists, and ensure defined folder is clean on start
prome_stats = os.environ["PROMETHEUS_MULTIPROC_DIR"]
if os.path.exists(prome_stats):
shutil.rmtree(prome_stats)
os.mkdir(prome_stats)
# pass the registry to server
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry)
start_http_server(8000, registry=registry)
p = Process(target=f1, args=())
a = p.start()
p2 = Process(target=f2, args=())
p2.start()
print("collect")
while True:
time.sleep(1)
localhost:8000/metrics
# HELP counter1_total Incremented by the first child process
# TYPE counter1_total counter
counter1_total 9.0
# HELP counter2_total Incremented by the second child process
# TYPE counter2_total counter
counter2_total 9.0
# HELP counter3_total Incremented by both child processes
# TYPE counter3_total counter
counter3_total 18.0