|
6 | 6 | import os |
7 | 7 |
|
8 | 8 | from .metrics_core import Metric |
9 | | -from .mmap_dict import MmapedDict |
| 9 | +from .mmap_dict import MmapedDict, mmap_key |
10 | 10 | from .samples import Sample |
11 | 11 | from .utils import floatToGoString |
12 | 12 |
|
@@ -157,3 +157,43 @@ def mark_process_dead(pid, path=None): |
157 | 157 | os.remove(f) |
158 | 158 | for f in glob.glob(os.path.join(path, 'gauge_liveall_{0}.db'.format(pid))): |
159 | 159 | os.remove(f) |
| 160 | + |
| 161 | + # get associated db files with pid |
| 162 | + files = glob.glob(os.path.join(path, '*_{0}.db'.format(pid))) |
| 163 | + if not files: |
| 164 | + return |
| 165 | + |
| 166 | + # get merge file name |
| 167 | + merge_files = [] |
| 168 | + for f in files: |
| 169 | + file_prefix = os.path.basename(f).rsplit('_', 1)[0] |
| 170 | + merge_file = os.path.join(path, '{0}_merge.db'.format(file_prefix)) |
| 171 | + if merge_file not in merge_files: |
| 172 | + merge_files.append(merge_file) |
| 173 | + |
| 174 | + # if not exist merge_file, create and init it |
| 175 | + if not os.path.exists(merge_file): |
| 176 | + MmapedDict(merge_file).close() |
| 177 | + |
| 178 | + # do merge |
| 179 | + metrics = MultiProcessCollector(None).merge(files + merge_files, accumulate=False) |
| 180 | + typ_metrics_dict = defaultdict(list) |
| 181 | + for metric in metrics: |
| 182 | + typ_metrics_dict[metric.type].append(metric) |
| 183 | + |
| 184 | + # write data to correct merge_file |
| 185 | + for merge_file in merge_files: |
| 186 | + typ = os.path.basename(merge_file).split('_')[0] |
| 187 | + d = MmapedDict(merge_file) |
| 188 | + for metric in typ_metrics_dict[typ]: |
| 189 | + for sample in metric.samples: |
| 190 | + labels = values = [] |
| 191 | + if sample.labels: |
| 192 | + labels, values = zip(*sample.labels.items()) |
| 193 | + key = mmap_key(metric.name, sample.name, labels, values) |
| 194 | + d.write_value(key, sample.value) |
| 195 | + d.close() |
| 196 | + |
| 197 | + # remove the old db file |
| 198 | + for f in files: |
| 199 | + os.remove(f) |
0 commit comments