From 350d24b57137b557769df4ec6e0d324919a33683 Mon Sep 17 00:00:00 2001 From: Max Usachev Date: Wed, 21 Mar 2018 13:22:32 +0300 Subject: [PATCH] Reduce unnecessary metric creation in loop --- prometheus_client/multiprocess.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/prometheus_client/multiprocess.py b/prometheus_client/multiprocess.py index 1543fca6..4f78261e 100644 --- a/prometheus_client/multiprocess.py +++ b/prometheus_client/multiprocess.py @@ -28,8 +28,12 @@ def collect(self): d = core._MmapedDict(f) for key, value in d.read_all_values(): metric_name, name, labelnames, labelvalues = json.loads(key) - metrics.setdefault(metric_name, core.Metric(metric_name, 'Multiprocess metric', typ)) - metric = metrics[metric_name] + + metric = metrics.get(metric_name) + if metric is None: + metric = core.Metric(metric_name, 'Multiprocess metric', typ) + metrics[metric_name] = metric + if typ == 'gauge': pid = parts[2][:-3] metric._multiprocess_mode = parts[1]