From b976e9bdbbb459e69e6dbc27dbc4ac993a292e04 Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Thu, 14 Apr 2022 10:21:18 +0200
Subject: [PATCH] L2SS-758: Allow port to be configured, added more comments

---
 .../code/tango-prometheus-client.py               | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/docker-compose/tango-prometheus-exporter/code/tango-prometheus-client.py b/docker-compose/tango-prometheus-exporter/code/tango-prometheus-client.py
index 1483ca3bc..28a840415 100644
--- a/docker-compose/tango-prometheus-exporter/code/tango-prometheus-client.py
+++ b/docker-compose/tango-prometheus-exporter/code/tango-prometheus-client.py
@@ -100,10 +100,14 @@ class CustomCollector(object):
         return ([dev.dev_name(), attr_info.name, str_value, data_type, f"{x:02}", f"{y:02}"], float_value)
 
     def metrics_scalar(self, dev, attr_info, attr_value):
+        """ Return all metrics for a given SCALAR attribute. """
+
         new_metric = self._to_metric(dev, attr_info, 0, 0, attr_value.value)
         return [new_metric] if new_metric else []
 
     def metrics_spectrum(self, dev, attr_info, attr_value):
+        """ Return all metrics for a given SPECTRUM attribute. """
+
         metrics = []
         for x in range(int(attr_value.dim_x)):
             new_metric = self._to_metric(dev, attr_info, x, 0, attr_value.value[x])
@@ -112,6 +116,8 @@ class CustomCollector(object):
         return metrics
 
     def metrics_image(self, dev, attr_info, attr_value):
+        """ Return all metrics for a given IMAGE attribute. """
+
         metrics = []
         for y in range(int(attr_value.dim_y)): 
             for x in range(int(attr_value.dim_x)):
@@ -124,6 +130,8 @@ class CustomCollector(object):
         return metrics
 
     def metrics(self, dev, attr_info, attr_value):
+        """ Return all metrics for a given attribute. """
+
         if attr_info.data_format == AttrDataFormat.SCALAR:
             return self.metrics_scalar(dev, attr_info, attr_value)
         elif attr_info.data_format == AttrDataFormat.SPECTRUM:
@@ -134,6 +142,8 @@ class CustomCollector(object):
             return []
 
     def device_metrics(self, device_name):
+        """ Return all metrics for a given device, as configured. """
+
         dev = DeviceProxy(device_name)
         dev.set_timeout_millis(self.proxy_timeout)
 
@@ -167,7 +177,7 @@ class CustomCollector(object):
         return metrics
 
     def collect(self):
-        """ Yield all scraped metrics. """
+        """ Yield all scraped metrics from all devices, as configured. """
 
         logger.info("Start scraping")
         scrape_begin = time.time()
@@ -207,13 +217,14 @@ if __name__ == '__main__':
     parser = argparse.ArgumentParser()
     parser.add_argument('-c', '--config', type=str, required=True, help='configuration file')
     parser.add_argument('-t', '--timeout', type=int, required=False, default=250, help='device proxy timeout (ms)')
+    parser.add_argument('-p', '--port', type=int, required=False, default=8000, help='HTTP server port to open')
     args = parser.parse_args()
 
     config = ArchiverPolicy.load_config(args.config)
     collector = CustomCollector(config, proxy_timeout=args.timeout)
 
     logger.info("Starting server")
-    start_http_server(8000)
+    start_http_server(args.port)
 
     logger.info("Registering collector")
     REGISTRY.register(collector)
-- 
GitLab