Coverage for mindsdb / metrics / server.py: 55%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1import os 

2 

3from flask import Flask, Response 

4from prometheus_client import generate_latest, multiprocess, CollectorRegistry 

5 

6from mindsdb.utilities import log 

7 

8_CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8') 

9logger = log.getLogger(__name__) 

10 

11 

12def init_metrics(app: Flask): 

13 prometheus_dir = os.environ.get('PROMETHEUS_MULTIPROC_DIR', None) 

14 if prometheus_dir is None: 14 ↛ 17line 14 didn't jump to line 17 because the condition on line 14 was always true

15 logger.info("PROMETHEUS_MULTIPROC_DIR environment variable is not set. Metrics server won't be started.") 

16 return 

17 elif not os.path.isdir(prometheus_dir): 

18 os.makedirs(prometheus_dir) 

19 # See: https://prometheus.github.io/client_python/multiprocess/ 

20 registry = CollectorRegistry() 

21 multiprocess.MultiProcessCollector(registry) 

22 

23 # It's important that the PROMETHEUS_MULTIPROC_DIR env variable is set, and the dir is empty. 

24 @app.route('/metrics') 

25 def metrics(): 

26 return Response(generate_latest(registry), mimetype=_CONTENT_TYPE_LATEST)