Pav*_*tov 6 python multiprocessing multiprocess prometheus
如何使用 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)
Run Code Online (Sandbox Code Playgroud)
pot*_*non 12
我也在想同样的事情,解决方案就像你想象的那么简单。
更新了您的示例代码以使其正常工作:
from multiprocessing import Process
import shutil
import time, os
from prometheus_client import start_http_server, multiprocess, CollectorRegistry, Counter
# 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)
MY_COUNTER = Counter('my_counter', 'Description of my counter')
def f():
while True:
time.sleep(1)
print("+1")
MY_COUNTER.inc()
if __name__ == '__main__':
# pass the registry to server
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry)
start_http_server(8000, registry=registry)
p = Process(target=f, args=())
a = p.start()
p2 = Process(target=f, args=())
p2.start()
print("collect")
while True:
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
localhost:8000/metrics
# HELP my_counter_total Multiprocess metric
# TYPE my_counter_total counter
my_counter_total 16.0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6129 次 |
| 最近记录: |