Switch multi-process support to custom data store. · pythonAI/client_python@f85ccd9 · GitHub
Skip to content

Commit f85ccd9

Browse files
committed
Switch multi-process support to custom data store.
This uses an mmaped file to store the data. Fsync is not called, and concurrent readers are permitted (which some shelve backends did not permit). Microbenchmarks indicate 1.2-1.5us per instrumentation event. This compares to 3-13ms from the shelve solution, so 3 orders of magnitude better. The 1.2-1.5us is about double normal multi-process instrumentation, due to the 2nd mutex required. Python doesn't offer an RWLock which would let this be reduced. Overall this performance is acceptable, as if someone was doing something extremely performance critical with instrumentation they wouldn't be using Python.
1 parent d3256c1 commit f85ccd9

3 files changed

Lines changed: 137 additions & 25 deletions

File tree

prometheus_client/core.py

Lines changed: 104 additions & 23 deletions

prometheus_client/multiprocess.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def collect(self):
2121
for f in glob.glob(os.path.join(self._path, '*.db')):
2222
parts = os.path.basename(f).split('_')
2323
typ = parts[0]
24-
for key, value in shelve.open(f).items():
24+
d = core._MmapedDict(f)
25+
for key, value in d.read_all_values():
2526
metric_name, name, labelnames, labelvalues = json.loads(key)
2627
metrics.setdefault(metric_name, core.Metric(metric_name, 'Multiprocess metric', typ))
2728
metric = metrics[metric_name]
@@ -30,8 +31,9 @@ def collect(self):
3031
metric._multiprocess_mode = parts[1]
3132
metric.add_sample(name, tuple(zip(labelnames, labelvalues)) + (('pid', pid), ), value)
3233
else:
33-
# The deplucates and labels are fixed in the next for.
34+
# The duplicates and labels are fixed in the next for.
3435
metric.add_sample(name, tuple(zip(labelnames, labelvalues)), value)
36+
d.close()
3537

3638
for metric in metrics.values():
3739
samples = {}

tests/test_multiprocess.py

Lines changed: 29 additions & 0 deletions

0 commit comments

Comments
 (0)