@cryptotaxi247 / netdata-1 / commits / 1dca7fd7c

python.d: mute stock jobs logging during check() (#16515)

Ilya Mashchenko committed Nov 30, 2023 at 20:59 UTC 1dca7fd7c9f5a467e721bd1d5d00f074195e0dd4
2 files changed +36 -12
collectors/python.d.plugin/python.d.plugin.in
+17 -5
@@ -222,8 +222,10 @@ class ModuleConfig:
222 def __init__(self, name, config=None):
223 self.name = name
224 self.config = config or OrderedDict()
225 + self.is_stock = False
226
227 def load(self, abs_path):
228 + self.is_stock = abs_path.startswith(DIRS.modules_stock_config)
229 self.config.update(load_config(abs_path) or dict())
230
231 def defaults(self):
@@ -242,6 +244,7 @@ class ModuleConfig:
244 config = OrderedDict()
245 config.update(job_config)
246 config['job_name'] = job_name
247 + config['__is_stock'] = self.is_stock
248 for k, v in self.defaults().items():
249 config.setdefault(k, v)
250
@@ -309,7 +312,8 @@ class JobsConfigsBuilder:
312 return None
313
314 configs = config.create_jobs()
312 - self.log.info("[{0}] built {1} job(s) configs".format(module_name, len(configs)))
315 + if not config.is_stock:
316 + self.log.info("[{0}] built {1} job(s) configs".format(module_name, len(configs)))
317
318 self.apply_defaults(configs, self.module_defaults)
319 self.apply_defaults(configs, self.job_defaults)
@@ -338,6 +342,7 @@ class Job(threading.Thread):
342 self.autodetection_retry = config['autodetection_retry']
343 self.checks = self.inf
344 self.job = None
345 + self.is_stock = config.get('__is_stock', False)
346 self.status = JOB_STATUS_INIT
347
348 def is_inited(self):
@@ -350,8 +355,14 @@ class Job(threading.Thread):
355 return self.job.name
356
357 def check(self):
358 + if self.is_stock:
359 + self.job.logger.mute()
360 +
361 ok = self.job.check()
362 +
363 + self.job.logger.unmute()
364 self.checks -= self.checks != self.inf and not ok
365 +
366 return ok
367
368 def create(self):
@@ -503,7 +514,6 @@ class FileLockRegistry:
514 name = "docker" + name[7:]
515 return name
516
506 -
517 def register(self, name):
518 name = self.rename(name)
519 if name in self.locks:
@@ -685,12 +695,14 @@ class Plugin:
695 try:
696 ok = job.check()
697 except Exception as error:
688 - self.log.warning("{0}[{1}] : unhandled exception on check : {2}, skipping the job".format(
689 - job.module_name, job.real_name, repr(error)))
698 + if not job.is_stock:
699 + self.log.warning("{0}[{1}] : unhandled exception on check : {2}, skipping the job".format(
700 + job.module_name, job.real_name, repr(error)))
701 job.status = JOB_STATUS_DROPPED
702 continue
703 if not ok:
693 - self.log.info('{0}[{1}] : check failed'.format(job.module_name, job.real_name))
704 + if not job.is_stock:
705 + self.log.info('{0}[{1}] : check failed'.format(job.module_name, job.real_name))
706 job.status = JOB_STATUS_RECOVERING if job.need_to_recheck() else JOB_STATUS_DROPPED
707 continue
708 self.log.info('{0}[{1}] : check success'.format(job.module_name, job.real_name))
collectors/python.d.plugin/python_modules/bases/loggers.py
+19 -7
@@ -15,7 +15,6 @@ except ImportError:
15
16 from bases.collection import on_try_except_finally, unicode_str
17
18 -
18 LOGGING_LEVELS = {'CRITICAL': 50,
19 'ERROR': 40,
20 'WARNING': 30,
@@ -47,6 +46,7 @@ def limiter(log_max_count=30, allowed_in_seconds=60):
46 func(*args)
47
48 return on_call
49 +
50 return on_decorator
51
52
@@ -87,6 +87,7 @@ class BaseLogger(object):
87 :param handler: <logging handler>
88 """
89 self.logger = logging.getLogger(logger_name)
90 + self._muted = False
91 if not self.has_handlers():
92 self.severity = 'INFO'
93 self.logger.addHandler(handler())
@@ -121,24 +122,35 @@ class BaseLogger(object):
122 self.logger.setLevel(LOGGING_LEVELS[level])
123
124 def debug(self, *msg, **kwargs):
124 - self.logger.debug(' '.join(map(unicode_str, msg)), **kwargs)
125 + if not self._muted:
126 + self.logger.debug(' '.join(map(unicode_str, msg)), **kwargs)
127
128 def info(self, *msg, **kwargs):
127 - self.logger.info(' '.join(map(unicode_str, msg)), **kwargs)
129 + if not self._muted:
130 + self.logger.info(' '.join(map(unicode_str, msg)), **kwargs)
131
132 def warning(self, *msg, **kwargs):
130 - self.logger.warning(' '.join(map(unicode_str, msg)), **kwargs)
133 + if not self._muted:
134 + self.logger.warning(' '.join(map(unicode_str, msg)), **kwargs)
135
136 def error(self, *msg, **kwargs):
133 - self.logger.error(' '.join(map(unicode_str, msg)), **kwargs)
137 + if not self._muted:
138 + self.logger.error(' '.join(map(unicode_str, msg)), **kwargs)
139
135 - def alert(self, *msg, **kwargs):
136 - self.logger.critical(' '.join(map(unicode_str, msg)), **kwargs)
140 + def alert(self, *msg, **kwargs):
141 + if not self._muted:
142 + self.logger.critical(' '.join(map(unicode_str, msg)), **kwargs)
143
144 @on_try_except_finally(on_finally=(exit, 1))
145 def fatal(self, *msg, **kwargs):
146 self.logger.critical(' '.join(map(unicode_str, msg)), **kwargs)
147
148 + def mute(self):
149 + self._muted = True
150 +
151 + def unmute(self):
152 + self._muted = False
153 +
154
155 class PythonDLogger(object):
156 def __init__(self, logger_name=PYTHON_D_LOG_NAME, log_fmt=PYTHON_D_LOG_LINE_FORMAT):