From 3cad01a220f9dcdce91770aeef891d287741281a Mon Sep 17 00:00:00 2001 From: Doug Szumski Date: Thu, 16 Oct 2025 16:21:50 +0100 Subject: [PATCH] Fix memory leak Every time the collector is scraped, it makes a new connection. The connection is never cleaned up, even if you add a conn.close() after the scrape [1]. This is a likely bug. This patch moves to using the instance connection, which works around the leak and is more efficient. [1] https://docs.openstack.org/openstacksdk/latest/user/connection.html#openstack.connection.Connection.close --- os_capacity/prometheus.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/os_capacity/prometheus.py b/os_capacity/prometheus.py index 9e0ac7d..f19c90f 100755 --- a/os_capacity/prometheus.py +++ b/os_capacity/prometheus.py @@ -339,11 +339,10 @@ def collect(self): ) skip_host_usage = int(os.environ.get("OS_CAPACITY_SKIP_HOST_USAGE", "0")) == 1 - conn = openstack.connect() openstack.enable_logging(debug=False) try: resource_providers, host_guages = get_host_details( - conn.compute, conn.placement + self.conn.compute, self.conn.placement ) guages += host_guages @@ -355,7 +354,9 @@ def collect(self): ) if not skip_project_usage: - guages += get_project_usage(conn.identity, conn.placement, conn.compute) + guages += get_project_usage( + self.conn.identity, self.conn.placement, self.conn.compute + ) project_time = time.perf_counter() project_duration = project_time - host_time print( @@ -366,7 +367,7 @@ def collect(self): print("2 of 3: skipping project usage") if not skip_host_usage: - guages += get_host_usage(resource_providers, conn.placement) + guages += get_host_usage(resource_providers, self.conn.placement) host_usage_time = time.perf_counter() host_usage_duration = host_usage_time - project_time print(