Skip to content

Commit d6c38bb

Browse files
authored
add withExprWrappersMixin (#1395)
* add withExprWrappersMixin * lint
1 parent 6f68215 commit d6c38bb

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

common-lib/common/signal/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ Workflow to generate dashboards would be as follows:
2121

2222
These functions modify one of the signal's property and then return signal back. Can be used as part of the builder pattern.
2323

24-
- withTopK(limit=25) - wraps signal expression into topk().
25-
- withOffset(offset) - adds offset modifier to the expression.
26-
- withFilteringSelectorMixin(mixin) - adds additional selector to filteringSelector used.
24+
- withTopK(limit=25) - wrap signal expression into topk().
25+
- withExprWrappersMixin(wrapper=[]) - wrap signal expression into additional function on top of existing wrappers.
26+
- withOffset(offset) - add offset modifier to the expression.
27+
- withFilteringSelectorMixin(mixin) - add additional selector to filteringSelector used.
2728

2829
### Render functions
2930

common-lib/common/signal/base.libsonnet

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';
144144
for source in this.sourceMaps
145145
],
146146
},
147+
148+
withExprWrappersMixin(wrappers=[]):
149+
this
150+
{
151+
sourceMaps:
152+
[
153+
source
154+
{
155+
exprWrappers+: [wrappers],
156+
}
157+
for source in this.sourceMaps
158+
],
159+
},
147160
//Return as grafana panel target(query+legend)
148161
asTarget(name=signalName):
149162
prometheusQuery.new(
@@ -344,6 +357,12 @@ local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';
344357
std.mapWithIndex(function(i, e) { index: i, el: e }, this.vars.aggLabels)
345358
},
346359
renameByName:
360+
// If 'Value' is still present, then rename value to signal name.
361+
// this is the case if only single query is used in the table.
362+
{
363+
Value: name,
364+
}
365+
+
347366
{
348367
[label]: utils.toSentenceCase(label)
349368
for label in this.vars.aggLabels

common-lib/common/signal/test_table.libsonnet

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,25 @@ local m1 = signal.init(
8383
pluginVersion: 'v11.0.0',
8484
targets: [{ datasource: { type: 'prometheus', uid: '${datasource}' }, expr: 'max by (job,instance) (\n rate(apiserver_request_total{job="abc",job=~"$job",instance=~"$instance"}[5m])\n)', format: 'table', instant: true, legendFormat: '{{instance}}: Requests', refId: 'API server requests' }],
8585
title: 'API server requests',
86-
transformations: [{ id: 'merge' }, { id: 'renameByRegex', options: { regex: 'Value #(.*)', renamePattern: '$1' } }, { id: 'filterFieldsByName', options: { include: { pattern: '^(?!Time).*$' } } }, { id: 'organize', options: { indexByName: { instance: 1, job: 0 }, renameByName: { instance: 'Instance', job: 'Job' } } }],
86+
transformations: [
87+
{ id: 'merge' },
88+
{ id: 'renameByRegex', options: { regex: 'Value #(.*)', renamePattern: '$1' } },
89+
{ id: 'filterFieldsByName', options: { include: { pattern: '^(?!Time).*$' } } },
90+
{
91+
id: 'organize',
92+
options:
93+
{
94+
indexByName: { instance: 1, job: 0 },
95+
renameByName:
96+
{
97+
Value: 'API server requests',
98+
instance: 'Instance',
99+
job: 'Job',
100+
},
101+
102+
},
103+
},
104+
],
87105
type: 'table',
88106
},
89107
},
@@ -108,7 +126,12 @@ local m1 = signal.init(
108126
id: 'organize',
109127
options: {
110128
indexByName: { instance: 1, job: 0 },
111-
renameByName: { instance: 'Instance', job: 'Job' },
129+
renameByName:
130+
{
131+
Value: 'API server requests',
132+
instance: 'Instance',
133+
job: 'Job',
134+
},
112135
},
113136
},
114137
],
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
local signal = import './signal.libsonnet';
2+
local test = import 'jsonnetunit/test.libsonnet';
3+
4+
local m1 = signal.init(
5+
filteringSelector=['job="abc"'],
6+
interval='5m',
7+
aggLevel='instance',
8+
aggFunction='max',
9+
).addSignal(
10+
name='API server requests',
11+
nameShort='requests',
12+
type='counter',
13+
unit='requests',
14+
description='API server calls.',
15+
sourceMaps=[
16+
{
17+
expr: 'apiserver_request_total{%(queriesSelector)s}',
18+
rangeFunction: 'rate',
19+
aggKeepLabels: [],
20+
legendCustomTemplate: null,
21+
infoLabel: null,
22+
},
23+
],
24+
)
25+
;
26+
27+
{
28+
29+
asTarget: {
30+
local raw = m1.withExprWrappersMixin(['sum(', ')']).asTarget(),
31+
testResult: test.suite({
32+
testLegend: {
33+
actual: raw.legendFormat,
34+
expect: '{{instance}}: requests',
35+
},
36+
testExpression: {
37+
actual: raw.expr,
38+
expect: 'sum(\n max by (job,instance) (\n rate(apiserver_request_total{job="abc",job=~"$job",instance=~"$instance"}[5m])\n)\n)',
39+
},
40+
}),
41+
},
42+
43+
}

0 commit comments

Comments
 (0)