Skip to content

Commit

Permalink
Add support for empty (filteringSelector='')
Browse files Browse the repository at this point in the history
  • Loading branch information
v-zhuravlev committed Nov 12, 2024
1 parent 69196e8 commit 1c68c5e
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions common-lib/common/variables/variables.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@ local utils = import '../utils.libsonnet';
varMetric='up',
enableLokiLogs=false,
customAllValue='.+',
prometheusDatasourceName='datasource',
prometheusDatasourceLabel='Data source',
prometheusDatasourceName=if enableLokiLogs then 'prometheus_datasource' else 'datasource',
prometheusDatasourceLabel=if enableLokiLogs then 'Prometheus datasource' else 'Data source',
): {
local varMetricTemplate =
if std.type(varMetric) == 'array'
then '{__name__=~"%s",%%s}' % std.join('|', std.uniq(varMetric))
local varMetricTemplate(varMetric, chainSelector) =
// check if chainSelector is not empty string (case when filtering selector is empty):
if std.type(varMetric) == 'array' && chainSelector != ''
then '{__name__=~"%s",%s}' % [std.join('|', std.uniq(varMetric)), chainSelector]
else if std.type(varMetric) == 'array' && chainSelector == ''
then '{__name__=~"%s"}' % std.join('|', std.uniq(varMetric))
else if std.type(varMetric) == 'string'
then '%s{%%s}' % varMetric
then '%s{%s}' % [varMetric, chainSelector]
else error ('varMetric must be array or string'),

local root = self,
local variablesFromLabels(groupLabels, instanceLabels, filteringSelector, multiInstance=true) =
local chainVarProto(index, chainVar) =
var.query.new(chainVar.label)
+ var.query.withDatasourceFromVariable(root.datasources.prometheus)
+ var.query.queryTypes.withLabelValues(
chainVar.label,
varMetricTemplate % [chainVar.chainSelector],
varMetricTemplate(varMetric, chainVar.chainSelector),
)
+ var.query.generalOptions.withLabel(utils.toSentenceCase(chainVar.label))
+ var.query.selectionOptions.withIncludeAll(
Expand All @@ -43,7 +47,7 @@ local utils = import '../utils.libsonnet';
asc=true,
caseInsensitive=false
);
std.mapWithIndex(chainVarProto, utils.chainLabels(groupLabels + instanceLabels, [filteringSelector])),
std.mapWithIndex(chainVarProto, utils.chainLabels(groupLabels + instanceLabels, if std.length(filteringSelector) > 0 then [filteringSelector] else [])),
datasources: {
prometheus:
var.datasource.new(prometheusDatasourceName, 'prometheus')
Expand Down

0 comments on commit 1c68c5e

Please sign in to comment.