Skip to content

Commit afeacf2

Browse files
authored
Add defaultSignalSource param to set default signal source. (#1458)
1 parent b9f4106 commit afeacf2

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

common-lib/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.4.0
2+
- [Signal] Add defaultSignalSource param to set default signal source.
3+
14
# 0.3.5
25
- [Variables] Add queriesSelectorGroupOnly, queriesSelectorFilterOnly attributes
36
- [Signal] Add %(queriesSelectorGroupOnly)s, %(queriesSequeriesSelectorFilterOnly)s templates

common-lib/common/signal/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Init level:
8484
|varAdHocEnabled| Attach ad hoc labels to variables generated. |`true`,`false`|`false`|`false`|
8585
|varAdHocLabels| Limit ad hoc to the specific labels |*|`["environment"]`|`[]`|
8686
|enableLokiLogs| Add additional loki datasource to variables generation |`true`,`false`|`true`|`false`|
87+
|defaultSignalSource| Set default signal source (see below) | * | `prometheus` |
8788

8889
Signal's level:
8990

@@ -124,7 +125,7 @@ The following is supported in expressions and legends:
124125

125126
## Making signals optional
126127

127-
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.
128+
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 (and also in source defined as `defaultSignalSource`), while internal 'stub' type will be used and empty panel will be rendered instead.
128129

129130
## Example 1: From JSON
130131

common-lib/common/signal/signal.libsonnet

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';
8787
else //array
8888
type
8989
);
90+
local defaultSignalSource = std.get(signalsJson, 'defaultSignalSource', 'prometheus');
9091

9192
self.init(
9293
datasource=std.get(signalsJson, 'datasource', if std.get(signalsJson, 'enableLokiLogs', false) then 'prometheus_datasource' else 'datasource'),
@@ -96,7 +97,7 @@ local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';
9697
instanceLabels=signalsJson.instanceLabels,
9798
interval=std.get(signalsJson, 'interval', '$__rate_interval'),
9899
alertsInterval=std.get(signalsJson, 'alertsInterval', '5m'),
99-
varMetric=self.getVarMetric(signalsJson, type),
100+
varMetric=self.getVarMetric(signalsJson, type, defaultSignalSource),
100101
aggLevel=std.get(signalsJson, 'aggLevel', 'none'),
101102
aggFunction=std.get(signalsJson, 'aggFunction', 'avg'),
102103
aggKeepLabels=std.get(signalsJson, 'aggKeepLabels', []),
@@ -118,9 +119,18 @@ local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';
118119
if
119120
std.get(signalsJson.signals[s], 'optional', false) == false
120121
&&
121-
!std.objectHas(signalsJson.signals[s].sources, sourceName)
122+
(
123+
!std.objectHas(signalsJson.signals[s].sources, sourceName)
124+
&&
125+
!std.objectHas(signalsJson.signals[s].sources, defaultSignalSource)
126+
)
122127
then error 'must provide source for signal %s of type=%s' % [s, sourceName]
123-
else sourceName
128+
else (
129+
if
130+
std.objectHas(signalsJson.signals[s].sources, sourceName) then sourceName
131+
else if
132+
std.objectHas(signalsJson.signals[s].sources, defaultSignalSource) then defaultSignalSource
133+
)
124134
for sourceName in typeArr
125135
];
126136
local sourceMaps =
@@ -348,14 +358,14 @@ local xtd = import 'github.com/jsonnet-libs/xtd/main.libsonnet';
348358
),
349359
},
350360

351-
getVarMetric(signalsJson, type):
361+
getVarMetric(signalsJson, type, defaultSignalSource):
352362
if std.objectHas(signalsJson, 'discoveryMetric')
353363
then
354364
if std.type(type) == 'array' then
355365
std.prune(
356-
[std.get(signalsJson.discoveryMetric, t, null) for t in type]
366+
[std.get(signalsJson.discoveryMetric, t, std.get(signalsJson.discoveryMetric, defaultSignalSource, null)) for t in type]
357367
)
358368
else
359-
std.get(signalsJson.discoveryMetric, type, 'up')
369+
std.get(signalsJson.discoveryMetric, type, std.get(signalsJson.discoveryMetric, defaultSignalSource, 'up'))
360370
else 'up',
361371
}

0 commit comments

Comments
 (0)