8000 Construct less tuples and dicts in accumulate · benxiaolang-hacker/client_python@2a56d5c · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a56d5c

Browse files
committed
Construct less tuples and dicts in accumulate
Signed-off-by: Aarni Koskela <akx@iki.fi>
1 parent fbed8d1 commit 2a56d5c

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

prometheus_client/multiprocess.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,22 @@ def _parse_key(key):
7575
def _accumulate_metrics(metrics, accumulate):
7676
for metric in metrics.values():
7777
samples = defaultdict(float)
78-
buckets = {}
78+
buckets = defaultdict(lambda: defaultdict(float))
79+
samples_setdefault = samples.setdefault
7980
for s in metric.samples:
80-
name, labels, value = s.name, s.labels, s.value
81+
name, labels, value, timestamp, exemplar = s
8182
if metric.type == 'gauge':
82-
without_pid = 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'))
8384
if metric._multiprocess_mode == 'min':
84-
current = samples.setdefault((name, without_pid), value)
85+
current = samples_setdefault(without_pid_key, value)
8586
if value < current:
86-
samples[(s.name, without_pid)] = value
87+
samples[without_pid_key] = value
8788
elif metric._multiprocess_mode == 'max':
88-
current = samples.setdefault((name, without_pid), value)
89+
current = samples_setdefault(without_pid_key, value)
8990
if value > current:
90-
samples[(s.name, without_pid)] = value
91+
samples[without_pid_key] = value
9192
elif metric._multiprocess_mode == 'livesum':
92-
samples[(name, without_pid)] += value
93+
samples[without_pid_key] += value
9394
else: # all/liveall
9495
samples[(name, labels)] = value
9596

@@ -98,16 +99,14 @@ def _accumulate_metrics(metrics, accumulate):
9899
if bucket:
99100
# _bucket
100101
without_le = tuple(l for l in labels if l[0] != 'le')
101-
buckets.setdefault(without_le, {})
102-
buckets[without_le].setdefault(bucket[0], 0.0)
103102
buckets[without_le][bucket[0]] += value
104103
else:
105104
# _sum/_count
106-
samples[(s.name, labels)] += value
105+
samples[(name, labels)] += value
107106

108107
else:
109108
# Counter and Summary.
110-
samples[(s.name, labels)] += value
109+
samples[(name, labels)] += value
111110

112111
# Accumulate bucket values.
113112
if metric.type == 'histogram':

0 commit comments

Comments
 (0)
0