Skip to content

Commit 5b01c4c

Browse files
authored
Merge pull request #9 from stackhpc/victoria-update
Apply proper backport of Prometheus query customisation
2 parents 76a777c + 7a02994 commit 5b01c4c

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

cloudkitty/collector/prometheus.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
'idelta', 'irange', 'irate',
8686
'rate'
8787
]),
88+
Optional('query_prefix', default=''): All(str),
8889
Optional('query_suffix', default=''): All(str),
8990
}
9091
}
@@ -161,6 +162,7 @@ def fetch_all(self, metric_name, start, end, scope_id, q_filter=None):
161162
'range_function')
162163
groupby = self.conf[metric_name].get('groupby', [])
163164
metadata = self.conf[metric_name].get('metadata', [])
165+
query_prefix = self.conf[metric_name]['extra_args']['query_prefix']
164166
query_suffix = self.conf[metric_name]['extra_args']['query_suffix']
165167
period = tzutils.diff_seconds(end, start)
166168
time = end
@@ -201,12 +203,14 @@ def fetch_all(self, metric_name, start, end, scope_id, q_filter=None):
201203
', '.join(groupby + metadata)
202204
)
203205

204-
# Append custom query suffix
206+
# Add custom query prefix
207+
if query_prefix:
208+
query = "{0} {1}".format(query_prefix, query)
209+
210+
# Add custom query suffix
205211
if query_suffix:
206212
query = "{0} {1}".format(query, query_suffix)
207213

208-
LOG.debug("Calling Prometheus with query: %s", query)
209-
210214
try:
211215
res = self._conn.get_instant(
212216
query,

cloudkitty/tests/collectors/test_validation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ def test_prometheus_minimal_config_empty_extra_args(self):
131131
expected_output['metric_one']['groupby'].append('project_id')
132132
expected_output['metric_one']['extra_args'] = {
133133
'aggregation_method': 'max',
134+
'query_prefix': '',
135+
'query_suffix': '',
134136
}
135137
self.assertEqual(
136138
collector.prometheus.PrometheusCollector.check_configuration(data),
@@ -143,6 +145,8 @@ def test_prometheus_minimal_config_no_extra_args(self):
143145
expected_output['metric_one']['groupby'].append('project_id')
144146
expected_output['metric_one']['extra_args'] = {
145147
'aggregation_method': 'max',
148+
'query_prefix': '',
149+
'query_suffix': '',
146150
}
147151
self.assertEqual(
148152
collector.prometheus.PrometheusCollector.check_configuration(data),
@@ -154,13 +158,17 @@ def test_prometheus_minimal_config_minimal_extra_args(self):
154158
data['metrics']['metric_one']['extra_args'] = {
155159
'aggregation_method': 'max',
156160
'query_function': 'abs',
161+
'query_prefix': 'custom_prefix',
162+
'query_suffix': 'custom_suffix',
157163
'range_function': 'delta',
158164
}
159165
expected_output = copy.deepcopy(self.base_output)
160166
expected_output['metric_one']['groupby'].append('project_id')
161167
expected_output['metric_one']['extra_args'] = {
162168
'aggregation_method': 'max',
163169
'query_function': 'abs',
170+
'query_prefix': 'custom_prefix',
171+
'query_suffix': 'custom_suffix',
164172
'range_function': 'delta',
165173
}
166174

doc/source/admin/configuration/collector.rst

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

332+
* ``query_prefix``: Optional argument. An arbitrary prefix to add to the
333+
Prometheus query generated by CloudKitty, separated by a space.
334+
335+
* ``query_suffix``: Optional argument. An arbitrary suffix to add to the
336+
Prometheus query generated by CloudKitty, separated by a space.
337+
332338
* ``range_function``: Optional argument. The function to apply instead of the
333339
implicit ``{aggregation_method}_over_time``. Must be one of ``changes``,
334340
``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)