Skip to content

Commit 7d4cf21

Browse files
authored
Merge pull request #12 from stackhpc/xena-backports
Apply Xena backports from Wallaby
2 parents a7c010e + bd75a5c commit 7d4cf21

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

cloudkitty/collector/prometheus.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from oslo_config import cfg
2020
from oslo_log import log
21+
from voluptuous import All
2122
from voluptuous import In
2223
from voluptuous import Optional
2324
from voluptuous import Required
@@ -83,7 +84,9 @@
8384
'changes', 'delta', 'deriv',
8485
'idelta', 'irange', 'irate',
8586
'rate'
86-
])
87+
]),
88+
Optional('query_prefix', default=''): All(str),
89+
Optional('query_suffix', default=''): All(str),
8790
}
8891
}
8992

@@ -159,6 +162,8 @@ def fetch_all(self, metric_name, start, end, scope_id, q_filter=None):
159162
'range_function')
160163
groupby = self.conf[metric_name].get('groupby', [])
161164
metadata = self.conf[metric_name].get('metadata', [])
165+
query_prefix = self.conf[metric_name]['extra_args']['query_prefix']
166+
query_suffix = self.conf[metric_name]['extra_args']['query_suffix']
162167
period = tzutils.diff_seconds(end, start)
163168
time = end
164169

@@ -198,6 +203,16 @@ def fetch_all(self, metric_name, start, end, scope_id, q_filter=None):
198203
', '.join(groupby + metadata)
199204
)
200205

206+
# Add custom query prefix
207+
if query_prefix:
208+
query = "{0} {1}".format(query_prefix, query)
209+
210+
# Add custom query suffix
211+
if query_suffix:
212+
query = "{0} {1}".format(query, query_suffix)
213+
214+
LOG.debug("Calling Prometheus with query: %s", query)
215+
201216
try:
202217
res = self._conn.get_instant(
203218
query,

cloudkitty/tests/collectors/test_validation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ def test_prometheus_minimal_config_empty_extra_args(self):
135135
expected_output['metric_one']['groupby'].append('project_id')
136136
expected_output['metric_one']['extra_args'] = {
137137
'aggregation_method': 'max',
138+
'query_prefix': '',
139+
'query_suffix': '',
138140
}
139141
self.assertEqual(
140142
collector.prometheus.PrometheusCollector.check_configuration(data),
@@ -147,6 +149,8 @@ def test_prometheus_minimal_config_no_extra_args(self):
147149
expected_output['metric_one']['groupby'].append('project_id')
148150
expected_output['metric_one']['extra_args'] = {
149151
'aggregation_method': 'max',
152+
'query_prefix': '',
153+
'query_suffix': '',
150154
}
151155
self.assertEqual(
152156
collector.prometheus.PrometheusCollector.check_configuration(data),
@@ -158,13 +162,17 @@ def test_prometheus_minimal_config_minimal_extra_args(self):
158162
data['metrics']['metric_one']['extra_args'] = {
159163
'aggregation_method': 'max',
160164
'query_function': 'abs',
165+
'query_prefix': 'custom_prefix',
166+
'query_suffix': 'custom_suffix',
161167
'range_function': 'delta',
162168
}
163169
expected_output = copy.deepcopy(self.base_output)
164170
expected_output['metric_one']['groupby'].append('project_id')
165171
expected_output['metric_one']['extra_args'] = {
166172
'aggregation_method': 'max',
167173
'query_function': 'abs',
174+
'query_prefix': 'custom_prefix',
175+
'query_suffix': 'custom_suffix',
168176
'range_function': 'delta',
169177
}
170178

doc/source/admin/configuration/collector.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ Prometheus
359359
``log10``, ``round``, ``sqrt``. For more information on these functions,
360360
you can check `this page`_
361361

362+
* ``query_prefix``: Optional argument. An arbitrary prefix to add to the
363+
Prometheus query generated by CloudKitty, separated by a space.
364+
365+
* ``query_suffix``: Optional argument. An arbitrary suffix to add to the
366+
Prometheus query generated by CloudKitty, separated by a space.
367+
362368
* ``range_function``: Optional argument. The function to apply instead of the
363369
implicit ``{aggregation_method}_over_time``. Must be one of ``changes``,
364370
``delta``, ``deriv``, ``idelta``, ``irange``, ``irate``, ``rate``. For more
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
Adds support for specifying optional prefix and/or suffix to add to
5+
Prometheus queries.

0 commit comments

Comments
 (0)