Skip to content

Commit

Permalink
V0.1.1
Browse files Browse the repository at this point in the history
* fix get query too long

* update script description
  • Loading branch information
tamir-michaeli authored Dec 6, 2023
1 parent 84221fa commit eb27237
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Dashboard-metrics-extractor
Python scripts that extract metric names from Grafana dashboards, and time series from prometheus.
The script will extract the metric names from all grafana dashboards and provide a regex that can be used to with prometheus filter or telegraf fieldpass.
When extracting metrics from grafana endpoint, the script will also count the total time series in the provided prometheus database (within interval)
and count the used timeseries of the extracted dashboard metrics (within interval).

## How to use

Expand All @@ -12,7 +15,7 @@ You can start the dashboard metrics extractor either by running it as a python s
Run the following command in the terminal:

```bash
$ curl -L -O https://github.com/logzio/dashboard-metrics-extractor/releases/download/V0.1.0/extract \
$ curl -L -O https://github.com/logzio/dashboard-metrics-extractor/releases/download/V0.1.1/extract \
&& sudo chmod 755 extract \
&& ./extract
```
Expand Down
3 changes: 3 additions & 0 deletions extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import metrics_dashboard_extractor
import timeseries_extractor

SCRIPT_VERSION = "0.1.1"

if __name__ == '__main__':
print(f"*** Running script version {SCRIPT_VERSION} ***")
menu_choice = settings_reader.read_menu_input()
if menu_choice == 1:
config = settings_reader.get_config()
Expand Down
15 changes: 9 additions & 6 deletions timeseries_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

logger = logging.getLogger()

PROMETHEUS_API_QUERY_PREFIX = '/api/v1/query?query='
PROMETHEUS_BASE_QUERY_URL='/api/v1/query'
PROMETHEUS_API_QUERY_PREFIX = '?query='
PROMETHEUS_TOTAL_TIMESERIES_COUNT_METRIC = 'prometheus_tsdb_head_series['
PROMETHEUS_LAST_OVER_TIME_QUERY_PREFIX = 'last_over_time('
PROMETHEUS_COUNT_FUNCTION_PREFIX = 'count('
Expand Down Expand Up @@ -60,15 +61,17 @@ def extract_timeseries_interval(prometheus_config):


def _get_used_timeseries_count(endpoint, used_timeseries_interval, metrics):
query_url = endpoint + PROMETHEUS_API_QUERY_PREFIX + PROMETHEUS_COUNT_FUNCTION_PREFIX + PROMETHEUS_LAST_OVER_TIME_QUERY_PREFIX + PROMETHEUS_METRIC_NAME_PREFIX
metrics_regex = ''
query_url = endpoint + PROMETHEUS_BASE_QUERY_URL
metrics_regex = PROMETHEUS_COUNT_FUNCTION_PREFIX + PROMETHEUS_LAST_OVER_TIME_QUERY_PREFIX + PROMETHEUS_METRIC_NAME_PREFIX
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 + f'[{used_timeseries_interval}])' + CLOSING_PERENTHESIS
metrics_regex += PROMETHEUS_METRIC_NAME_CLOSING_PERENTHESIS + f'[{used_timeseries_interval}])' + CLOSING_PERENTHESIS
post_query = {'query': metrics_regex}
logger.info(f"Prometheus used timeseries count query url: {query_url}")
response = requests.get(query_url)
logger.info(f"Prometheus used timeseries count query body: {metrics_regex}")
response = requests.post(query_url, data=post_query)
if response.status_code == 200:
response_json = json.loads(response.content)
try:
Expand All @@ -83,7 +86,7 @@ def _get_used_timeseries_count(endpoint, used_timeseries_interval, metrics):


def _get_total_timeseries_count(endpoint, total_count_timeseries_interval):
total_timeseries_count_url = endpoint + PROMETHEUS_API_QUERY_PREFIX + PROMETHEUS_LAST_OVER_TIME_QUERY_PREFIX + PROMETHEUS_TOTAL_TIMESERIES_COUNT_METRIC + total_count_timeseries_interval + PROMETHEUS_INTERVAL_TIME_FUNCTION_SUFFIX
total_timeseries_count_url = endpoint +PROMETHEUS_BASE_QUERY_URL+ PROMETHEUS_API_QUERY_PREFIX + PROMETHEUS_LAST_OVER_TIME_QUERY_PREFIX + PROMETHEUS_TOTAL_TIMESERIES_COUNT_METRIC + total_count_timeseries_interval + PROMETHEUS_INTERVAL_TIME_FUNCTION_SUFFIX
logger.info(f"Prometheus total timeseries count query url: {total_timeseries_count_url}")
try:
response = requests.get(
Expand Down

0 comments on commit eb27237

Please sign in to comment.