From 1c68c5ea2c2dbcd372a05f6ecd723ac3bd86ed4a Mon Sep 17 00:00:00 2001 From: v-zhuravlev Date: Tue, 12 Nov 2024 19:47:57 +0800 Subject: [PATCH] Add support for empty (filteringSelector='') --- .../common/variables/variables.libsonnet | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/common-lib/common/variables/variables.libsonnet b/common-lib/common/variables/variables.libsonnet index 1a8d255d7..2e57534b1 100644 --- a/common-lib/common/variables/variables.libsonnet +++ b/common-lib/common/variables/variables.libsonnet @@ -10,15 +10,19 @@ 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) = @@ -26,7 +30,7 @@ local utils = import '../utils.libsonnet'; + 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( @@ -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')