10000 Avoid duplicate JSON parsing and small allocations · benxiaolang-hacker/client_python@9c35671 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c35671

Browse files
committed
Avoid duplicate JSON parsing and small allocations
Signed-off-by: Aarni Koskela <akx@iki.fi>
1 parent db2a7f0 commit 9c35671

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

prometheus_client/multiprocess.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from .samples import Sample
1313
from .utils import floatToGoString
1414

15+
MP_METRIC_HELP = 'Multiprocess metric'
16+
1517

1618
class MultiProcessCollector(object):
1719
"""Collector for files for multi-process mode."""
@@ -39,17 +41,26 @@ def merge(files, accumulate=True):
3941
@staticmethod
4042
def _read_metrics(files):
4143
metrics = {}
44+
key_cache = {}
45+
46+
def _parse_key(key):
47+
val = key_cache.get(key)
48+
if not val:
49+
metric_name, name, labels = json.loads(key)
50+
labels_key = tuple(sorted(labels.items()))
51+
val = key_cache[key] = (metric_name, name, labels, labels_key)
52+
return val
53+
4254
for f in files:
4355
parts = os.path.basename(f).split('_')
4456
typ = parts[0]
4557
d = MmapedDict(f, read_mode=True)
4658
for key, value in d.read_all_values():
47-
metric_name, name, labels = json.loads(key)
48-
labels_key = tuple(sorted(labels.items()))
59+
metric_name, name, labels, labels_key = _parse_key(key)
4960

5061
metric = metrics.get(metric_name)
5162
if metric is None:
52-
metric = Metric(metric_name, 'Multiprocess metric', typ)
63+
metric = Metric(metric_name, MP_METRIC_HELP, typ)
5364
metrics[metric_name] = metric
5465

5566
if typ == 'gauge':

0 commit comments

Comments
 (0)
0