]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/prometheus: use threading.Event instead of sleep
authorBoris Ranto <branto@redhat.com>
Wed, 25 Nov 2020 19:49:48 +0000 (20:49 +0100)
committerBoris Ranto <branto@redhat.com>
Thu, 28 Jan 2021 14:54:24 +0000 (15:54 +0100)
This allows us to avoid waiting for the sleep to finish when waiting for
the thread to finish.

Signed-off-by: Boris Ranto <branto@redhat.com>
(cherry picked from commit dd5886c3c006e388283df50cc87addeffb3b2b52)

src/pybind/mgr/prometheus/module.py

index 5008fcc8ece023c5144609c8473d72ac1b58aada..f5684f257ef54caac7dcb2323fb8d1445759abd7 100644 (file)
@@ -189,6 +189,7 @@ class MetricCollectionThread(threading.Thread):
         # type: (Module) -> None
         self.mod = module
         self.active = True
+        self.event = threading.Event()
         super(MetricCollectionThread, self).__init__(target=self.collect)
 
     def collect(self):
@@ -203,7 +204,7 @@ class MetricCollectionThread(threading.Thread):
                 except Exception as e:
                     # Log any issues encountered during the data collection and continue
                     self.mod.log.exception("failed to collect metrics:")
-                    time.sleep(self.mod.scrape_interval)
+                    self.event.wait(self.mod.scrape_interval)
                     continue
 
                 duration = time.time() - start_time
@@ -227,13 +228,14 @@ class MetricCollectionThread(threading.Thread):
                     self.mod.collect_cache = data
                     self.mod.collect_time = duration
 
-                time.sleep(sleep_time)
+                self.event.wait(sleep_time)
             else:
                 self.mod.log.error('No MON connection')
-                time.sleep(self.mod.scrape_interval)
+                self.event.wait(self.mod.scrape_interval)
 
     def stop(self):
         self.active = False
+        self.event.set()
 
 class Module(MgrModule):
     COMMANDS = [