Skip to content

Commit

Permalink
Merge pull request #9 from stackhpc/victoria-update
Browse files Browse the repository at this point in the history
Apply proper backport of Prometheus query customisation
  • Loading branch information
priteau authored Jun 13, 2022
2 parents 76a777c + 7a02994 commit 5b01c4c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
10 changes: 7 additions & 3 deletions cloudkitty/collector/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
'idelta', 'irange', 'irate',
'rate'
]),
Optional('query_prefix', default=''): All(str),
Optional('query_suffix', default=''): All(str),
}
}
Expand Down Expand Up @@ -161,6 +162,7 @@ def fetch_all(self, metric_name, start, end, scope_id, q_filter=None):
'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
Expand Down Expand Up @@ -201,12 +203,14 @@ def fetch_all(self, metric_name, start, end, scope_id, q_filter=None):
', '.join(groupby + metadata)
)

# Append custom query suffix
# 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)

LOG.debug("Calling Prometheus with query: %s", query)

try:
res = self._conn.get_instant(
query,
Expand Down
8 changes: 8 additions & 0 deletions cloudkitty/tests/collectors/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ def test_prometheus_minimal_config_empty_extra_args(self):
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),
Expand All @@ -143,6 +145,8 @@ def test_prometheus_minimal_config_no_extra_args(self):
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),
Expand All @@ -154,13 +158,17 @@ def test_prometheus_minimal_config_minimal_extra_args(self):
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)
expected_output['metric_one']['groupby'].append('project_id')
expected_output['metric_one']['extra_args'] = {
'aggregation_method': 'max',
'query_function': 'abs',
'query_prefix': 'custom_prefix',
'query_suffix': 'custom_suffix',
'range_function': 'delta',
}

Expand Down
6 changes: 6 additions & 0 deletions doc/source/admin/configuration/collector.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
features:
- |
Adds support for specifying optional prefix and/or suffix to add to
Prometheus queries.

0 comments on commit 5b01c4c

Please sign in to comment.