Skip to content

Commit e01125b

Browse files
authored
Merge pull request #30 from burmanm/metrics_doc_update
Update metrics documentation
2 parents 7938e40 + 32b0849 commit e01125b

4 files changed

Lines changed: 72 additions & 23 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
hawkular-client-python
22
=========================
33

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.
4+
This repository includes the necessary Python client libraries to access Hawkular remotely. Currently we only have a driver for the metrics and alerts components.
55

66
## Introduction
77

@@ -10,7 +10,7 @@ Python client to access Hawkular-Metrics, an abstraction to invoke REST-methods
1010
## License and copyright
1111

1212
```
13-
Copyright 2015-2016 Red Hat, Inc. and/or its affiliates
13+
Copyright 2015-2017 Red Hat, Inc. and/or its affiliates
1414
and other contributors.
1515
1616
Licensed under the Apache License, Version 2.0 (the "License");
@@ -30,7 +30,7 @@ Python client to access Hawkular-Metrics, an abstraction to invoke REST-methods
3030

3131
To install, run ``python setup.py install`` if you installed from source code, or ``pip install hawkular-client`` if using pip.
3232

33-
## Usage
33+
## Metrics Usage
3434

3535
To use hawkular-client-python in your own program, after installation import from hawkular the class HawkularMetricsClient and instantiate it. After this, push dicts with keys id, timestamp and value with put or use assistant method create to send events. pydoc gives the list of allowed parameters for each function.
3636

hawkular/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def __init__(self,
187187
# Call the server status endpoint to get the version number,
188188
# Use the return sematic version to set the value of legacy_api
189189
if auto_set_legacy_api:
190-
major, minor, patch = self.query_semantic_version()
190+
major, minor = self.query_semantic_version()
191191
self.legacy_api = (major == 0 and minor < 16)
192192

193193
def _get_base_url(self):
@@ -320,10 +320,10 @@ def query_semantic_version(self):
320320
status_hash = self.query_status()
321321
try:
322322
version = status_hash['Implementation-Version']
323-
major, minor, patch = map(int, version.split('.')[:3])
323+
major, minor = map(int, version.split('.')[:2])
324324
except Exception as e:
325325
self._handle_error(e)
326-
return major, minor, patch
326+
return major, minor
327327

328328
def query_status(self):
329329
return self._get(self._get_status_url())

hawkular/metrics.py

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,14 @@ class HawkularMetricsClient(HawkularBaseClient):
5858
"""
5959
Internal methods
6060
"""
61-
@staticmethod
62-
def _clean_metric_id(metric_id):
63-
return HawkularBaseClient.quote(metric_id)
64-
6561
def _get_url(self, metric_type=None):
6662
if metric_type is None:
6763
metric_type = MetricType._Metrics
6864

6965
return self._get_base_url() + '{0}'.format(metric_type)
7066

7167
def _get_metrics_single_url(self, metric_type, metric_id):
72-
return self._get_url(metric_type) + '/{0}'.format(self._clean_metric_id(metric_id))
68+
return self._get_url(metric_type) + '/{0}'.format(HawkularBaseClient.quote(metric_id))
7369

7470
def _get_metrics_raw_url(self, metrics_url):
7571
return metrics_url + '/data' if self.legacy_api else metrics_url + '/raw'
@@ -113,7 +109,7 @@ def put(self, data):
113109
Send multiple different metric_ids to the server in a single batch. Metrics can be a mixture
114110
of types.
115111
116-
data is a dict or a list of dicts created with create_metric(metric_type, metric_id, datapoints)
112+
:param data: A dict or a list of dicts created with create_metric(metric_type, metric_id, datapoints)
117113
"""
118114
if not isinstance(data, list):
119115
data = [data]
@@ -136,29 +132,57 @@ def push(self, metric_type, metric_id, value, timestamp=None):
136132
137133
This method is an assistant method for the put method by removing the need to
138134
create data structures first.
135+
136+
:param metric_type: MetricType to be matched (required)
137+
:param metric_id: Exact string matching metric id
138+
:param value: Datapoint value (depending on the MetricType)
139+
:param timestamp: Timestamp of the datapoint. If left empty, uses current client time.
139140
"""
140141
item = create_metric(metric_type, metric_id, create_datapoint(value, timestamp))
141142
self.put(item)
142143

143144
def query_metric(self, metric_type, metric_id, **query_options):
144145
"""
145-
Query for metrics from the server. For possible query_options, see the Hawkular-Metrics documentation.
146+
Query for metrics datapoints from the server.
147+
148+
:param metric_type: MetricType to be matched (required)
149+
:param metric_id: Exact string matching metric id
150+
:param query_options: For possible query_options, see the Hawkular-Metrics documentation.
146151
"""
147152
return self._get(
148153
self._get_metrics_raw_url(
149154
self._get_metrics_single_url(metric_type, metric_id)),
150155
**query_options)
151156

152157
def query_metric_stats(self, metric_type, metric_id, **query_options):
158+
"""
159+
Query for metric aggregates from the server. This is called buckets in the Hawkular-Metrics documentation.
160+
161+
:param metric_type: MetricType to be matched (required)
162+
:param metric_id: Exact string matching metric id
163+
:param query_options: For possible query_options, see the Hawkular-Metrics documentation.
164+
"""
153165
return self._get(
154166
self._get_metrics_stats_url(
155167
self._get_metrics_single_url(metric_type, metric_id)),
156168
**query_options)
169+
170+
def query_metric_definition(self, metric_type, metric_id):
171+
"""
172+
Query definition of a single metric id.
173+
174+
:param metric_type: MetricType to be matched (required)
175+
:param metric_id: Exact string matching metric id
176+
"""
177+
return self._get(self._get_metrics_single_url(metric_type, metric_id))
157178

158179
def query_metric_definitions(self, metric_type=None, id_filter=None, **tags):
159180
"""
160-
Query available metric definitions. Available query options are id_filter for id filtering and tags (dict) as tag based query.
161-
Tags filtering is required for the id filtering to work.
181+
Query available metric definitions.
182+
183+
:param metric_type: A MetricType to be queried. If left to None, matches all the MetricTypes
184+
:param id_filter: Filter the id with regexp is tag filtering is used, otherwise a list of exact metric ids
185+
:param tags: A dict of tag key/value pairs. Uses Hawkular-Metrics tag query language for syntax
162186
"""
163187
if id is not None and tags is None:
164188
raise HawkularMetricsError('Tags query is required when id filter is used')
@@ -175,8 +199,10 @@ def query_metric_definitions(self, metric_type=None, id_filter=None, **tags):
175199

176200
def query_tag_values(self, metric_type=None, **tags):
177201
"""
178-
Query for possible tag values. **tags is a dict which has tagname as key and regexp tagvalue as value. See Hawkular-Metrics
179-
tag query language for more detailed definition.
202+
Query for possible tag values.
203+
204+
:param metric_type: A MetricType to be queried. If left to None, matches all the MetricTypes
205+
:param tags: A dict of tag key/value pairs. Uses Hawkular-Metrics tag query language for syntax
180206
"""
181207
tagql = self._transform_tags(**tags)
182208

@@ -186,6 +212,10 @@ def create_metric_definition(self, metric_type, metric_id, **tags):
186212
"""
187213
Create metric definition with custom definition. **tags should be a set of tags, such as
188214
units, env ..
215+
216+
:param metric_type: MetricType of the new definition
217+
:param metric_id: metric_id is the string index of the created metric
218+
:param tags: Key/Value tag values of the new metric
189219
"""
190220
item = { 'id': metric_id }
191221
if len(tags) > 0:
@@ -209,20 +239,31 @@ def create_metric_definition(self, metric_type, metric_id, **tags):
209239

210240
def query_metric_tags(self, metric_type, metric_id):
211241
"""
212-
Returns a list of tags in the metric definition of metric_id
242+
Returns a list of tags in the metric definition.
243+
244+
:param metric_type: MetricType to be matched (required)
245+
:param metric_id: Exact string matching metric id
213246
"""
214247
definition = self._get(self._get_metrics_tags_url(self._get_metrics_single_url(metric_type, metric_id)))
215248
return definition
216249

217250
def update_metric_tags(self, metric_type, metric_id, **tags):
218251
"""
219252
Replace the metric_id's tags with given **tags
253+
254+
:param metric_type: MetricType to be matched (required)
255+
:param metric_id: Exact string matching metric id
256+
:param tags: Updated key/value tag values of the metric
220257
"""
221258
self._put(self._get_metrics_tags_url(self._get_metrics_single_url(metric_type, metric_id)), tags, parse_json=False)
222259

223260
def delete_metric_tags(self, metric_type, metric_id, **deleted_tags):
224261
"""
225262
Delete one or more tags from the metric definition.
263+
264+
:param metric_type: MetricType to be matched (required)
265+
:param metric_id: Exact string matching metric id
266+
:param deleted_tags: List of deleted tag names. Values can be set to anything
226267
"""
227268
tags = self._transform_tags(**deleted_tags)
228269
tags_url = self._get_metrics_tags_url(self._get_metrics_single_url(metric_type, metric_id)) + '/{0}'.format(tags)
@@ -243,14 +284,15 @@ def create_tenant(self, tenant_id, retentions=None):
243284
"""
244285
Create a tenant. Currently nothing can be set (to be fixed after the master
245286
version of Hawkular-Metrics has fixed implementation.
287+
288+
:param retentions: A set of retention settings, see Hawkular-Metrics documentation for more info
246289
"""
247290
item = { 'id': tenant_id }
248291
if retentions is not None:
249292
item['retentions'] = retentions
250293

251294
self._post(self._get_tenants_url(), json.dumps(item, indent=2))
252295

253-
254296
"""
255297
Static methods
256298
"""
@@ -262,8 +304,11 @@ def time_millis():
262304

263305
def create_datapoint(value, timestamp=None, **tags):
264306
"""
265-
Creates a single datapoint dict with a value, timestamp (optional - filled by the method if missing)
266-
and tags (optional)
307+
Creates a single datapoint dict with a value, timestamp and tags.
308+
309+
:param value: Value of the datapoint. Type depends on the id's MetricType
310+
:param timestamp: Optional timestamp of the datapoint. Uses client current time if not set. Millisecond accuracy
311+
:param tags: Optional datapoint tags. Not to be confused with metric definition tags
267312
"""
268313
if timestamp is None:
269314
timestamp = time_millis()
@@ -278,7 +323,11 @@ def create_datapoint(value, timestamp=None, **tags):
278323

279324
def create_metric(metric_type, metric_id, data):
280325
"""
281-
Create Hawkular-Metrics' submittable structure, data is a datapoint or list of datapoints
326+
Create Hawkular-Metrics' submittable structure.
327+
328+
:param metric_type: MetricType to be matched (required)
329+
:param metric_id: Exact string matching metric id
330+
:param data: A datapoint or a list of datapoints created with create_datapoint(value, timestamp, tags)
282331
"""
283332
if not isinstance(data, list):
284333
data = [data]

tests/test_metrics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def test_tenant_changing(self):
260260
self.assertEqual(0, len(m))
261261

262262
def test_query_semantic_version(self):
263-
major, minor, patch = self.client.query_semantic_version()
263+
major, minor = self.client.query_semantic_version()
264264
self.assertTrue(0 <= major <= 1000)
265265
self.assertTrue(0 <= minor <= 1000)
266266

0 commit comments

Comments
 (0)