Refactor promql_api after project structure change 83/88983/2
authorRajamohan Raj <rajamohan.raj@intel.com>
Thu, 30 May 2019 23:33:35 +0000 (23:33 +0000)
committerRajamohan Raj <rajamohan.raj@intel.com>
Sat, 1 Jun 2019 00:12:51 +0000 (00:12 +0000)
Refactored prompql_api after project structure change.
Fix review comments and make a better read me.

Change-Id: I40b7b2da40d85e9f297d4bb87d8633c71bdd1ec0
Issue-ID: ONAPARC-452
Signed-off-by: Rajamohan Raj <rajamohan.raj@intel.com>
vnfs/DAaaS/lib/promql_api/README.md [new file with mode: 0644]
vnfs/DAaaS/lib/promql_api/__init__.py [moved from vnfs/DAaaS/sample-apps/m3db_promql/promql_api/__init__.py with 100% similarity]
vnfs/DAaaS/lib/promql_api/prom_ql_api.py [moved from vnfs/DAaaS/sample-apps/m3db_promql/promql_api/prom_ql_api.py with 66% similarity]
vnfs/DAaaS/sample-apps/m3db_promql/promql_api/README.md [deleted file]
vnfs/DAaaS/sample-apps/m3db_promql/sample_promql_query.py

diff --git a/vnfs/DAaaS/lib/promql_api/README.md b/vnfs/DAaaS/lib/promql_api/README.md
new file mode 100644 (file)
index 0000000..de64fe0
--- /dev/null
@@ -0,0 +1,65 @@
+## What does this API do ?
+This api as of now provides a function which takes in a lits of 'LABELS' of prometheus
+and returns the corresponding result_sets in a list.
+
+For eg:
+If the labels is
+
+```
+LABELS = ['irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])']
+```
+
+The return is:
+
+```
+[{'metric': {'cpufreq': '1',
+             'endpoint': 'collectd-prometheus',
+             'exported_instance': 'otconap7',
+             'instance': '172.25.103.1:9103',
+             'job': 'collectd',
+             'namespace': 'edge1',
+             'pod': 'plundering-liger-collectd-wz7xg',
+             'service': 'collectd'},
+  'value': [1559177169.415, '119727200']}]
+```
+
+## How to use this API ?
+
+```
+1. Copy the directory 'promql_api' to your working directory. 
+```
+
+```
+2. Import the API function: query
+from promql_api.prom_ql_api import query
+```
+
+```
+3. have a global or local variable as 'LABELS'
+LABELS = ['irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])']
+```
+
+```
+4. Store the result set in a list:
+list_of_result_sets = query(LABELS)
+```
+
+## Troubleshooting tips
+
+* Check the sample file - sample_promql_query.py in the repo ( sample-apps/m3db_promql)
+* Make sure the file "__init__.py" is present in promql_api directory after you copy the directory.
+* Make sure environment variables like "DATA_ENDPOINT" are correctly set.
+* Logs are generated in the directory where the query function is called.
+* sample log file - promql_api.log
+
+```
+ 05-30-2019 08:47:53PM ::prom_ql_api.py :: set_log_config :: INFO :: Set the log configs.
+ 05-30-2019 08:47:53PM ::prom_ql_api.py :: load_and_validate_env_vars :: INFO :: Loading the env variables ...
+ 05-30-2019 08:47:53PM ::prom_ql_api.py :: load_and_validate_env_vars :: ERROR :: Env var: DATA_ENDPOINT not         found !
+ 05-30-2019 08:47:53PM ::prom_ql_api.py :: query :: INFO :: Forming the get request ...
+ 05-30-2019 08:47:53PM ::prom_ql_api.py :: query :: INFO :: API request::: URL: http://172.25.103.1:30090/api/v1/    query
+ 05-30-2019 08:47:53PM ::prom_ql_api.py :: query :: INFO :: API request::: params: {'query':                         'irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])'}
+ 05-30-2019 08:47:53PM ::connectionpool.py :: _new_conn :: DEBUG :: Starting new HTTP connection (1): 172.25.103.1   05-30-2019 08:47:53PM ::connectionpool.py :: _make_request :: DEBUG :: http://172.25.103.1:30090 "GET /api/v1/      query?query=irate%28collectd_cpufreq%7Bexported_instance%3D%22otconap7%22%2Ccpufreq%3D%221%22%7D%5B2m%5D%29 HTTP/1. 1" 200 370
+ 05-30-2019 08:47:53PM ::prom_ql_api.py :: query :: INFO :: ::::::::::RESULTS:::::::::::::                           irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])
+ 05-30-2019 08:47:53PM ::prom_ql_api.py :: query :: INFO :: {'metric': {'cpufreq': '1', 'endpoint': 'collectd-       prometheus', 'exported_instance': 'otconap7', 'instance': '172.25.103.1:9103', 'job': 'collectd', 'namespace':      'edge1', 'pod': 'plundering-liger-collectd-wz7xg', 'service': 'collectd'}, 'value': [1559249299.084, '236300']}
+ ```
@@ -1,23 +1,20 @@
 from __future__ import print_function
 from os import environ
 import logging
-import sysconfig
 import requests
 from requests.exceptions import HTTPError
 
 
-#LABELS = [ 'irate(http_requests_total{code="200"}[1m])', 'collectd_cpu_percent{job="collectd", exported_instance="an11-31"}[1m]' ]
-#LABELS = ['irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])', 'go_info']
+
 API_VERSION = '/api/v1/query'
-LIST_OF_ENV_VARIABLES = ["PROMETHEUS_SERVER_URL"]
+LIST_OF_ENV_VARIABLES = ["DATA_ENDPOINT"]
 MAP_ENV_VARIABLES = dict()
-#MAP_ENV_VARIABLES['PROMETHEUS_SERVER_URL']='http://172.25.103.1:30090' #to be deleted
 LOG = logging.getLogger(__name__)
 
 def set_log_config():
-    logging.basicConfig(format='%(asctime)s ::%(filename)s :: %(funcName)s :: %(levelname)s :: %(message)s', 
+    logging.basicConfig(format='%(asctime)s ::%(filename)s :: %(funcName)s :: %(levelname)s :: %(message)s',
                     datefmt='%m-%d-%Y %I:%M:%S%p',
-                    level=logging.DEBUG, 
+                    level=logging.DEBUG,
                     filename='promql_api.log',
                     filemode='w')
     LOG.info("Set the log configs.")
@@ -30,27 +27,38 @@ def load_and_validate_env_vars(list_of_env_vars):
             LOG.info("Found env variable: {} ".format(env_var.upper()))
             MAP_ENV_VARIABLES[env_var.upper()] = environ.get(env_var)
         else:
-            #MAP_ENV_VARIABLES['PROMETHEUS_SERVER_URL']='http://172.25.103.1:30090' # to be deleted
+            #MAP_ENV_VARIABLES['DATA_ENDPOINT']='http://127.0.0.1:30090' # to be deleted
             LOG.error("Env var: {} not found ! ".format(env_var.upper()))
             raise KeyError("Env variable: {} not found ! ".format(env_var.upper()))
 
 
-def query_m3db(LABELS):
+def query(LABELS):
     """
     Input parameters:
-        LABELS : a list of the LABELS
+        LABELS : a list of the LABELS like ['irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])']
     Return:
-        returns a list of  result sets of different labels
+        returns a list of  result sets of different labels.
+        SAMPLE O/P:
+        [{'metric': {'cpufreq': '1',
+             'endpoint': 'collectd-prometheus',
+             'exported_instance': 'otconap7',
+             'instance': '172.25.103.1:9103',
+             'job': 'collectd',
+             'namespace': 'edge1',
+             'pod': 'plundering-liger-collectd-wz7xg',
+             'service': 'collectd'},
+        'value': [1559177169.415, '119727200']}]
     """
-
+    set_log_config()
+    load_and_validate_env_vars(LIST_OF_ENV_VARIABLES)
     LOG.info("Forming the get request ...")
     list_of_substrings = []
     params_map = {}
     list_of_result_sets = []
-    list_of_substrings.append(MAP_ENV_VARIABLES['PROMETHEUS_SERVER_URL'])
+    list_of_substrings.append(MAP_ENV_VARIABLES['DATA_ENDPOINT'])
     list_of_substrings.append(API_VERSION)
     url = ''.join(list_of_substrings)
-    
+
     for each_label in LABELS:
         params_map['query'] = each_label
         try:
@@ -60,24 +68,15 @@ def query_m3db(LABELS):
             response.raise_for_status()
         except HTTPError as http_err:
             print(f'HTTP error occurred: {http_err}')
-            return None  
+            return None
         except Exception as err:
             print(f'Other error occurred: {err}')
-            return None 
+            return None
         else:
-            
+
             results = response.json()['data']['result']
             LOG.info('::::::::::RESULTS::::::::::::: {}'.format(each_label))
             for each_result in results:
                 LOG.info(each_result)
             list_of_result_sets.append(results)
     return list_of_result_sets
-
-
-# def main():
-#     set_log_config()
-#     load_and_validate_env_vars(LIST_OF_ENV_VARIABLES)
-#     query_m3db(LABELS)
-
-# if __name__ == "__main__":
-#     main()
\ No newline at end of file
diff --git a/vnfs/DAaaS/sample-apps/m3db_promql/promql_api/README.md b/vnfs/DAaaS/sample-apps/m3db_promql/promql_api/README.md
deleted file mode 100644 (file)
index 4493af2..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-## What does this API do ?
-This api as of now provides a function which takes in a lits of 'LABELS' of prometheus
-and returns the corresponding result_sets in a list.
-
-For eg:
-If the labels is
-
-```
-LABELS = ['irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])']
-```
-
-The return is:
-
-```
-[{'metric': {'cpufreq': '1',
-             'endpoint': 'collectd-prometheus',
-             'exported_instance': 'otconap7',
-             'instance': '172.25.103.1:9103',
-             'job': 'collectd',
-             'namespace': 'edge1',
-             'pod': 'plundering-liger-collectd-wz7xg',
-             'service': 'collectd'},
-  'value': [1559177169.415, '119727200']}]
-```
-
-## How to use this API ?
-
-```
-1. Copy the directory 'promql_api' to your working directory. 
-```
-
-```
-2. Import the API function: query_m3db
-from promql_api.prom_ql_api import query_m3db
-```
-
-```
-3. have a global or local variable as 'LABELS'
-LABELS = ['irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])']
-```
-
-```
-4. Store the result set in a list:
-list_of_result_sets = query_m3db(LABELS)
-```
-
-## How to troubleshoot ?
-
-* Check the sample file - sample_promql_query.py in the repo.
-* Make sure the file __init__.py is present in promql_api directory after you copy the directory.
index ef36080..270c508 100644 (file)
@@ -1,10 +1,14 @@
-from promql_api.prom_ql_api import query_m3db
+from promql_api.prom_ql_api import query
 import pprint
 
 LABELS = ['irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])']
 
+#Other examples
+#LABELS = [ 'irate(http_requests_total{code="200"}[1m])', 'collectd_cpu_percent{job="collectd", exported_instance="an11-31"}[1m]' ]
+#LABELS = ['irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])', 'go_info']
+
 def main():
-    list_of_result_sets = query_m3db(LABELS)
+    list_of_result_sets = query(LABELS)
     for each_result in list_of_result_sets:
         pprint.pprint(each_result)