How expose metrics in multiprocess app use start_http_server
I found many examples with gunicorn in internet but i want use start_http_server
what should i do with code below to make it work properly?
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)
I was figuring out the same thing, and the solution was as simple as you would imagine.
Updated your example code to work:
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