Helmize the sample promql_query_app
[demo.git] / vnfs / DAaaS / lib / promql_api / README.md
1 ## What does this API do ?
2 This api as of now provides a function which takes in a list of 'QUERY_STRINGs' with respect to prometheus
3 and returns the corresponding result_sets in a list.
4
5 For eg:
6 If the QUERY_STRING is
7
8 ```
9 QUERY_STRING = ['irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])']
10 ```
11
12 The return is:
13
14 ```
15 [{'metric': {'cpufreq': '1',
16              'endpoint': 'collectd-prometheus',
17              'exported_instance': 'otconap7',
18              'instance': '172.25.103.1:9103',
19              'job': 'collectd',
20              'namespace': 'edge1',
21              'pod': 'plundering-liger-collectd-wz7xg',
22              'service': 'collectd'},
23   'value': [1559177169.415, '119727200']}]
24 ```
25
26 ## How to use this API ?
27
28 ```
29 1. Copy the directory 'promql_api' to your working directory. 
30 ```
31
32 ```
33 2. Import the API function: query
34 from promql_api.prom_ql_api import query
35 ```
36
37 ```
38 3. have a global or local variable as 'QUERY_STRING'
39 QUERY_STRING = ['irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])']
40 ```
41
42 ```
43 4. Store the result set in a list:
44 list_of_result_sets = query(QUERY_STRING)
45 ```
46
47 ## Troubleshooting tips
48
49 * Check the sample file - sample_promql_query.py in the repo ( sample-apps/m3db_promql)
50 * Make sure the file "__init__.py" is present in promql_api directory after you copy the directory.
51 * Make sure environment variables like "DATA_ENDPOINT" are correctly set.
52 * For custom and advanced querying https://prometheus.io/docs/prometheus/latest/querying/api/
53 * Logs are generated in the directory where the query function is called.
54 * sample log file - promql_api.log
55
56 ```
57  05-30-2019 08:47:53PM ::prom_ql_api.py :: set_log_config :: INFO :: Set the log configs.
58  05-30-2019 08:47:53PM ::prom_ql_api.py :: load_and_validate_env_vars :: INFO :: Loading the env variables ...
59  05-30-2019 08:47:53PM ::prom_ql_api.py :: load_and_validate_env_vars :: ERROR :: Env var: DATA_ENDPOINT not         found !
60  05-30-2019 08:47:53PM ::prom_ql_api.py :: query :: INFO :: Forming the get request ...
61  05-30-2019 08:47:53PM ::prom_ql_api.py :: query :: INFO :: API request::: URL: http://172.25.103.1:30090/api/v1/    query
62  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])'}
63  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
64  05-30-2019 08:47:53PM ::prom_ql_api.py :: query :: INFO :: ::::::::::RESULTS:::::::::::::                           irate(collectd_cpufreq{exported_instance="otconap7",cpufreq="1"}[2m])
65  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']}
66  ```
67
68  * Tested Error scenario: Configure QUERY_STRING as :
69  ```
70  QUERY_STRING = ['irate(collectd_cpufreq{exported_instance="otconap7", cpufreq="1"}[2m])', 'collectd_cpu_percent{job="collectd" exported_instance="an11-31"}[1m]']
71  ```
72  O/P :
73  ```
74  Check logs..HTTP error occurred: 400 Client Error: Bad Request for url: http://172.25.103.1:30090/api/v1/query?query=collectd_cpu_percent%7Bjob%3D%22collectd%22+exported_instance%3D%22an11-31%22%7D%5B1m%5D
75 [{'metric': {'cpufreq': '1',
76              'endpoint': 'collectd-prometheus',
77              'exported_instance': 'otconap7',
78              'instance': '172.25.103.1:9103',
79              'job': 'collectd',
80              'namespace': 'edge1',
81              'pod': 'plundering-liger-collectd-wz7xg',
82              'service': 'collectd'},
83   'value': [1559343866.631, '119798600']}]
84 {'error': 'parse error at char 37: missing comma before next identifier '
85           '"exported_instance"',
86  'errorType': 'bad_data'}
87 ```