Skip to content

Commit 2b357e0

Browse files
committed
Add support for changing address prefix of hawkular-metrics
1 parent 4598cd7 commit 2b357e0

2 files changed

Lines changed: 22 additions & 48 deletions

File tree

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@ To use hawkular-client-python in your own program, after installation import fro
1717

1818
Timestamps should be in the milliseconds after epoch and numeric values should be float. The client provides a method to request current time in milliseconds, ``time_millis()``
1919

20-
See metrics_test.py for more detailed examples.
20+
See metrics_test.py for more detailed examples. The tests target a running docker instance of Hawkular Kettle by default (change setUp() to override).
2121

2222
### General
2323

2424
When a method wants a metric_type one can use the shortcuts of MetricType.Numeric or MetricType.Availability. For availability values, one can use Availability.Up and Availability.Down to simplify usage.
2525

2626
```python
27-
>>> from hawkular import metrics
28-
>>> client = metrics.HawkularMetricsClient(tenant_id='doc', port=8081)
27+
>>> from hawkular.metrics import *
28+
>>> client = HawkularMetricsClient(tenant_id='doc', port=8081)
2929
```
3030

31-
See metrics_test.py for more detailed examples.
32-
3331
### Creating and modifying metric definitions
3432

3533
While creating a metric definition is not required to use them, it is possible to define a custom data retention times as well as tags for each metric. To create a metric, use method create_metric_definition(metric_id, metric_type, **tags). There are assistant methods create_numeric_definition(metric_id, **tags) and create_availability_definition(metric_id, **tags) which bypass the need for MetricType definition. The only reserved keyword for tags is dataRetention, which will change the dataRetention time, other tags are used for user's metadata.

hawkular/metrics.py

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
TODO: Search datapoints with tags.. tag datapoints.
99
TODO: Allow changing instance's tenant?
1010
TODO: Authentication when it's done..
11-
TODO: Remove HawkularMetricsConnectionError and use HawkularMetricsError only? HTTPError extends URLError..
12-
TODO: 0.3.3 will change /hawkular-metrics to hawkular/metrics ..
13-
TODO: 0.3.3 will also change lots of other small things on REST.
11+
TODO: Remove HawkularMetricsConnectionError and use HawkularMetricsError only?
12+
TODO: 0.3.3 will change /hawkular-metrics to hawkular/metrics and the REST-interfaces
1413
"""
1514

1615
class MetricType:
@@ -54,21 +53,23 @@ class HawkularMetricsClient:
5453
def __init__(self,
5554
tenant_id,
5655
host='localhost',
57-
port=8080):
56+
port=8081,
57+
path='hawkular-metrics'):
5858
"""
5959
A new instance of HawkularMetricsClient is created with the following defaults:
6060
6161
host = localhost
62-
port = 8080
63-
tenant_id = tenant_id
62+
port = 8081
63+
path = hawkular-metrics
6464
65-
The url that is called by default is:
65+
The url that is called by the client is:
6666
67-
http://{host}:{port}/hawkular-metrics/
67+
http://{host}:{port}/{2}/
6868
"""
6969
self.tenant_id = tenant_id
7070
self.host = host
7171
self.port = port
72+
self.path = path
7273

7374
opener = urllib2.build_opener(HTTPErrorProcessor())
7475
urllib2.install_opener(opener)
@@ -81,7 +82,7 @@ def _clean_metric_id(metric_id):
8182
return urllib.quote(metric_id, '')
8283

8384
def _get_base_url(self):
84-
return "http://{0}:{1}/hawkular-metrics/".format(self.host, str(self.port))
85+
return "http://{0}:{1}/{2}/".format(self.host, str(self.port), self.path)
8586

8687
def _get_url(self, service):
8788
return self._get_base_url() + '{0}/{1}'.format(self.tenant_id, service)
@@ -119,7 +120,6 @@ def _http(self, url, method, data=None):
119120
if method == 'GET':
120121
if res.getcode() == 200:
121122
data = json.load(res)
122-
# return data
123123
elif res.getcode() == 204:
124124
data = {}
125125

@@ -158,7 +158,7 @@ def _handle_error(self, e):
158158
err_d = json.loads(err_json)
159159
e.msg = err_d['errorMsg']
160160
except:
161-
# We keep the original payload, couldn't parse it
161+
# Keep the original payload, couldn't parse it
162162
e.msg = err_json
163163

164164
raise e
@@ -242,11 +242,6 @@ def query_definitions(self, query_type):
242242
definition_url = self._get_url('metrics') + '?type=' + MetricType.short(query_type)
243243
return self._get(definition_url)
244244

245-
# def query_metric_definition(self, metric_type, metric_id):
246-
# # This is actually using the tags method because of weird behavior in HWKMETRICS
247-
# # TODO Fix this once Hawkular-Metrics is fixed.
248-
# pass
249-
250245
def create_metric_definition(self, metric_type, metric_id, **tags):
251246
"""
252247
Create metric definition with custom definition. **options should be a set of tags, such as
@@ -284,42 +279,23 @@ def query_metric_tags(self, metric_type, metric_id):
284279
"""
285280
Returns a list of tags in the metric definition of metric_id
286281
"""
287-
# Slightly overlapping with query_definition, as that would return tags also..
288-
# 200 ok, 204 ok, but nothing found
289-
# @Path("/{tenantId}/metrics/numeric/{id}/tags")
290282
definition = self._get(self._get_metrics_tags_url(self._get_metrics_single_url(metric_type, metric_id)))
291283
return definition.get('tags', {})
292284

293285
def update_metric_tags(self, metric_type, metric_id, **tags):
294286
"""
295-
Replace the metric_id's tags
287+
Replace the metric_id's tags with given **tags
296288
"""
297-
# This will replace all the tags with PUT
298289
self._put(self._get_metrics_tags_url(self._get_metrics_single_url(metric_type, metric_id)), tags)
299290

300291
def delete_metric_tags(self, metric_type, metric_id, **deleted_tags):
301292
"""
302-
Delete one or more tags from the metric definition. The tag values must match what's on the server.
293+
Delete one or more tags from the metric definition. The tag values must match what's stored on the server.
303294
"""
304-
# 400 is tags are invalid
305295
tags = ','.join("%s:%s" % (key,val) for (key,val) in deleted_tags.iteritems())
306296
tags_url = self._get_metrics_tags_url(self._get_metrics_single_url(metric_type, metric_id)) + '/{0}'.format(tags)
307297

308-
self._delete(tags_url)
309-
310-
def query_data_with_tags(self, metric_type, **tags):
311-
# 400 if invalid tags, 204 is nothing found
312-
pass
313-
314-
# def change_datapoint_tags(self, metric_type, metric_id, timestamp=None, start=None, end=None, **tags):
315-
# # TagRequest model.. mm?
316-
# pass
317-
318-
319-
# def query_metric_definitions_with_tag(self, metric_type, **tags):
320-
# # The description in Metrics REST-API is confusing, which one is datapoints and which one definions? Fix.
321-
# pass
322-
298+
self._delete(tags_url)
323299

324300
"""
325301
Tenant related queries
@@ -351,12 +327,14 @@ def create_tenant(self, tenant_id):
351327
Static methods
352328
"""
353329
def time_millis():
330+
"""
331+
Returns current milliseconds since epoch
332+
"""
354333
return int(round(time.time() * 1000))
355334

356-
# This should datapoint dict actually..
357335
def create_datapoint(value, timestamp=None, **tags):
358336
"""
359-
Returns a single datapoint dict with a value, timestamp (optional - filled by the client if missing)
337+
Creates a single datapoint dict with a value, timestamp (optional - filled by the method if missing)
360338
and tags (optional)
361339
"""
362340
if timestamp is None:
@@ -370,11 +348,9 @@ def create_datapoint(value, timestamp=None, **tags):
370348

371349
return item
372350

373-
# This on the other hand is actually the metric ..
374351
def create_metric(metric_id, data):
375352
"""
376-
Create Hawkular-Metrics' submittable structure, metric_dict is a
377-
dict created with create_datapoint(value, timestamp)
353+
Create Hawkular-Metrics' submittable structure, data is a datapoint or list of datapoints
378354
"""
379355
if not isinstance(data, list):
380356
data = [data]

0 commit comments

Comments
 (0)