Skip to content

Commit 35502dc

Browse files
authored
Send monitoring metrics from repocleaner (#811)
1 parent 33120a1 commit 35502dc

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

Framework/script/RepoCleaner/o2-qc-repo-cleaner

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ import re
2626
import sys
2727
from typing import List
2828
import tempfile
29+
import socket
2930

3031
import dryable
3132
import yaml
3233
import time
3334
import consul
3435
import multiprocessing as mp
36+
from pathlib import Path
3537

3638
from Ccdb import Ccdb
3739
from pidfile import PIDFile, AlreadyRunningError
@@ -206,6 +208,21 @@ def storeSavedTimestamp():
206208
f.close()
207209

208210

211+
def storeMonitoringMetrics(success, duration):
212+
"""
213+
Store the status and the duration in influxdb via telegraf for monitoring purpose.
214+
"""
215+
socket_file="/tmp/telegraf.sock"
216+
if Path(socket_file).exists():
217+
telegraf = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
218+
telegraf.connect(socket_file)
219+
telegraf.send(f"repoCleaner success={success}".encode('utf-8'))
220+
telegraf.send(f"repoCleaner duration={duration}".encode('utf-8'))
221+
logging.info(f"Monitoring metrics stored.")
222+
else:
223+
logging.warning(f"File {socket_file} does not exist, no monitoring metrics stored.")
224+
225+
209226
def prepare_main_logger():
210227
logger = logging.getLogger()
211228
# Logging (split between stderr and stdout)
@@ -239,7 +256,7 @@ def create_parallel_logger():
239256
return logger
240257

241258

242-
def readConfig(args):
259+
def read_config(args):
243260
path = args.config
244261
if args.config_consul:
245262
items = args.config_consul.split(':')
@@ -270,6 +287,7 @@ def process_object(object_path, rules, ccdb):
270287

271288

272289
def run(args, ccdb_url, rules):
290+
273291
# Get list of objects from CCDB
274292
ccdb = Ccdb(ccdb_url)
275293
paths = ccdb.getObjectsList(getTimestampLastExecution())
@@ -295,6 +313,7 @@ def run(args, ccdb_url, rules):
295313
# ****************
296314

297315
def main():
316+
start_time = time.time()
298317
prepare_main_logger()
299318

300319
# Parse arguments
@@ -303,11 +322,15 @@ def main():
303322

304323
try:
305324
with PIDFile(filename='o2-qc-repo-cleaner.pid'):
306-
ccdb_url, rules = readConfig(args)
325+
ccdb_url, rules = read_config(args)
307326
run(args, ccdb_url, rules)
308-
309327
except AlreadyRunningError:
310328
print('Already running. Exiting.')
329+
except:
330+
storeMonitoringMetrics(success=0, duration=time.time()-start_time)
331+
raise
332+
333+
storeMonitoringMetrics(success=1, duration=time.time()-start_time)
311334

312335

313336
if __name__ == "__main__": # to be able to run the test code above when not imported.

0 commit comments

Comments
 (0)