Skip to content

Commit fc39c0c

Browse files
committed
Improve support for querying for definitions and add support for upcoming feature of 0.16.0 to support querying tag values
1 parent 890dac6 commit fc39c0c

2 files changed

Lines changed: 48 additions & 43 deletions

File tree

hawkular/metrics.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
"""
3535
TODO: Search datapoints with tags.. tag datapoints.
3636
TODO: Allow changing instance's tenant?
37-
TODO: Authentication when it's done..
37+
TODO: Authentication
3838
TODO: Remove HawkularMetricsConnectionError and use HawkularMetricsError only?
3939
TODO: HWKMETRICS-110 (fetching a single definition)
40-
TODO: Tag queries, stats queries
40+
TODO: Stats queries
4141
"""
4242

4343
class MetricType:
@@ -50,6 +50,8 @@ class MetricType:
5050
def short(metric_type):
5151
if metric_type is MetricType.Gauge:
5252
return 'gauge'
53+
elif metric_type is MetricType.Counter:
54+
return 'counter'
5355
else:
5456
return 'availability'
5557

@@ -132,6 +134,10 @@ def _get_metrics_tags_url(self, metrics_url):
132134

133135
def _get_tenants_url(self):
134136
return self._get_base_url() + 'tenants'
137+
138+
@staticmethod
139+
def _transform_tags(**tags):
140+
return ','.join("%s:%s" % (key,val) for (key,val) in tags.items())
135141

136142
def _http(self, url, method, data=None):
137143
res = None
@@ -281,16 +287,38 @@ def query_single_availability(self, metric_id, **search_options):
281287
"""
282288
return self.query_metric(MetricType.Availability, metric_id, **search_options)
283289

284-
def query_definitions(self, query_type):
290+
def query_definitions(self, metric_type=None, id_filter=None, **tags):
291+
"""
292+
Query available metric definitions. Available query options are id_filter for id filtering and tags (dict) as tag based query.
293+
Tags filtering is required for the id filtering to work.
294+
"""
295+
if id is not None and tags is None:
296+
raise HawkularMetricsError('Tags query is required when id filter is used')
297+
298+
params = {}
299+
300+
if metric_type is not None:
301+
params = { 'type': MetricType.short(metric_type) }
302+
303+
if len(tags) > 0:
304+
params['tags'] = self._transform_tags(**tags)
305+
306+
return self._get(self._get_url('metrics'), **params)
307+
308+
def query_tagvalues(self, metric_type=None, **tags):
285309
"""
286-
Query available metric definitions.
310+
Query for possible tag values.
287311
"""
288-
definition_url = self._get_url('metrics') + '?type=' + MetricType.short(query_type)
289-
return self._get(definition_url)
312+
tagql = self._transform_tags(**tags)
313+
314+
if metric_type is None:
315+
metric_type = 'metrics'
316+
317+
return self._get(self._get_metrics_tags_url(self._get_url(metric_type)) + '/{}'.format(tagql))
290318

291319
def create_metric_definition(self, metric_type, metric_id, **tags):
292320
"""
293-
Create metric definition with custom definition. **options should be a set of tags, such as
321+
Create metric definition with custom definition. **tags should be a set of tags, such as
294322
units, env ..
295323
296324
Use methods create_gauge_definition and create_availability_definition to avoid using
@@ -334,7 +362,6 @@ def query_metric_tags(self, metric_type, metric_id):
334362
"""
335363
definition = self._get(self._get_metrics_tags_url(self._get_metrics_single_url(metric_type, metric_id)))
336364
return definition
337-
# return definition.get('tags', {})
338365

339366
def update_metric_tags(self, metric_type, metric_id, **tags):
340367
"""
@@ -346,7 +373,7 @@ def delete_metric_tags(self, metric_type, metric_id, **deleted_tags):
346373
"""
347374
Delete one or more tags from the metric definition. The tag values must match what's stored on the server.
348375
"""
349-
tags = ','.join("%s:%s" % (key,val) for (key,val) in deleted_tags.items())
376+
tags = self._transform_tags(**deleted_tags)
350377
tags_url = self._get_metrics_tags_url(self._get_metrics_single_url(metric_type, metric_id)) + '/{0}'.format(tags)
351378

352379
self._delete(tags_url)

hawkular/metrics_test.py

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,7 @@ def test_tenant_creation(self):
4242
self.assertIn(expect, tenants)
4343

4444
def test_tenant_creation_with_retentions_and_aggregations(self):
45-
# This feature isn't finished in the current master version of Hawkular-Metrics
4645
pass
47-
# tenant = str(uuid.uuid4())
48-
# self.client.create_tenant(tenant, 40)
49-
# tenants = self.client.query_tenants()
50-
51-
# expect = { 'id': tenant, 'dataRetention': 40 }
52-
# self.assertIn(expect, tenants)
5346

5447
class MetricsTestCase(TestMetricFunctionsBase):
5548
"""
@@ -122,18 +115,18 @@ def test_tags_modifications(self):
122115
tags_2 = self.client.query_metric_tags(MetricType.Gauge, m)
123116
self.assertEqual(0, len(tags_2))
124117

125-
# def test_tags_behavior(self):
126-
# print 'START: TEST TAGS'
127-
# metric = float(1.2345)
128-
# print 'CREATE'
129-
# self.client.create_gauge_definition('test.gauge.single.tags.1', hostname='')
130-
# print 'POST'
131-
# self.client.push('test.gauge.single.tags.1', metric, hostname='localhost')
132-
# print 'GET'
133-
# data = self.client.query_single_gauge('test.gauge.single.tags.1')
134-
# print data
135-
# print 'END: TEST TAGS'
136-
118+
def test_tags_queries(self):
119+
for i in range(1,9):
120+
m_id = 'test.query.tags.{}'.format(i)
121+
hostname = 'host{}'.format(i)
122+
self.client.create_metric_definition(MetricType.Counter, m_id, hostname=hostname, env='qa')
123+
124+
mds = self.client.query_definitions(hostname='host[123]')
125+
self.assertEqual(3, len(mds))
126+
127+
values = self.client.query_tagvalues(MetricType.Counter, hostname='host*', env='qa')
128+
self.assertEqual(2, len(values))
129+
137130
def test_add_gauge_single(self):
138131
# Normal way
139132
value = float(4.35)
@@ -223,21 +216,6 @@ def test_query_options(self):
223216
# Query for data which has start time limitation
224217
d = self.client.query_metric(MetricType.Gauge, 'test.query.gauge.1', start=(t-1000))
225218
self.assertEqual(1, len(d))
226-
227-
# This feature isn't really ready for prime time in Hawkular-Metrics yet..
228-
# def test_tags_finding(self):
229-
# # Create metrics with tags
230-
# m = 'test.create.data.tags.1'
231-
# self.client.create_metric_definition(MetricType.Gauge, m, ab='cd')
232-
# # Push some data to them
233-
# t = time_millis()
234-
# v = float(1.4)
235-
# self.client.push(MetricType.Gauge, m, v, t)
236-
# # Fetch data with certain tags
237-
# expected = { 'id': m, 'timestamp': t, 'value': v }
238-
# d = self.client.query_data_with_tags(MetricType.Gauge, ab='cd')
239-
# self.assertIsNotNone(d)
240-
# self.assertIn(expected, d)
241219

242220
if __name__ == '__main__':
243221
unittest.main()

0 commit comments

Comments
 (0)