8000 Use `collections.defaultdict` to speed up metrics aggregation by umax · Pull Request #257 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

Use collections.defaultdict to speed up metrics aggregation #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions prometheus_client/multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import unicode_literals

from collections import defaultdict

import glob
import json
import os
Expand Down Expand Up @@ -44,39 +46,38 @@ def collect(self):
d.close()

for metric in metrics.values():
samples = {}
samples = defaultdict(float)
buckets = {}
for name, labels, value in metric.samples:
if metric.type == 'gauge':
without_pid = tuple([l for l in labels if l[0] != 'pid'])
without_pid = tuple(l for l in labels if l[0] != 'pid')
if metric._multiprocess_mode == 'min':
samples.setdefault((name, without_pid), value)
if samples[(name, without_pid)] > value:
current = samples.setdefault((name, without_pid), value)
if value < current:
samples[(name, without_pid)] = value
elif metric._multiprocess_mode == 'max':
samples.setdefault((name, without_pid), value)
if samples[(name, without_pid)] < value:
current = samples.setdefault((name, without_pid), value)
if value > current:
samples[(name, without_pid)] = value
elif metric._multiprocess_mode == 'livesum':
samples.setdefault((name, without_pid), 0.0)
samples[(name, without_pid)] += value
else: # all/liveall
samples[(name, labels)] = value

elif metric.type == 'histogram':
bucket = [float(l[1]) for l in labels if l[0] == 'le']
bucket = tuple(float(l[1]) for l in labels if l[0] == 'le')
if bucket:
# _bucket
without_le = tuple([l for l in labels if l[0] != 'le'])
without_le = tuple(l for l in labels if l[0] != 'le')
buckets.setdefault(without_le, {})
buckets[without_le].setdefault(bucket[0], 0.0)
buckets[without_le][bucket[0]] += value
else:
# _sum/_count
samples.setdefault((name, labels), 0.0)
samples[(name, labels)] += value

else:
# Counter and Summary.
samples.setdefault((name, labels), 0.0)
samples[(name, labels)] += value

# Accumulate bucket values.
Expand Down
0