forked from logzio/dashboard-metrics-extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeseries_extractor.py
60 lines (49 loc) · 2.63 KB
/
timeseries_extractor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
import logging
import requests
logger = logging.getLogger()
PROMETHEUS_API_QUERY_PREFIX = '/api/v1/query?query='
PROMETHEUS_TOTAL_TIMESERIES_COUNT_METRIC = 'prometheus_tsdb_head_series'
PROMETHEUS_LAST_OVER_TIME_QUERY_PREFIX = 'last_over_time('
PROMETHEUS_COUNT_FUNCTION_PREFIX = 'count('
CLOSING_PERENTHESIS = ')'
PROMETHEUS_FIVE_MINUTES_INTERVAL_TIME_FUNCTION_SUFFIX = '[5m])'
PROMETHEUS_METRIC_NAME_PREFIX = '{__name__=~"'
PROMETHEUS_METRIC_NAME_CLOSING_PERENTHESIS = '"}'
def _count_prometheus_total_timeseries(response):
response_json = json.loads(response.content)
timeseries_count = response_json.get('data').get('result')[0].get('value')[1]
print('Total time series count: {}'.format(timeseries_count))
def get_prometheus_timeseries_count(config: dict, metrics):
try:
prometheus_config = config['prometheus']
if prometheus_config.get('endpoint') is not None:
_get_total_timeseries_count(prometheus_config.get('endpoint'))
_get_used_timeseries_count(prometheus_config.get('endpoint'), metrics)
except KeyError:
logger.error('Invalid config for prometheus server, skipping time series count')
def _get_used_timeseries_count(endpoint, metrics):
query_url = endpoint + PROMETHEUS_API_QUERY_PREFIX + PROMETHEUS_COUNT_FUNCTION_PREFIX + PROMETHEUS_LAST_OVER_TIME_QUERY_PREFIX + PROMETHEUS_METRIC_NAME_PREFIX
metrics_regex = ''
for i, metric in enumerate(metrics):
metrics_regex += metric
if i < len(metrics) - 1:
metrics_regex += '|'
query_url += metrics_regex + PROMETHEUS_METRIC_NAME_CLOSING_PERENTHESIS + PROMETHEUS_FIVE_MINUTES_INTERVAL_TIME_FUNCTION_SUFFIX + CLOSING_PERENTHESIS
response = requests.get(query_url)
response_json = json.loads(response.content)
used_timeseries_count = response_json.get('data').get('result')[0].get('value')[1]
print(f'Total used time series in the last 5m: {used_timeseries_count}')
def _get_total_timeseries_count(endpoint):
try:
response = requests.get(
endpoint + PROMETHEUS_API_QUERY_PREFIX + PROMETHEUS_LAST_OVER_TIME_QUERY_PREFIX + PROMETHEUS_TOTAL_TIMESERIES_COUNT_METRIC + PROMETHEUS_FIVE_MINUTES_INTERVAL_TIME_FUNCTION_SUFFIX)
if (response.status_code != 200):
logger.error(
"Recieved status code: {} from prometheus, cannot complete the total time series request".format(
response.status_code))
return
_count_prometheus_total_timeseries(response)
except requests.HTTPError:
logger.error(
"Cannot get a response from prometheus server, please check the config file")