Skip to content

Commit 263d558

Browse files
authored
mixin-utils: add disable option to showClassicHistogramQuery (#1567)
For the case when you want to migrate some query , but the namespace it acts on isn't fully migrated yet. Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
1 parent 581d242 commit 263d558

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

mixin-utils/test/test_native-classic-histogram.libsonnet

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,59 @@ test.new(std.thisFile)
258258
}
259259
)
260260
)
261+
+ test.case.new(
262+
name='showClassicHistogramQuery defaults',
263+
test=test.expect.eq(
264+
actual=utils.showClassicHistogramQuery({ classic: 'foo' }),
265+
expected='(foo) and on() (vector($latency_metrics) == 1)',
266+
)
267+
)
268+
+ test.case.new(
269+
name='showClassicHistogramQuery other variable',
270+
test=test.expect.eq(
271+
actual=utils.showClassicHistogramQuery({ classic: 'foo' }, dashboard_variable='my_var'),
272+
expected='(foo) and on() (vector($my_var) == 1)',
273+
)
274+
)
275+
+ test.case.new(
276+
name='showClassicHistogramQuery disable',
277+
test=test.expect.eq(
278+
actual=utils.showClassicHistogramQuery({ classic: 'foo' }, disable=true),
279+
expected='(foo) and on() (vector(1) == 1)',
280+
)
281+
)
282+
+ test.case.new(
283+
name='showClassicHistogramQuery disable ignore dashboard variable',
284+
test=test.expect.eq(
285+
actual=utils.showClassicHistogramQuery({ classic: 'foo' }, dashboard_variable='my_var', disable=true),
286+
expected='(foo) and on() (vector(1) == 1)',
287+
)
288+
)
289+
+ test.case.new(
290+
name='showNativeHistogramQuery defaults',
291+
test=test.expect.eq(
292+
actual=utils.showNativeHistogramQuery({ native: 'foo' }),
293+
expected='(foo) and on() (vector($latency_metrics) == -1)',
294+
)
295+
)
296+
+ test.case.new(
297+
name='showNativeHistogramQuery other variable',
298+
test=test.expect.eq(
299+
actual=utils.showNativeHistogramQuery({ native: 'foo' }, dashboard_variable='my_var'),
300+
expected='(foo) and on() (vector($my_var) == -1)',
301+
)
302+
)
303+
+ test.case.new(
304+
name='showNativeHistogramQuery disable',
305+
test=test.expect.eq(
306+
actual=utils.showNativeHistogramQuery({ native: 'foo' }, disable=true),
307+
expected='(foo) and on() (vector(1) == -1)',
308+
)
309+
)
310+
+ test.case.new(
311+
name='showNativeHistogramQuery disable ignore dashboard variable',
312+
test=test.expect.eq(
313+
actual=utils.showNativeHistogramQuery({ native: 'foo' }, dashboard_variable='my_var', disable=true),
314+
expected='(foo) and on() (vector(1) == -1)',
315+
)
316+
)

mixin-utils/utils.libsonnet

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,17 @@ local g = import 'grafana-builder/grafana.libsonnet';
187187

188188
// showClassicHistogramQuery wraps a query defined as map {classic: q, native: q}, and compares the classic query
189189
// to dashboard variable which should take -1 or +1 as values in order to hide or show the classic query.
190-
showClassicHistogramQuery(query, dashboard_variable='latency_metrics'):: '(%s) and on() (vector($%s) == 1)' % [query.classic, dashboard_variable],
190+
// If "disable" is true it ignores the dashboard variable and shows the classic query.
191+
showClassicHistogramQuery(query, dashboard_variable='latency_metrics', disable=false)::
192+
local testAgainst = if disable then '1' else '$%s' % dashboard_variable;
193+
'(%s) and on() (vector(%s) == 1)' % [query.classic, testAgainst],
194+
191195
// showNativeHistogramQuery wraps a query defined as map {classic: q, native: q}, and compares the native query
192196
// to dashboard variable which should take -1 or +1 as values in order to show or hide the native query.
193-
showNativeHistogramQuery(query, dashboard_variable='latency_metrics'):: '(%s) and on() (vector($%s) == -1)' % [query.native, dashboard_variable],
197+
// If "disable" is true it ignores the dashboard variable and hides the native query.
198+
showNativeHistogramQuery(query, dashboard_variable='latency_metrics', disable=false)::
199+
local testAgainst = if disable then '1' else '$%s' % dashboard_variable;
200+
'(%s) and on() (vector(%s) == -1)' % [query.native, testAgainst],
194201

195202
histogramRules(metric, labels, interval='1m', record_native=false)::
196203
local vars = {

0 commit comments

Comments
 (0)