Browse Source

Merge "Support customising Prometheus queries"

changes/45/824745/1
Zuul 4 months ago committed by Gerrit Code Review
parent
commit
78da8eac53
  1. 15
      cloudkitty/collector/prometheus.py
  2. 8
      cloudkitty/tests/collectors/test_validation.py
  3. 6
      doc/source/admin/configuration/collector.rst
  4. 5
      releasenotes/notes/prometheus-custom-query-ab2dc00e97b14be2.yaml

15
cloudkitty/collector/prometheus.py

@ -18,6 +18,7 @@ from decimal import ROUND_HALF_UP
from oslo_config import cfg
from oslo_log import log
from voluptuous import All
from voluptuous import In
from voluptuous import Optional
from voluptuous import Required
@ -83,7 +84,9 @@ PROMETHEUS_EXTRA_SCHEMA = {
'changes', 'delta', 'deriv',
'idelta', 'irange', 'irate',
'rate'
])
]),
Optional('query_prefix', default=''): All(str),
Optional('query_suffix', default=''): All(str),
}
}
@ -159,6 +162,8 @@ class PrometheusCollector(collector.BaseCollector):
'range_function')
groupby = self.conf[metric_name].get('groupby', [])
metadata = self.conf[metric_name].get('metadata', [])
query_prefix = self.conf[metric_name]['extra_args']['query_prefix']
query_suffix = self.conf[metric_name]['extra_args']['query_suffix']
period = tzutils.diff_seconds(end, start)
time = end
@ -198,6 +203,14 @@ class PrometheusCollector(collector.BaseCollector):
', '.join(groupby + metadata)
)
# Add custom query prefix
if query_prefix:
query = "{0} {1}".format(query_prefix, query)
# Add custom query suffix
if query_suffix:
query = "{0} {1}".format(query, query_suffix)
try:
res = self._conn.get_instant(
query,

8
cloudkitty/tests/collectors/test_validation.py

@ -135,6 +135,8 @@ class MetricConfigValidationTest(tests.TestCase):
expected_output['metric_one']['groupby'].append('project_id')
expected_output['metric_one']['extra_args'] = {
'aggregation_method': 'max',
'query_prefix': '',
'query_suffix': '',
}
self.assertEqual(
collector.prometheus.PrometheusCollector.check_configuration(data),
@ -147,6 +149,8 @@ class MetricConfigValidationTest(tests.TestCase):
expected_output['metric_one']['groupby'].append('project_id')
expected_output['metric_one']['extra_args'] = {
'aggregation_method': 'max',
'query_prefix': '',
'query_suffix': '',
}
self.assertEqual(
collector.prometheus.PrometheusCollector.check_configuration(data),
@ -158,6 +162,8 @@ class MetricConfigValidationTest(tests.TestCase):
data['metrics']['metric_one']['extra_args'] = {
'aggregation_method': 'max',
'query_function': 'abs',
'query_prefix': 'custom_prefix',
'query_suffix': 'custom_suffix',
'range_function': 'delta',
}
expected_output = copy.deepcopy(self.base_output)
@ -165,6 +171,8 @@ class MetricConfigValidationTest(tests.TestCase):
expected_output['metric_one']['extra_args'] = {
'aggregation_method': 'max',
'query_function': 'abs',
'query_prefix': 'custom_prefix',
'query_suffix': 'custom_suffix',
'range_function': 'delta',
}

6
doc/source/admin/configuration/collector.rst

@ -359,6 +359,12 @@ Prometheus
``log10``, ``round``, ``sqrt``. For more information on these functions,
you can check `this page`_
* ``query_prefix``: Optional argument. An arbitrary prefix to add to the
Prometheus query generated by CloudKitty, separated by a space.
* ``query_suffix``: Optional argument. An arbitrary suffix to add to the
Prometheus query generated by CloudKitty, separated by a space.
* ``range_function``: Optional argument. The function to apply instead of the
implicit ``{aggregation_method}_over_time``. Must be one of ``changes``,
``delta``, ``deriv``, ``idelta``, ``irange``, ``irate``, ``rate``. For more

5
releasenotes/notes/prometheus-custom-query-ab2dc00e97b14be2.yaml

@ -0,0 +1,5 @@
---
features:
- |
Adds support for specifying optional prefix and/or suffix to add to
Prometheus queries.
Loading…
Cancel
Save