Skip to content

Commit 76bd56b

Browse files
authored
Add the ability to fetch stats with tags only, fixes #55 (#56)
1 parent 7e2558a commit 76bd56b

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ This repository includes the necessary Python client libraries to access Hawkula
77

88
## Introduction
99

10-
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.x (tested on 2.7.5/2.7.6 and 2.7.10/2.7.13) and Python 3.4.x / Python 3.5.x (tested with the Python 3.4.2 and Python 3.5.3, might work with newer versions also).
10+
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.x (tested on 2.7.14) and Python 3.4.x / 3.5.x / 3.6.x (tested with the Python 3.4.2, Python 3.5.3 and Python 3.6.4, might work with newer versions also).
1111

1212
## License and copyright
1313

1414
```
15-
Copyright 2015-2017 Red Hat, Inc. and/or its affiliates
15+
Copyright 2015-2018 Red Hat, Inc. and/or its affiliates
1616
and other contributors.
1717
1818
Licensed under the Apache License, Version 2.0 (the "License");
@@ -38,7 +38,7 @@ To use hawkular-client-python in your own program, after installation import fro
3838

3939
The client provides a method to request current time in milliseconds, ``time_millis()`` that's accepted by the methods, but you can use ``datetime`` and ``timedelta`` to control the time also when sending requests to the Hawkular-Metrics.
4040

41-
See metrics_test.py for more detailed examples and [Hawkular-Metrics documentation](http://www.hawkular.org/hawkular-metrics/docs/user-guide/) for more detailed explanation of available features.
41+
See ``tests/test_metrics.py`` for more detailed examples and [Hawkular-Metrics documentation](http://www.hawkular.org/hawkular-metrics/docs/user-guide/) for more detailed explanation of available features.
4242

4343
### General
4444

hawkular/metrics.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ def query_metric(self, metric_type, metric_id, start=None, end=None, **query_opt
173173
self._get_metrics_single_url(metric_type, metric_id)),
174174
**query_options)
175175

176-
def query_metric_stats(self, metric_type, metric_id, start=None, end=None, bucketDuration=None, **query_options):
176+
def query_metric_stats(self, metric_type, metric_id=None, start=None, end=None, bucketDuration=None, **query_options):
177177
"""
178178
Query for metric aggregates from the server. This is called buckets in the Hawkular-Metrics documentation.
179179
180180
:param metric_type: MetricType to be matched (required)
181-
:param metric_id: Exact string matching metric id
181+
:param metric_id: Exact string matching metric id or None for tags matching only
182182
:param start: Milliseconds since epoch or datetime instance
183183
:param end: Milliseconds since epoch or datetime instance
184184
:param bucketDuration: The timedelta or duration of buckets. Can be a string presentation or timedelta object
@@ -202,10 +202,14 @@ def query_metric_stats(self, metric_type, metric_id, start=None, end=None, bucke
202202
else:
203203
query_options['bucketDuration'] = bucketDuration
204204

205-
return self._get(
206-
self._get_metrics_stats_url(
207-
self._get_metrics_single_url(metric_type, metric_id)),
208-
**query_options)
205+
if metric_id is not None:
206+
url = self._get_metrics_stats_url(self._get_metrics_single_url(metric_type, metric_id))
207+
else:
208+
if len(query_options) < 0:
209+
raise HawkularError('Tags are required when querying without metric_id')
210+
url = self._get_metrics_stats_url(self._get_url(metric_type))
211+
212+
return self._get(url, **query_options)
209213

210214
def query_metric_definition(self, metric_type, metric_id):
211215
"""

tests/test_metrics.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ def test_stats_queries(self):
309309
bp = self.client.query_metric_stats(MetricType.Gauge, 'test.buckets.1', bucketDuration=timedelta(seconds=2), start=now-timedelta(seconds=10), end=now, distinct=True)
310310
self.assertEqual(5, len(bp), "Single bucket is two seconds")
311311

312+
# Test previous metrics in a stacked configuration
313+
stacks = self.client.query_metric_stats(MetricType.Gauge, buckets=1, tags=create_tags_filter(units='bytes', env='unittest'), percentiles=create_percentiles_filter(90.0, 99.0), stacked='true')
314+
self.assertEqual(2.4, stacks[0]['min'], stacks)
315+
self.assertEqual(15.45, stacks[0]['max'], stacks)
316+
312317
def test_tenant_changing(self):
313318
self.client.create_metric_definition(MetricType.Availability, 'test.tenant.avail.1')
314319
# Fetch metrics and check that it did appear

0 commit comments

Comments
 (0)