Skip to content

Commit

Permalink
Update metrics before starting the exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanCacqueray committed Nov 14, 2024
1 parent 9c2bceb commit 408a47f
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions zuul-capacity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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)


Expand Down

0 comments on commit 408a47f

Please sign in to comment.