Skip to content

Commit 632c419

Browse files
authored
Merge pull request #20 from yaacov/add-hawkular-old-api-option
Add option to use data restful endpoint
2 parents b5417ce + 5cac314 commit 632c419

2 files changed

Lines changed: 71 additions & 5 deletions

File tree

hawkular/metrics.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class HawkularMetricsError(HTTPError):
6767
class HawkularMetricsConnectionError(URLError):
6868
pass
6969

70+
class HawkularMetricsStatusError(ValueError):
71+
pass
72+
7073
class 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
"""
403443
Static methods
404444
"""

hawkular/metrics_test.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def test_add_availability_single(self):
177177
def test_add_string_single(self):
178178
self.client.push(MetricType.String, 'test.string.1', "foo")
179179
data = self.client.query_metric(MetricType.String, 'test.string.1')
180-
self.assertEqual(data, 'foo')
180+
self.assertEqual(data[0]["value"], 'foo')
181181

182182
def test_add_gauge_multi_datapoint(self):
183183
metric_1v = create_datapoint(float(1.45))
@@ -251,6 +251,32 @@ def test_tenant_changing(self):
251251
m = self.client.query_metric_definitions(MetricType.Availability)
252252
self.assertEqual(0, len(m))
253253

254-
254+
def test_query_semantic_version(self):
255+
major, minor, patch = self.client.query_semantic_version()
256+
self.assertTrue(0 <= major <= 1000)
257+
self.assertTrue(0 <= minor <= 1000)
258+
259+
def test_query_status(self):
260+
status = self.client.query_status()
261+
self.assertTrue('Implementation-Version' in status)
262+
263+
def test_get_metrics_raw_url(self):
264+
self.client.legacy_api = True
265+
url = self.client._get_metrics_raw_url('some.key')
266+
self.assertEqual('some.key/data', url)
267+
268+
self.client.legacy_api = False
269+
url = self.client._get_metrics_raw_url('some.key')
270+
self.assertEqual('some.key/raw', url)
271+
272+
def test_get_metrics_stats_url(self):
273+
self.client.legacy_api = True
274+
url = self.client._get_metrics_stats_url('some.key')
275+
self.assertEqual('some.key/data', url)
276+
277+
self.client.legacy_api = False
278+
url = self.client._get_metrics_stats_url('some.key')
279+
self.assertEqual('some.key/stats', url)
280+
255281
if __name__ == '__main__':
256282
unittest.main()

0 commit comments

Comments
 (0)