diff --git a/README.md b/README.md index fe1fd3b..2bf0356 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,12 @@ This repository includes the necessary Python client libraries to access Hawkula ## Introduction -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). +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). ## License and copyright ``` - Copyright 2015-2017 Red Hat, Inc. and/or its affiliates + Copyright 2015-2018 Red Hat, Inc. and/or its affiliates and other contributors. 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 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. -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. +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. ### General diff --git a/hawkular/metrics.py b/hawkular/metrics.py index ac18005..e50729b 100644 --- a/hawkular/metrics.py +++ b/hawkular/metrics.py @@ -177,12 +177,12 @@ def query_metric(self, metric_type, metric_id, start=None, end=None, **query_opt self._get_metrics_single_url(metric_type, metric_id)), **query_options) - def query_metric_stats(self, metric_type, metric_id, start=None, end=None, bucketDuration=None, **query_options): + def query_metric_stats(self, metric_type, metric_id=None, start=None, end=None, bucketDuration=None, **query_options): """ Query for metric aggregates from the server. This is called buckets in the Hawkular-Metrics documentation. :param metric_type: MetricType to be matched (required) - :param metric_id: Exact string matching metric id + :param metric_id: Exact string matching metric id or None for tags matching only :param start: Milliseconds since epoch or datetime instance :param end: Milliseconds since epoch or datetime instance :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 else: query_options['bucketDuration'] = bucketDuration - return self._get( - self._get_metrics_stats_url( - self._get_metrics_single_url(metric_type, metric_id)), - **query_options) + if metric_id is not None: + url = self._get_metrics_stats_url(self._get_metrics_single_url(metric_type, metric_id)) + else: + if len(query_options) < 0: + raise HawkularError('Tags are required when querying without metric_id') + url = self._get_metrics_stats_url(self._get_url(metric_type)) + + return self._get(url, **query_options) def query_metric_definition(self, metric_type, metric_id): """ diff --git a/tests/test_metrics.py b/tests/test_metrics.py index bbea3ea..7cf1d60 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -306,6 +306,11 @@ def test_stats_queries(self): bp = self.client.query_metric_stats(MetricType.Gauge, 'test.buckets.1', bucketDuration=timedelta(seconds=2), start=now-timedelta(seconds=10), end=now, distinct=True) self.assertEqual(5, len(bp), "Single bucket is two seconds") + # Test previous metrics in a stacked configuration + 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') + self.assertEqual(2.4, stacks[0]['min'], stacks) + self.assertEqual(15.45, stacks[0]['max'], stacks) + def test_tenant_changing(self): self.client.create_metric_definition(MetricType.Availability, 'test.tenant.avail.1') # Fetch metrics and check that it did appear