@staticmethod<EOL><INDENT>def merge(files, accumulate=True):<DEDENT>
metrics = {}<EOL>for f in files:<EOL><INDENT>parts = os.path.basename(f).split('<STR_LIT:_>')<EOL>typ = parts[<NUM_LIT:0>]<EOL>d = MmapedDict(f, read_mode=True)<EOL>for key, value in d.read_all_values():<EOL><INDENT>metric_name, name, labels = json.loads(key)<EOL>labels_key = tuple(sorted(labels.items()))<EOL>metric = metrics.get(metric_name)<EOL>if metric is None:<EOL><INDENT>metric = Metric(metric_name, '<STR_LIT>', typ)<EOL>metrics[metric_name] = metric<EOL><DEDENT>if typ == '<STR_LIT>':<EOL><INDENT>pid = parts[<NUM_LIT:2>][:-<NUM_LIT:3>]<EOL>metric._multiprocess_mode = parts[<NUM_LIT:1>]<EOL>metric.add_sample(name, labels_key + (('<STR_LIT>', pid),), value)<EOL><DEDENT>else:<EOL><INDENT>metric.add_sample(name, labels_key, value)<EOL><DEDENT><DEDENT>d.close()<EOL><DEDENT>for metric in metrics.values():<EOL><INDENT>samples = defaultdict(float)<EOL>buckets = {}<EOL>for s in metric.samples:<EOL><INDENT>name, labels, value = s.name, s.labels, s.value<EOL>if metric.type == '<STR_LIT>':<EOL><INDENT>without_pid = tuple(l for l in labels if l[<NUM_LIT:0>] != '<STR_LIT>')<EOL>if metric._multiprocess_mode == '<STR_LIT>':<EOL><INDENT>current = samples.setdefault((name, without_pid), value)<EOL>if value < current:<EOL><INDENT>samples[(s.name, without_pid)] = value<EOL><DEDENT><DEDENT>elif metric._multiprocess_mode == '<STR_LIT>':<EOL><INDENT>current = samples.setdefault((name, without_pid), value)<EOL>if value > current:<EOL><INDENT>samples[(s.name, without_pid)] = value<EOL><DEDENT><DEDENT>elif metric._multiprocess_mode == '<STR_LIT>':<EOL><INDENT>samples[(name, without_pid)] += value<EOL><DEDENT>else:  <EOL><INDENT>samples[(name, labels)] = value<EOL><DEDENT><DEDENT>elif metric.type == '<STR_LIT>':<EOL><INDENT>bucket = tuple(float(l[<NUM_LIT:1>]) for l in labels if l[<NUM_LIT:0>] == '<STR_LIT>')<EOL>if bucket:<EOL><INDENT>without_le = tuple(l for l in labels if l[<NUM_LIT:0>] != '<STR_LIT>')<EOL>buckets.setdefault(without_le, {})<EOL>buckets[without_le].setdefault(bucket[<NUM_LIT:0>], <NUM_LIT:0.0>)<EOL>buckets[without_le][bucket[<NUM_LIT:0>]] += value<EOL><DEDENT>else:<EOL><INDENT>samples[(s.name, labels)] += value<EOL><DEDENT><DEDENT>else:<EOL><INDENT>samples[(s.name, labels)] += value<EOL><DEDENT><DEDENT>if metric.type == '<STR_LIT>':<EOL><INDENT>for labels, values in buckets.items():<EOL><INDENT>acc = <NUM_LIT:0.0><EOL>for bucket, value in sorted(values.items()):<EOL><INDENT>sample_key = (<EOL>metric.name + '<STR_LIT>',<EOL>labels + (('<STR_LIT>', floatToGoString(bucket)),),<EOL>)<EOL>if accumulate:<EOL><INDENT>acc += value<EOL>samples[sample_key] = acc<EOL><DEDENT>else:<EOL><INDENT>samples[sample_key] = value<EOL><DEDENT><DEDENT>if accumulate:<EOL><INDENT>samples[(metric.name + '<STR_LIT>', labels)] = acc<EOL><DEDENT><DEDENT><DEDENT>metric.samples = [Sample(name_, dict(labels), value) for (name_, labels), value in samples.items()]<EOL><DEDENT>return metrics.values()<EOL>
Merge metrics from given mmap files.

        By default, histograms are accumulated, as per prometheus wire format.
        But if writing the merged data back to mmap files, use
        accumulate=False to avoid compound accumulation.