Coverage for src/couchers/metrics.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-07-20 21:46 +0000

1import threading 

2 

3from prometheus_client import Counter, Histogram, exposition 

4from prometheus_client.registry import CollectorRegistry 

5 

6JOB_LABEL = "job" 

7ATTEMPT_LABEL = "attempt" 

8STATUS_LABEL = "status" 

9METHOD_LABEL = "method" 

10CODE_LABEL = "code" 

11EXCEPTION_LABEL = "exception" 

12 

13main_process_registry = CollectorRegistry() 

14job_process_registry = CollectorRegistry() 

15jobs_counter = Counter( 

16 "jobs", 

17 "Durations of processing jobs", 

18 labelnames=(JOB_LABEL, STATUS_LABEL, ATTEMPT_LABEL, EXCEPTION_LABEL), 

19 registry=job_process_registry, 

20) 

21 

22servicer_duration_histogram = Histogram( 

23 "servicer_duration", 

24 "Durations of processing gRPC calls", 

25 labelnames=(METHOD_LABEL, CODE_LABEL, EXCEPTION_LABEL), 

26 buckets=(1, 5, 20, 50, 100, 200, 500, 1000, 5000, 10000), 

27) 

28 

29 

30def create_prometheus_server(registry, port): 

31 """custom start method to fix problem descrbied in https://github.com/prometheus/client_python/issues/155""" 

32 app = exposition.make_wsgi_app(registry) 

33 httpd = exposition.make_server( 

34 "", port, app, exposition.ThreadingWSGIServer, handler_class=exposition._SilentHandler 

35 ) 

36 t = threading.Thread(target=httpd.serve_forever) 

37 t.daemon = True 

38 t.start() 

39 return httpd