From 56f9342030b24d4faaa2362a6196af5ae1bb6e17 Mon Sep 17 00:00:00 2001
From: sguazt <marco.guazzone@gmail.com>
Date: Sun, 17 Jan 2021 11:37:29 +0100
Subject: [PATCH] Core: small optimization (avoid collecting measures for sinks
 if the associated thread is not running, that is when no sink is active)

---
 easycloud/core/metamonitor.py | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/easycloud/core/metamonitor.py b/easycloud/core/metamonitor.py
index 0f8e2f6..1fc356a 100755
--- a/easycloud/core/metamonitor.py
+++ b/easycloud/core/metamonitor.py
@@ -141,7 +141,15 @@ class MetaMonitor(ABC):
 
     #[ALTERNATIVE #2]
     def _measures_sink_runner(self, sinks):
-        if sinks is None or len(sinks) == 0:
+        #TODO: in the future, support the possibility to add sinks at runtime.
+        #      In this case, this runner should be changed as follows:
+        #      1. it does not terminate if the `sinks` parameter is empty (see
+        #         the `if` statement below),
+        #      2. it appends measures (extracted from `_measures_sink_queue`) to
+        #         `meaures` only if there is at least a sink to which sending them.
+
+        if not sinks:
+            # Terminate this runner as there is no sink to which sending measures
             return
 
         #FIXME makes these two settings a parameter
@@ -250,14 +258,15 @@ class MetaMonitor(ABC):
                                                 granularity=self.conf.granularity)
                     if samples is not None:
                         _metrics_samples.append(samples)
-                        for sample in samples['values']:
-                            measure = Measure(object_ns = self.conf.platform + '-generic', #TODO: let the subclass decide what the object namespace should be (e.g., to consider the project ID, the region, etc.)
-                                              object_id = _instance,
-                                              metric = _requested_metric,
-                                              timestamp = sample['timestamp'],
-                                              unit = sample['unit'] if 'unit' in sample else None,
-                                              value = sample['value'] if 'value' in sample else sample['error']) #TODO: add a conf option to decide whether or not to send errors to sinks
-                            self._measures_sink_queue.put(measure)
+                        if self._measures_sink_thread.is_alive():
+                            for sample in samples['values']:
+                                measure = Measure(object_ns = self.conf.platform + '-generic', #TODO: let the subclass decide what the object namespace should be (e.g., to consider the project ID, the region, etc.)
+                                                  object_id = _instance,
+                                                  metric = _requested_metric,
+                                                  timestamp = sample['timestamp'],
+                                                  unit = sample['unit'] if 'unit' in sample else None,
+                                                  value = sample['value'] if 'value' in sample else sample['error']) #TODO: add a conf option to decide whether or not to send errors to sinks
+                                self._measures_sink_queue.put(measure)
                 #logging.debug("[" + self.__class__.__name__ + "] Sending message: " + str({"instance_id": _instance, "measurements": _metrics_samples}))
                 self.measurements_queue.put({"instance_id": _instance, "measurements": _metrics_samples})
 
-- 
GitLab