'Prometheus how to expose metrics in multiprocess app with start_http_server

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)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source