Shad Ansari | 7e28dc6 | 2022-06-07 13:43:22 -0700 | [diff] [blame^] | 1 | """ |
| 2 | SPDX-FileCopyrightText: 2020-present Intel Corporation |
| 3 | SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> |
| 4 | SPDX-License-Identifier: LicenseRef-ONF-Member-1.01 |
| 5 | """ |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 6 | |
| 7 | import os |
| 8 | import time |
Shad Ansari | 0a90503 | 2022-02-10 19:37:15 +0000 | [diff] [blame] | 9 | from prometheus_client import start_http_server, Gauge, REGISTRY, PROCESS_COLLECTOR, PLATFORM_COLLECTOR |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 10 | import requests |
| 11 | import json |
| 12 | |
Shad Ansari | 0a90503 | 2022-02-10 19:37:15 +0000 | [diff] [blame] | 13 | REGISTRY.unregister(PROCESS_COLLECTOR) |
| 14 | REGISTRY.unregister(PLATFORM_COLLECTOR) |
| 15 | # Unlike process and platform_collector gc_collector registers itself as three different collectors that have no corresponding public named variable. |
| 16 | REGISTRY.unregister(REGISTRY._names_to_collectors['python_gc_objects_collected_total']) |
| 17 | #REGISTRY.unregister(REGISTRY._names_to_collectors['python_gc_uncollectable_objects_sum']) |
| 18 | #REGISTRY.unregister(REGISTRY._names_to_collectors['python_gc_collected_objects_sum']) |
| 19 | |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 20 | class Metrics: |
| 21 | def __init__(self, app_port=3333, polling_interval_seconds=5): |
| 22 | self.app_port = app_port |
| 23 | self.polling_interval_seconds = polling_interval_seconds |
| 24 | |
| 25 | self.num_configured_devices = Gauge("num_configured_devices", "Number of configured devices") |
Shad Ansari | 6292c45 | 2022-02-08 17:39:06 +0000 | [diff] [blame] | 26 | self.num_reachable_devices = Gauge("num_reachable_devices", "Number of reachable devices") |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 27 | |
| 28 | def run_metrics_loop(self): |
| 29 | while True: |
| 30 | self.fetch() |
| 31 | time.sleep(self.polling_interval_seconds) |
| 32 | |
| 33 | def fetch(self): |
| 34 | resp = requests.get(url=f"http://localhost:{self.app_port}/devices") |
Shad Ansari | 6292c45 | 2022-02-08 17:39:06 +0000 | [diff] [blame] | 35 | self.num_configured_devices.set(len(json.loads(resp.text))) |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 36 | |
| 37 | resp = requests.get(url=f"http://localhost:{self.app_port}/devices/reachable") |
Shad Ansari | 6292c45 | 2022-02-08 17:39:06 +0000 | [diff] [blame] | 38 | self.num_reachable_devices.set(len(json.loads(resp.text))) |
Shad Ansari | 467862f | 2022-02-08 00:40:40 +0000 | [diff] [blame] | 39 | |
| 40 | def main(): |
| 41 | polling_interval_seconds = int(os.getenv("POLLING_INTERVAL_SECONDS", "5")) |
| 42 | app_port = int(os.getenv("APP_PORT", "3333")) |
| 43 | exporter_port = int(os.getenv("EXPORTER_PORT", "4444")) |
| 44 | |
| 45 | app_metrics = Metrics( |
| 46 | app_port=app_port, |
| 47 | polling_interval_seconds=polling_interval_seconds |
| 48 | ) |
| 49 | start_http_server(exporter_port) |
| 50 | app_metrics.run_metrics_loop() |
| 51 | |
| 52 | if __name__ == "__main__": |
| 53 | main() |