From 6efff55447f6a0d84b6b3f91770fa752f278825b Mon Sep 17 00:00:00 2001 From: Hanno Hecker Date: Fri, 5 Apr 2019 16:54:57 +0200 Subject: [PATCH] cleanup entities task (#388) * cleanup entities task see also https://github.com/zalando-zmon/zmon-scheduler/pull/89 * finish span... * fix span? * fix span? * log_kv requires dict --- zmon_worker_monitor/zmon_worker/tasks/main.py | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/zmon_worker_monitor/zmon_worker/tasks/main.py b/zmon_worker_monitor/zmon_worker/tasks/main.py index 34d5ba2f..f3a37a8c 100755 --- a/zmon_worker_monitor/zmon_worker/tasks/main.py +++ b/zmon_worker_monitor/zmon_worker/tasks/main.py @@ -1101,8 +1101,9 @@ def trial_run(self, req, alerts, task_context=None, **kwargs): else: self.notify_for_trial_run(val, req, alerts) - @trace() + @trace(pass_span=True) def cleanup(self, *args, **kwargs): + current_span = extract_span_from_kwargs(**kwargs) self.task_context = kwargs.get('task_context') p = self.con.pipeline() p.smembers('zmon:checks') @@ -1158,6 +1159,23 @@ def cleanup(self, *args, **kwargs): else: self._cleanup_alert(p, alert_id) + current_span.log_kv({'cleanup_entities': kwargs.get('cleanup_entities', [])}) + for entity_id in kwargs.get('cleanup_entities', []): + alert_ids = [a.replace('zmon:alerts:', '').replace(':{}'.format(entity_id), '') + for a in self.con.keys('zmon:alerts:*:{}'.format(entity_id))] + for alert_id in alert_ids: + self._cleanup_common(p, 'alerts', alert_id, set(entity_id)) + # All entities matching given alert definition. + self.logger.info('Removing entity %s from hash %s', entity_id, + 'zmon:alerts:{}:entities'.format(alert_id)) + p.hdel('zmon:alerts:{}:entities'.format(alert_id), entity_id) + p.delete('zmon:notifications:{}:{}'.format(alert_id, entity_id)) + + check_ids = [c.replace('zmon:checks:', '').replace(':{}'.format(entity_id), '') + for c in self.con.keys('zmon:checks:*:{}'.format(entity_id))] + for check_id in check_ids: + self._cleanup_common(p, 'checks', check_id, set(entity_id)) + p.execute() def _cleanup_check(self, pipeline, check_id):