5
5
import time
6
6
import dateparser
7
7
import sys
8
+ import os
8
9
from retrying import retry
10
+ import bz2
9
11
10
12
# set up logging
11
13
import logging
@@ -55,7 +57,7 @@ def get_current_metric_value(self, metric_name, label_config = None):
55
57
query = metric_name + "{" + "," .join (label_list ) + "}"
56
58
else :
57
59
query = metric_name
58
-
60
+
59
61
response = requests .get ('{0}/api/v1/query' .format (self .url ), # using the query API to get raw data
60
62
params = {'query' : query },#label_config},
61
63
verify = False , # Disable ssl certificate verification temporarily
@@ -72,7 +74,7 @@ def get_current_metric_value(self, metric_name, label_config = None):
72
74
return (data )
73
75
74
76
@retry (stop_max_attempt_number = MAX_REQUEST_RETRIES , wait_fixed = CONNECTION_RETRY_WAIT_TIME )
75
- def get_metric_range_data (self , metric_name , start_time , end_time = 'now' , chunk_size = None ,label_config = None ):
77
+ def get_metric_range_data (self , metric_name , start_time , end_time = 'now' , chunk_size = None ,label_config = None , store_locally = False ):
76
78
data = []
77
79
78
80
start = int (dateparser .parse (str (start_time )).timestamp ())
@@ -110,5 +112,44 @@ def get_metric_range_data(self, metric_name, start_time, end_time='now', chunk_s
110
112
requests .status_codes ._codes [response .status_code ][0 ],
111
113
response .content
112
114
))
115
+ if store_locally :
116
+ # store it locally
117
+ self .store_metric_values_local (metric_name , (response .json ()['data' ]['result' ]), start + chunk_seconds )
118
+
113
119
start += chunk_seconds
114
120
return (data )
121
+
122
+ def store_metric_values_local (self , metric_name , values , end_timestamp , file_path = None , compressed = True ):
123
+ '''
124
+ Function to store metrics locally
125
+ '''
126
+ if not values :
127
+ return "No values for {}" .format (metric_name )
128
+
129
+ if not file_path :
130
+ file_path = self ._metric_filename (metric_name , end_timestamp )
131
+
132
+ if compressed :
133
+ payload = bz2 .compress (str (values ).encode ('utf-8' ))
134
+ file_path = file_path + ".bz2"
135
+ else :
136
+ payload = (str (values ).encode ('utf-8' ))
137
+
138
+ os .makedirs (os .path .dirname (file_path ), exist_ok = True )
139
+ with open (file_path , "wb" ) as file :
140
+ file .write (payload )
141
+
142
+ def _metric_filename (self , metric_name , end_timestamp ):
143
+ '''
144
+ Adds a timestamp to the filename before it is stored
145
+ '''
146
+ end_timestamp = dateparser .parse (str (end_timestamp ))
147
+ directory_name = end_timestamp .strftime ("%Y%m%d" )
148
+ timestamp = end_timestamp .strftime ("%Y%m%d%H%M" )
149
+ object_path = "./metrics/" + self .prometheus_host + "/" + metric_name + "/" + directory_name + "/" + timestamp + ".json"
150
+ return object_path
151
+
152
+ def pretty_print_metric (self , metric_data ):
153
+ data = metric_data
154
+ for metric in data :
155
+ print (json .dumps (metric , indent = 4 , sort_keys = True ))
0 commit comments