Skip to content

Commit

Permalink
Add the ability to fetch stats with tags only, fixes hawkular#55 (haw…
Browse files Browse the repository at this point in the history
…kular#56)

(cherry picked from commit 76bd56b)
  • Loading branch information
burmanm committed Mar 14, 2018
1 parent 627f0ea commit cf82b1e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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

Expand Down
16 changes: 10 additions & 6 deletions hawkular/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cf82b1e

Please sign in to comment.