Skip to content

Commit 9eecc6a

Browse files
committed
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 parent 09f29d9 commit 9eecc6a

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

os_capacity/prometheus.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,10 @@ def collect(self):
339339
)
340340
skip_host_usage = int(os.environ.get("OS_CAPACITY_SKIP_HOST_USAGE", "0")) == 1
341341

342-
conn = openstack.connect()
343342
openstack.enable_logging(debug=False)
344343
try:
345344
resource_providers, host_guages = get_host_details(
346-
conn.compute, conn.placement
345+
self.conn.compute, self.conn.placement
347346
)
348347
guages += host_guages
349348

@@ -355,7 +354,7 @@ def collect(self):
355354
)
356355

357356
if not skip_project_usage:
358-
guages += get_project_usage(conn.identity, conn.placement, conn.compute)
357+
guages += get_project_usage(self.conn.identity, self.conn.placement, self.conn.compute)
359358
project_time = time.perf_counter()
360359
project_duration = project_time - host_time
361360
print(
@@ -366,7 +365,7 @@ def collect(self):
366365
print("2 of 3: skipping project usage")
367366

368367
if not skip_host_usage:
369-
guages += get_host_usage(resource_providers, conn.placement)
368+
guages += get_host_usage(resource_providers, self.conn.placement)
370369
host_usage_time = time.perf_counter()
371370
host_usage_duration = host_usage_time - project_time
372371
print(

0 commit comments

Comments
 (0)