8000 Use less genexprs in multiprocess accumulate · ethervoid/client_python@0cfb054 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0cfb054

Browse files
committed
Use less genexprs in multiprocess accumulate
Signed-off-by: Aarni Koskela <akx@iki.fi>
1 parent 2a56d5c commit 0cfb054

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

prometheus_client/multiprocess.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def _accumulate_metrics(metrics, accumulate):
8080
for s in metric.samples:
8181
name, labels, value, timestamp, exemplar = s
8282
if metric.type == 'gauge':
83-
without_pid_key = (name, tuple(l for l in labels if l[0] != 'pid'))
83+
without_pid_key = (name, tuple([l for l in labels if l[0] != 'pid']))
8484
if metric._multiprocess_mode == 'min':
8585
current = samples_setdefault(without_pid_key, value)
8686
if value < current:
@@ -95,15 +95,18 @@ def _accumulate_metrics(metrics, accumulate):
9595
samples[(name, labels)] = value
9696

9797
elif metric.type == 'histogram':
98-
bucket = tuple(float(l[1]) for l in labels if l[0] == 'le')
99-
if bucket:
100-
# _bucket
101-
without_le = tuple(l for l in labels if l[0] != 'le')
102-
buckets[without_le][bucket[0]] += value
103-
else:
98+
# A for loop with early exit is faster than a genexpr
99+
# or a listcomp that ends up building unnecessary things
100+
for l in labels:
101+
if l[0] == 'le':
102+
bucket_value = float(l[1])
103+
# _bucket
104+
without_le = tuple(l for l in labels if l[0] != 'le')
105+
buckets[without_le][bucket_value] += value
106+
break
107+
else: # did not find the `le` key
104108
# _sum/_count
105109
samples[(name, labels)] += value
106-
107110
else:
108111
# Counter and Summary.
109112
samples[(name, labels)] += value

0 commit comments

Comments
 (0)
0