|
2 | 2 |
|
3 | 3 | from __future__ import unicode_literals
|
4 | 4 |
|
| 5 | +from collections import defaultdict |
| 6 | + |
5 | 7 | import glob
|
6 | 8 | import json
|
7 | 9 | import os
|
@@ -44,39 +46,38 @@ def collect(self):
|
44 | 46 | d.close()
|
45 | 47 |
|
46 | 48 | for metric in metrics.values():
|
47 |
| - samples = {} |
| 49 | + samples = defaultdict(float) |
48 | 50 | buckets = {}
|
49 | 51 | for name, labels, value in metric.samples:
|
50 | 52 | if metric.type == 'gauge':
|
51 |
| - without_pid = tuple([l for l in labels if l[0] != 'pid']) |
| 53 | + without_pid = tuple(l for l in labels if l[0] != 'pid') |
52 | 54 | if metric._multiprocess_mode == 'min':
|
53 |
| - samples.setdefault((name, without_pid), value) |
54 |
| - if samples[(name, without_pid)] > value: |
| 55 | + current = samples.setdefault((name, without_pid), value) |
| 56 | + if value < current: |
55 | 57 | samples[(name, without_pid)] = value
|
56 | 58 | elif metric._multiprocess_mode == 'max':
|
57 |
| - samples.setdefault((name, without_pid), value) |
58 |
| - if samples[(name, without_pid)] < value: |
| 59 | + current = samples.setdefault((name, without_pid), value) |
| 60 | + if value > current: |
59 | 61 | samples[(name, without_pid)] = value
|
60 | 62 | elif metric._multiprocess_mode == 'livesum':
|
61 |
| - samples.setdefault((name, without_pid), 0.0) |
62 | 63 | samples[(name, without_pid)] += value
|
63 | 64 | else: # all/liveall
|
64 | 65 | samples[(name, labels)] = value
|
| 66 | + |
65 | 67 | elif metric.type == 'histogram':
|
66 |
| - bucket = [float(l[1]) for l in labels if l[0] == 'le'] |
| 68 | + bucket = tuple(float(l[1]) for l in labels if l[0] == 'le') |
67 | 69 | if bucket:
|
68 | 70 | # _bucket
|
69 |
| - without_le = tuple([l for l in labels if l[0] != 'le']) |
| 71 | + without_le = tuple(l for l in labels if l[0] != 'le') |
70 | 72 | buckets.setdefault(without_le, {})
|
71 | 73 | buckets[without_le].setdefault(bucket[0], 0.0)
|
72 | 74 | buckets[without_le][bucket[0]] += value
|
73 | 75 | else:
|
74 | 76 | # _sum/_count
|
75 |
| - samples.setdefault((name, labels), 0.0) |
76 | 77 | samples[(name, labels)] += value
|
| 78 | + |
77 | 79 | else:
|
78 | 80 | # Counter and Summary.
|
79 |
| - samples.setdefault((name, labels), 0.0) |
80 | 81 | samples[(name, labels)] += value
|
81 | 82 |
|
82 | 83 | # Accumulate bucket values.
|
|
0 commit comments