|
| 1 | +hawkular-client-python |
| 2 | +========================= |
| 3 | + |
| 4 | +This repository includes the necessary Python client libraries to access Hawkular remotely. Currently we only have a driver for the metrics component as it's the most mature of the components. |
| 5 | + |
| 6 | +## Introduction |
| 7 | + |
| 8 | +Python client to access Hawkular-Metrics, an abstraction to invoke REST-methods on the server endpoint using urllib2. No external dependencies, works with Python 2.7 (Python 3.x support planned). |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +To install, run ``python setup.py install`` |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +To use hawkular-client-python in your own program, after installation import from hawkular the class HawkularMetricsClient and instantiate it. It accepts parameters tenant_id, hostname and port. After this, push dicts with keys id, timestamp and value with put or use assistant method create to send events. |
| 17 | + |
| 18 | +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()`` |
| 19 | + |
| 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). |
| 21 | + |
| 22 | +### General |
| 23 | + |
| 24 | +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. |
| 25 | + |
| 26 | +```python |
| 27 | +>>> from hawkular.metrics import * |
| 28 | +>>> client = HawkularMetricsClient(tenant_id='doc', port=8081) |
| 29 | +``` |
| 30 | + |
| 31 | +### Creating and modifying metric definitions |
| 32 | + |
| 33 | +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. |
| 34 | + |
| 35 | +Example: |
| 36 | + |
| 37 | +```python |
| 38 | +>>> client.create_numeric_definition('example.metric.1', dataRetention=90, units='bytes', hostname='localhost') |
| 39 | +>>> client.query_definitions(metrics.MetricType.Numeric) |
| 40 | +[{u'tags': {u'units': u'bytes', u'hostname': u'localhost'}, u'id': u'example.metric.1', u'dataRetention': 90, u'tenantId': u'doc'}] |
| 41 | +``` |
| 42 | + |
| 43 | +### Modifying metric definition tags |
| 44 | + |
| 45 | +One powerful feature of Hawkular-Metrics is the tagging feature that allows one to define descriptive metadata for any metric. Tags can be added when creating a metric definition (see above), but also modified later. |
| 46 | + |
| 47 | +Example: |
| 48 | + |
| 49 | +```python |
| 50 | +>>> client.create_numeric_definition('example.metric.1', dataRetention=90, units='bytes', hostname='localhost') |
| 51 | +>>> client.query_metric_tags(MetricType.Numeric, 'example.metric.1') |
| 52 | +{u'units': u'bytes', u'hostname': u'localhost'} |
| 53 | +>>> client.delete_metric_tags(MetricType.Numeric, 'example.metric.1', units='bytes') |
| 54 | +>>> client.query_metric_tags(MetricType.Numeric, 'example.metric.1') |
| 55 | +{u'hostname': u'localhost'} |
| 56 | +>>> client.update_metric_tags(MetricType.Numeric, 'example.metric.1', hostname='machine1', env='test') |
| 57 | +>>> client.query_metric_tags(MetricType.Numeric, 'example.metric.1') |
| 58 | +{u'hostname': u'machine1', u'env': u'test'} |
| 59 | +``` |
| 60 | + |
| 61 | +### Pushing new values |
| 62 | + |
| 63 | +All the methods that allow pushing values can accept both availability status as well as numeric values. It is possible to push multiple metrics with multiple values per metric in one call to the Hawkular-Metrics. However for convenience, methods which will push just one value for one metric is also provided. To push availability values, use MetricType.Availability and values Availability.Up and Availability.Down, otherwise the syntax is equal. |
| 64 | + |
| 65 | +Example pushing a single value: |
| 66 | + |
| 67 | +```python |
| 68 | +datapoint = create_datapoint(float(4.35), time_millis()) |
| 69 | +metric = create_metric('example.metric.1', datapoint) |
| 70 | +client.put(MetricType.Numeric, metric) |
| 71 | +``` |
| 72 | + |
| 73 | +And a shortcut method for the above: |
| 74 | + |
| 75 | +```python |
| 76 | +client.push(MetricType.Numeric, 'example.metric.1', float(4.35)) |
| 77 | +``` |
| 78 | + |
| 79 | +Example pushing multiple values for the same metric (with given timestamp and without): |
| 80 | + |
| 81 | +```python |
| 82 | +>>> v1 = create_datapoint(float(2.345)) # Timestamp is autogenerated |
| 83 | +>>> v2 = create_datapoint(float(3.45), 1429711362289) # Timestamp is given |
| 84 | +>>> m = create_metric('example.metric.2', [v1, v2]) |
| 85 | +>>> client.put(MetricType.Numeric, m) |
| 86 | +>>> client.query_single_numeric('example.metric.2') |
| 87 | +[{u'timestamp': 1429711362289, u'value': 3.45}, {u'timestamp': 1429711311895, u'value': 2.345}] |
| 88 | +``` |
| 89 | + |
| 90 | +To push multiple metrics with multiple values per metric, see metrics_test.py and method ``test_add_multi_metrics_and_datapoints()``. |
| 91 | + |
| 92 | +### Querying metric values |
| 93 | + |
| 94 | +Querying metrics is limited to just metric_id querying now with given search_options. Search for tagged data is coming up later. The supported parameters are ``start``, ``end``, ``buckets``, ``bucketDuration`` and ``distinct``. The returned data structure will change depending on the given parameters. |
| 95 | + |
| 96 | +```python |
| 97 | +>>> c.query_metric(MetricType.Numeric, 'test.query.numeric.1') |
| 98 | +[{u'timestamp': 1429816731689, u'value': 1.45}, {u'timestamp': 1429816729689, u'value': 2.0}] |
| 99 | +>>> c.query_metric(MetricType.Numeric, 'test.query.numeric.1', start=1429816731689) |
| 100 | +[{u'timestamp': 1429816731689, u'value': 1.45}] |
| 101 | +>>> c.query_metric(MetricType.Numeric, 'test.query.numeric.1', buckets=1) |
| 102 | +[{u'end': 1429816997651, u'min': 1.45, u'max': 2.0, u'median': 1.725, u'value': u'NaN', u'start': 1429788197651, u'avg': 1.725, u'empty': False, u'percentile95th': 2.0}] |
| 103 | +``` |
| 104 | + |
| 105 | +## Method documentation |
| 106 | + |
| 107 | +Method documentation is available with ``pydoc hawkular`` |
0 commit comments