Skip to content

Commit b843f6e

Browse files
authored
Fix optional signals (#1403)
1 parent 6bd3759 commit b843f6e

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

common-lib/CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
# 0.3.1
2+
- [Signal] Fix optional signals (type=stub).
3+
- [Signal] Add optional signals documentation.
4+
15
# 0.3.0
26

3-
- [Signal] add new signal `log`.
4-
- [Signal] add enableLokiLogs=true|false to signals init.
7+
- [Signal] Add new signal `log`.
8+
- [Signal] Add enableLokiLogs=true|false to signals init.
59
- [Signal] `withExprWrappersMixin(offset)` - wrap signal expression into additional function on top of existing wrappers.
610

711

common-lib/common/signal/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Signal's level:
9191
|name|Signal's name. Used to populate panel's titles. |*|CPU utilization|-|
9292
|nameShort|Signal's short name. Used to populate panel's legends and column names. |*|CPU|-|
9393
|type|Signal's type. Depending on the type, some opinionated autotransformations would happen with queries, units. |gauge,counter,histogram,info,raw|gauge|-|
94+
|optional| Set this signal optional.| true,false | false | false|
9495
|unit| Signal's units. |*|bytes|``|
9596
|description| Signal's description. Used to populate panel's description. |*|CPU usage time in percent.|``|
9697
|sourceMaps[].expr| Signal's BASE expression in simplest form. Simplified jsonnet templating is supported (see below). Depending on signal's type(not `raw`) could autotransform to different form. |*|network_bytes_received_total{%(queriesSelector)s}|-|
@@ -117,6 +118,9 @@ The following is supported in expressions and legends:
117118
- `%(interval)s` - expands to `interval` value
118119
- `%(alertsInterval)s` - expands to `interval` value
119120

121+
## Making signals optional
122+
123+
When defining signals from multiple sources, you can make some of the signals optional. In this case, rendering will not throw a validation error that signal is missing for the specific source, while internal 'stub' type will be used and empty panel will be rendered instead.
120124

121125
## Example 1: From JSON
122126

common-lib/common/signal/signal.libsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ local stub = import './stub.libsonnet';
152152
else
153153
super.addSignal(
154154
name=std.get(signalsJson.signals[s], 'name', error 'Must provide name'),
155+
nameShort=std.get(signalsJson.signals[s], 'nameShort', name),
155156
type='stub',
156157
description=std.get(signalsJson.signals[s], 'description', ''),
157158
))

common-lib/common/signal/stub.libsonnet

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
local g = import '../g.libsonnet';
22
local utils = import '../utils.libsonnet';
33
local signalUtils = import './utils.libsonnet';
4-
54
{
65
new(
76
signalName,
87
type,
98
): {
9+
local this = self,
1010

11+
withTopK(limit): this,
12+
withOffset(offset): this,
13+
withFilteringSelectorMixin(mixin): this,
14+
withExprWrappersMixin(wrappers=[]): this,
15+
asOverride(name=signalName, override='byQuery', format='time_series'): {},
1116
asTarget():: {},
1217
asTableTarget():: {},
1318

@@ -31,9 +36,13 @@ local signalUtils = import './utils.libsonnet';
3136
asTable(name=signalName, format)::
3237
self.asTimeSeries(),
3338

39+
asStatusHistory(name=signalName)::
40+
self.asTimeSeries(),
41+
3442
//Return as timeSeriesPanel
3543
asGauge(name=signalName)::
3644
self.asTimeSeries(),
45+
3746
asTableColumn(override, format):: {},
3847
},
3948

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
local signal = import './signal.libsonnet';
2+
local test = import 'jsonnetunit/test.libsonnet';
3+
4+
local m1 = signal.init(
5+
filteringSelector=['job="abc"'],
6+
).addSignal(
7+
name='Go version',
8+
nameShort='Version',
9+
type='stub',
10+
aggLevel='instance',
11+
description='Go version.',
12+
sourceMaps=[
13+
{
14+
expr: 'go_info{%(queriesSelector)s}',
15+
infoLabel: 'version',
16+
legendCustomTemplate: null,
17+
aggKeepLabels: [],
18+
},
19+
]
20+
);
21+
22+
{
23+
24+
asStat:
25+
{
26+
local raw = m1.asStat(),
27+
testResult: test.suite({
28+
testTStitle: {
29+
actual: raw.title,
30+
expect: '', // empty title for stub panels
31+
},
32+
testType: {
33+
actual: raw.type,
34+
expect: 'text',
35+
},
36+
testTSversion: {
37+
actual: raw.pluginVersion,
38+
expect: 'v11.0.0',
39+
},
40+
testTSUid: {
41+
actual: raw.datasource,
42+
expect: {
43+
uid: '-- Mixed --',
44+
type: 'datasource',
45+
},
46+
},
47+
}),
48+
},
49+
asTableTarget:
50+
{
51+
local raw = m1.asTableTarget(),
52+
testResult: test.suite({
53+
testTStitle: {
54+
actual: raw,
55+
expect: {},
56+
},
57+
}),
58+
},
59+
60+
}

0 commit comments

Comments
 (0)