@@ -67,6 +67,9 @@ class HawkularMetricsError(HTTPError):
6767class HawkularMetricsConnectionError (URLError ):
6868 pass
6969
70+ class HawkularMetricsStatusError (ValueError ):
71+ pass
72+
7073class HawkularHTTPErrorProcessor (HTTPErrorProcessor ):
7174 """
7275 Hawkular-Metrics uses http codes 201, 204
@@ -94,7 +97,8 @@ def __init__(self,
9497 context = None ,
9598 token = None ,
9699 username = None ,
97- password = None ):
100+ password = None ,
101+ auto_set_legacy_api = True ):
98102 """
99103 A new instance of HawkularMetricsClient is created with the following defaults:
100104
@@ -118,10 +122,17 @@ def __init__(self,
118122 self .token = token
119123 self .username = username
120124 self .password = password
125+ self .legacy_api = False
121126
122127 opener = build_opener (HawkularHTTPErrorProcessor ())
123128 install_opener (opener )
124129
130+ # Call the server status endpoint to get the version number,
131+ # Use the return sematic version to set the value of legacy_api
132+ if auto_set_legacy_api :
133+ major , minor , patch = self .query_semantic_version ()
134+ self .legacy_api = (major == 0 and minor < 16 )
135+
125136 """
126137 Internal methods
127138 """
@@ -142,17 +153,20 @@ def _get_metrics_single_url(self, metric_type, metric_id):
142153 return self ._get_url (metric_type ) + '/{0}' .format (self ._clean_metric_id (metric_id ))
143154
144155 def _get_metrics_raw_url (self , metrics_url ):
145- return metrics_url + '/raw'
156+ return metrics_url + '/data' if self . legacy_api else metrics_url + '/ raw'
146157
147158 def _get_metrics_stats_url (self , metrics_url ):
148- return metrics_url + '/stats'
159+ return metrics_url + '/data' if self . legacy_api else metrics_url + '/ stats'
149160
150161 def _get_metrics_tags_url (self , metrics_url ):
151162 return metrics_url + '/tags'
152163
153164 def _get_tenants_url (self ):
154165 return self ._get_base_url () + 'tenants'
155166
167+ def _get_status_url (self ):
168+ return self ._get_base_url () + 'status'
169+
156170 @staticmethod
157171 def _transform_tags (** tags ):
158172 return ',' .join ("%s:%s" % (key ,val ) for (key ,val ) in tags .items ())
@@ -236,6 +250,16 @@ def _handle_error(self, e):
236250 e .__class__ = HawkularMetricsConnectionError
237251 e .msg = "Error, could not send event(s) to the Hawkular Metrics: " + str (e .reason )
238252 raise e
253+ elif isinstance (e , KeyError ):
254+ # Cast to HawkularMetricsStatusError
255+ e .__class__ = HawkularMetricsStatusError
256+ e .msg = "Error, unable to get implementation version for metrics: " + str (e .reason )
257+ raise e
258+ elif isinstance (e , ValueError ):
259+ # Cast to HawkularMetricsStatusError
260+ e .__class__ = HawkularMetricsStatusError
261+ e .msg = "Error, unable to determine implementation version for metrics: " + str (e .reason )
262+ raise e
239263 else :
240264 raise e
241265
@@ -399,6 +423,22 @@ def create_tenant(self, tenant_id, retentions=None):
399423
400424 self ._post (self ._get_tenants_url (), json .dumps (item , indent = 2 ))
401425
426+ """
427+ General information related queries
428+ """
429+
430+ def query_semantic_version (self ):
431+ status_hash = self .query_status ()
432+ try :
433+ version = status_hash ['Implementation-Version' ]
434+ major , minor , patch = map (int , version .split ('.' )[:3 ])
435+ except Exception as e :
436+ self ._handle_error (e )
437+ return major , minor , patch
438+
439+ def query_status (self ):
440+ return self ._get (self ._get_status_url ())
441+
402442"""
403443Static methods
404444"""
0 commit comments