Skip to content

Commit bebdcf4

Browse files
author
lixiaoyu
committed
merge exit process db file into a common db file to avoid db file booming
Signed-off-by: lixiaoyu <lixiaoyu2@taou.com>
1 parent 56a8a53 commit bebdcf4

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

prometheus_client/multiprocess.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77

88
from .metrics_core import Metric
9-
from .mmap_dict import MmapedDict
9+
from .mmap_dict import MmapedDict, mmap_key
1010
from .samples import Sample
1111
from .utils import floatToGoString
1212

@@ -157,3 +157,43 @@ def mark_process_dead(pid, path=None):
157157
os.remove(f)
158158
for f in glob.glob(os.path.join(path, 'gauge_liveall_{0}.db'.format(pid))):
159159
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

Comments
 (0)