/
prometheus.py
144 lines (118 loc) · 4.39 KB
/
prometheus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from __future__ import annotations
import os
import sys
import typing as t
import logging
from typing import TYPE_CHECKING
from functools import partial
if TYPE_CHECKING:
from prometheus_client.metrics_core import Metric
from ... import external_typing as ext
logger = logging.getLogger(__name__)
class PrometheusClient:
def __init__(
self,
*,
multiproc: bool = True,
multiproc_dir: str | None = None,
):
"""
Set up multiproc_dir for prometheus to work in multiprocess mode,
which is required when working with Gunicorn server
Warning: for this to work, prometheus_client library must be imported after
this function is called. It relies on the os.environ['PROMETHEUS_MULTIPROC_DIR']
to properly setup for multiprocess mode
"""
if multiproc:
assert multiproc_dir is not None, "multiproc_dir must be provided"
self.multiproc = multiproc
self.multiproc_dir: str | None = multiproc_dir
self._registry = None
self._imported = False
self._pid: int | None = None
@property
def prometheus_client(self):
if self.multiproc and not self._imported:
# step 1: check environment
assert (
"prometheus_client" not in sys.modules
), "prometheus_client is already imported, multiprocessing will not work properly"
assert (
self.multiproc_dir
), f"Invalid prometheus multiproc directory: {self.multiproc_dir}"
assert os.path.isdir(self.multiproc_dir)
os.environ["PROMETHEUS_MULTIPROC_DIR"] = self.multiproc_dir
# step 2:
import prometheus_client
import prometheus_client.parser
import prometheus_client.multiprocess
self._imported = True
return prometheus_client
@property
def registry(self):
if self._registry is None:
if self.multiproc:
self._pid = os.getpid()
self._registry = self.prometheus_client.REGISTRY
else:
if self.multiproc:
assert self._pid is not None
assert (
os.getpid() == self._pid
), "The current process's different than the process which the prometheus client gets created"
return self._registry
def __del__(self):
self.mark_process_dead()
def mark_process_dead(self) -> None:
if self.multiproc:
assert self._pid is not None
assert (
os.getpid() == self._pid
), "The current process's different than the process which the prometheus client gets created"
self.prometheus_client.multiprocess.mark_process_dead(self._pid)
def start_http_server(self, port: int, addr: str = "") -> None:
self.prometheus_client.start_http_server(
port=port,
addr=addr,
registry=self.registry,
)
def make_wsgi_app(self) -> ext.WSGIApp:
return self.prometheus_client.make_wsgi_app(registry=self.registry) # type: ignore (unfinished prometheus types)
def generate_latest(self):
if self.multiproc:
registry = self.prometheus_client.CollectorRegistry()
self.prometheus_client.multiprocess.MultiProcessCollector(registry)
return self.prometheus_client.generate_latest(registry)
else:
return self.prometheus_client.generate_latest()
def text_string_to_metric_families(self) -> t.Generator[Metric, None, None]:
yield from self.prometheus_client.parser.text_string_to_metric_families(
self.generate_latest().decode("utf-8")
)
@property
def CONTENT_TYPE_LATEST(self) -> str:
return self.prometheus_client.CONTENT_TYPE_LATEST
@property
def Histogram(self):
return partial(
self.prometheus_client.Histogram,
registry=self.registry,
)
@property
def Counter(self):
return partial(
self.prometheus_client.Counter,
registry=self.registry,
)
@property
def Summary(self):
return partial(
self.prometheus_client.Summary,
registry=self.registry,
)
@property
def Gauge(self):
return partial(
self.prometheus_client.Gauge,
registry=self.registry,
)