Skip to content

Commit cf82b1e

Browse files
committed
Add the ability to fetch stats with tags only, fixes #55 (#56)
(cherry picked from commit 76bd56b)
1 parent 627f0ea commit cf82b1e

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/docs/components/metrics/index.html) 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
@@ -177,12 +177,12 @@ def query_metric(self, metric_type, metric_id, start=None, end=None, **query_opt
177177
self._get_metrics_single_url(metric_type, metric_id)),
178178
**query_options)
179179

180-
def query_metric_stats(self, metric_type, metric_id, start=None, end=None, bucketDuration=None, **query_options):
180+
def query_metric_stats(self, metric_type, metric_id=None, start=None, end=None, bucketDuration=None, **query_options):
181181
"""
182182
Query for metric aggregates from the server. This is called buckets in the Hawkular-Metrics documentation.
183183
184184
:param metric_type: MetricType to be matched (required)
185-
:param metric_id: Exact string matching metric id
185+
:param metric_id: Exact string matching metric id or None for tags matching only
186186
:param start: Milliseconds since epoch or datetime instance
187187
:param end: Milliseconds since epoch or datetime instance
188188
:param bucketDuration: The timedelta or duration of buckets. Can be a string presentation or timedelta object
@@ -206,10 +206,14 @@ def query_metric_stats(self, metric_type, metric_id, start=None, end=None, bucke
206206
else:
207207
query_options['bucketDuration'] = bucketDuration
208208

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

214218
def query_metric_definition(self, metric_type, metric_id):
215219
"""

tests/test_metrics.py

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

309+
# Test previous metrics in a stacked configuration
310+
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')
311+
self.assertEqual(2.4, stacks[0]['min'], stacks)
312+
self.assertEqual(15.45, stacks[0]['max'], stacks)
313+
309314
def test_tenant_changing(self):
310315
self.client.create_metric_definition(MetricType.Availability, 'test.tenant.avail.1')
311316
# Fetch metrics and check that it did appear

0 commit comments

Comments
 (0)