Skip to content

Commit 17819a8

Browse files
committed
Starting to add standalone exporter
1 parent 205c00f commit 17819a8

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

os_capacity/prometheus.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import collections
44
import json
5+
import time
6+
import uuid
57

68
import openstack
9+
import prometheus_client as prom_client
10+
from prometheus_client import core as prom_core
711

812

913
def get_capacity_per_flavor(placement_client, flavors):
@@ -266,8 +270,46 @@ def print_exporter_data(app):
266270
print_host_free_details(app.compute_client, app.placement_client)
267271

268272

273+
class OpenStackCapacityCollector(object):
274+
def __init__(self):
275+
self.conn = openstack.connect()
276+
openstack.enable_logging(debug=True)
277+
print("got openstack connection")
278+
# for some reason this makes the logging work?!
279+
self.conn.compute.flavors()
280+
281+
def collect(self):
282+
start_time = time.perf_counter()
283+
collect_id = uuid.uuid4().hex
284+
print(f"Collect started {collect_id}")
285+
guages = []
286+
287+
conn = openstack.connect()
288+
openstack.enable_logging(debug=True)
289+
try:
290+
resource_providers = print_host_details(conn.compute, conn.placement)
291+
print_project_usage(conn.identity, conn.placement, conn.compute)
292+
print_host_usage(resource_providers, conn.placement)
293+
except Exception as e:
294+
print(f"error {e}")
295+
296+
gauge = prom_core.GaugeMetricFamily(
297+
"random_number",
298+
"A random number generator, I have no better idea",
299+
labels=["randomNum"],
300+
)
301+
gauge.add_metric(["mine"], 42)
302+
guages.append(gauge)
303+
304+
end_time = time.perf_counter()
305+
duration = end_time - start_time
306+
print(f"Collect complete {collect_id} it took {duration} seconds")
307+
return guages
308+
309+
269310
if __name__ == "__main__":
270-
conn = openstack.connect()
271-
resource_providers = print_host_details(conn.compute, conn.placement)
272-
print_project_usage(conn.identity, conn.placement, conn.compute)
273-
print_host_usage(resource_providers, conn.placement)
311+
prom_client.start_http_server(9000)
312+
prom_core.REGISTRY.register(OpenStackCapacityCollector())
313+
# there must be a better way!
314+
while True:
315+
time.sleep(5000)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
cliff>=2.8.0 # Apache
22
os-client-config>=1.28.0 # Apache-2.0
33
pbr>=2.0.0,!=2.1.0 # Apache-2.0
4+
prometheus-client==0.16.0

0 commit comments

Comments
 (0)