]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/influx: try to call close() 29207/head
authorKefu Chai <kchai@redhat.com>
Thu, 6 Jun 2019 09:02:01 +0000 (17:02 +0800)
committerNathan Cutler <ncutler@suse.com>
Tue, 23 Jul 2019 11:40:03 +0000 (13:40 +0200)
let's at least try to call influxclient.close() which in turn closes the
requests.Session

see
https://github.com/influxdata/influxdb-python/blob/v4.1.1/influxdb/client.py
and
https://github.com/influxdata/influxdb-python/blob/v5.0.0/influxdb/client.py

Fixes: http://tracker.ceph.com/issues/40174
Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit 8066a55b324643a40f37243262aa138d959dee91)

src/pybind/mgr/influx/module.py

index 52bc7c410fb0c34d4000a3efcbcc328b9f2c00b1..f3b80041c2b4d380fe6d12245fd95c6852ce98a7 100644 (file)
@@ -1,3 +1,4 @@
+from contextlib import contextmanager
 from datetime import datetime
 from threading import Event, Thread
 from itertools import chain
@@ -130,9 +131,8 @@ class Module(MgrModule):
                     break
 
                 start = time.time()
-                client = self.get_influx_client()
-                client.write_points(points, time_precision='ms')
-                client.close()
+                with self.get_influx_client() as client:
+                    client.write_points(points, time_precision='ms')
                 runtime = time.time() - start
                 self.log.debug('Writing points %d to Influx took %.3f seconds',
                                len(points), runtime)
@@ -332,14 +332,23 @@ class Module(MgrModule):
                      self.get_pg_summary_osd(pools, now),
                      self.get_pg_summary_pool(pools, now))
 
+    @contextmanager
     def get_influx_client(self):
-        return InfluxDBClient(self.config['hostname'],
-                                     self.config['port'],
-                                     self.config['username'],
-                                     self.config['password'],
-                              self.config['database'],
-                                     self.config['ssl'],
-                                     self.config['verify_ssl'])
+        client = InfluxDBClient(self.config['hostname'],
+                                self.config['port'],
+                                self.config['username'],
+                                self.config['password'],
+                                self.config['database'],
+                                self.config['ssl'],
+                                self.config['verify_ssl'])
+        try:
+            yield client
+        finally:
+            try:
+                client.close()
+            except AttributeError:
+                # influxdb older than v5.0.0
+                pass
 
     def send_to_influx(self):
         if not self.config['hostname']:
@@ -360,21 +369,20 @@ class Module(MgrModule):
         self.log.debug("Sending data to Influx host: %s",
                        self.config['hostname'])
         try:
-            client = self.get_influx_client()
-            databases = client.get_list_database()
-            if {'name': self.config['database']} not in databases:
-                self.log.info("Database '%s' not found, trying to create "
-                              "(requires admin privs). You can also create "
-                              "manually and grant write privs to user "
-                              "'%s'", self.config['database'],
-                              self.config['database'])
-                client.create_database(self.config['database'])
-                client.create_retention_policy(name='8_weeks',
-                                               duration='8w',
-                                               replication='1',
-                                               default=True,
-                                               database=self.config['database'])
-            client.close()
+            with self.get_influx_client() as client:
+                databases = client.get_list_database()
+                if {'name': self.config['database']} not in databases:
+                    self.log.info("Database '%s' not found, trying to create "
+                                  "(requires admin privs). You can also create "
+                                  "manually and grant write privs to user "
+                                  "'%s'", self.config['database'],
+                                  self.config['database'])
+                    client.create_database(self.config['database'])
+                    client.create_retention_policy(name='8_weeks',
+                                                   duration='8w',
+                                                   replication='1',
+                                                   default=True,
+                                                   database=self.config['database'])
 
             self.log.debug('Gathering statistics')
             points = self.gather_statistics()