From 408a47ff9b6d038e85c42e35501bb4422a5bd2ac Mon Sep 17 00:00:00 2001 From: Tristan Cacqueray Date: Thu, 14 Nov 2024 15:51:50 -0500 Subject: [PATCH] Update metrics before starting the exporter --- zuul-capacity.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/zuul-capacity.py b/zuul-capacity.py index edc7708..dc38b21 100644 --- a/zuul-capacity.py +++ b/zuul-capacity.py @@ -5,7 +5,7 @@ from dataclasses import dataclass from prometheus_client import start_http_server, Gauge -log = logging.getLogger(__name__) +log = logging.getLogger("zuul-capacity") @dataclass class Resource: @@ -29,7 +29,7 @@ def get_resources(cloud): @dataclass class Provider: max_server: int - cloud: any + cloud: openstack.Cloud def from_nodepool(provider): return Provider( @@ -46,6 +46,17 @@ def get_providers(nodepool_yaml): providers[provider["name"]] = Provider.from_nodepool(provider) return providers +def update_providers_metric(metrics, providers): + for (name, provider) in providers.items(): + resources = get_resources(provider.cloud) + metrics["instances"].labels(cloud=name).set(len(resources)) + cpu, mem = 0, 0 + for resource in resources: + cpu += resource.cpu + mem += resource.mem + metrics["cpu"].labels(cloud=name).set(cpu) + metrics["mem"].labels(cloud=name).set(mem) + def usage(): parser = argparse.ArgumentParser() parser.add_argument("--nodepool", metavar="FILE", required=True) @@ -63,20 +74,14 @@ def main(): providers = get_providers(args.nodepool) + update_providers_metric(metrics, providers) + # Initialize connection log.info("Starting exporter at :8080 for %d provider", len(providers)) start_http_server(8080) while True: - for (name, provider) in providers.items(): - resources = get_resources(provider.cloud) - metrics["instances"].labels(cloud=name).set(len(resources)) - cpu, mem = 0, 0 - for resource in resources: - cpu += resource.cpu - mem += resource.mem - metrics["cpu"].labels(cloud=name).set(cpu) - metrics["mem"].labels(cloud=name).set(mem) + update_providers_metric(metrics, providers) time.sleep(300)