From fbc1e5a5b6cf9f8d61758060da0220dcc553280f Mon Sep 17 00:00:00 2001 From: Vitaly Zhuravlev Date: Wed, 11 Dec 2024 09:48:23 +0000 Subject: [PATCH 1/4] Fix trailing comma issue in logs lib when filteringSelector is "" --- logs-lib/logs/variables.libsonnet | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/logs-lib/logs/variables.libsonnet b/logs-lib/logs/variables.libsonnet index de3354874..27cb331ce 100644 --- a/logs-lib/logs/variables.libsonnet +++ b/logs-lib/logs/variables.libsonnet @@ -52,8 +52,11 @@ function( + [self.regex_search], queriesSelector: - '%s,%s' % [ - filterSelector, - utils.labelsToPromQLSelector(labels), - ], + std.join( + ',', + std.filter(function(x) std.length(x) > 0, [ + filterSelector, + utils.labelsToPromQLSelector(labels), + ]) + ), } From d5342a76414e639885763b0e6498b2645570afd2 Mon Sep 17 00:00:00 2001 From: Vitaly Zhuravlev Date: Wed, 11 Dec 2024 09:54:36 +0000 Subject: [PATCH 2/4] Strip starting comma (and trailing too) in commonlib variables --- common-lib/common/variables/variables.libsonnet | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/common-lib/common/variables/variables.libsonnet b/common-lib/common/variables/variables.libsonnet index 951bd94e5..417ae5f9a 100644 --- a/common-lib/common/variables/variables.libsonnet +++ b/common-lib/common/variables/variables.libsonnet @@ -13,6 +13,11 @@ local utils = import '../utils.libsonnet'; prometheusDatasourceName=if enableLokiLogs then 'prometheus_datasource' else 'datasource', prometheusDatasourceLabel=if enableLokiLogs then 'Prometheus data source' else 'Data source', ): { + // strip trailing or starting comma if present: + // while trailing comman is accepted in PromQL expressions, starting comma is not. + // starting comma can be present in case of concatenation of empty filteringSelector with some extra selectors. + local _filteringSelector = std.stripChars(std.stripChars(filteringSelector, ' '), ','), + local varMetricTemplate(varMetric, chainSelector) = // check if chainSelector is not empty string (case when filtering selector is empty): if std.type(varMetric) == 'array' && chainSelector != '' @@ -57,16 +62,16 @@ local utils = import '../utils.libsonnet'; // Use on dashboards where multiple entities can be selected, like fleet dashboards multiInstance: [root.datasources.prometheus] - + variablesFromLabels(groupLabels, instanceLabels, filteringSelector), + + variablesFromLabels(groupLabels, instanceLabels, _filteringSelector), // Use on dashboards where only single entity can be selected singleInstance: [root.datasources.prometheus] - + variablesFromLabels(groupLabels, instanceLabels, filteringSelector, multiInstance=false), + + variablesFromLabels(groupLabels, instanceLabels, _filteringSelector, multiInstance=false), queriesSelectorAdvancedSyntax: std.join( ',', std.filter(function(x) std.length(x) > 0, [ - filteringSelector, + _filteringSelector, utils.labelsToPromQLSelectorAdvanced(groupLabels + instanceLabels), ]) ), @@ -74,7 +79,7 @@ local utils = import '../utils.libsonnet'; std.join( ',', std.filter(function(x) std.length(x) > 0, [ - filteringSelector, + _filteringSelector, utils.labelsToPromQLSelector(groupLabels + instanceLabels), ]) ), From 2a1ed758613c3ef241db851e7c505d14d9e85f31 Mon Sep 17 00:00:00 2001 From: Vitaly Zhuravlev Date: Wed, 11 Dec 2024 11:02:23 +0000 Subject: [PATCH 3/4] strip from logs-lib --- logs-lib/logs/variables.libsonnet | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/logs-lib/logs/variables.libsonnet b/logs-lib/logs/variables.libsonnet index 27cb331ce..3e1dd48a7 100644 --- a/logs-lib/logs/variables.libsonnet +++ b/logs-lib/logs/variables.libsonnet @@ -9,6 +9,9 @@ function( labels, ) { + // strip trailing or starting comma if present that are not accepted in LoqQL + // starting comma can be present in case of concatenation of empty filteringSelector with some extra selectors. + local _filteringSelector = std.stripChars(std.stripChars(filterSelector, ' '), ','), local this = self, local variablesFromLabels(labels, filterSelector) = local chainVarProto(chainVar) = @@ -34,7 +37,7 @@ function( ; [ chainVarProto(chainVar) - for chainVar in utils.chainLabels(labels, [filterSelector]) + for chainVar in utils.chainLabels(labels, [_filteringSelector]) ], datasource: @@ -48,14 +51,14 @@ function( toArray: [self.datasource] - + variablesFromLabels(labels, filterSelector) + + variablesFromLabels(labels, _filteringSelector) + [self.regex_search], queriesSelector: std.join( ',', std.filter(function(x) std.length(x) > 0, [ - filterSelector, + _filteringSelector, utils.labelsToPromQLSelector(labels), ]) ), From 556567f4bc666b437f4c994461e4072a040d2a4b Mon Sep 17 00:00:00 2001 From: v-zhuravlev Date: Wed, 11 Dec 2024 19:04:34 +0800 Subject: [PATCH 4/4] Update common-lib/common/variables/variables.libsonnet Co-authored-by: Emily <1282515+Dasomeone@users.noreply.github.com> --- common-lib/common/variables/variables.libsonnet | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common-lib/common/variables/variables.libsonnet b/common-lib/common/variables/variables.libsonnet index 417ae5f9a..a65a2bace 100644 --- a/common-lib/common/variables/variables.libsonnet +++ b/common-lib/common/variables/variables.libsonnet @@ -14,7 +14,7 @@ local utils = import '../utils.libsonnet'; prometheusDatasourceLabel=if enableLokiLogs then 'Prometheus data source' else 'Data source', ): { // strip trailing or starting comma if present: - // while trailing comman is accepted in PromQL expressions, starting comma is not. + // while trailing comma is accepted in PromQL expressions, starting comma is not. // starting comma can be present in case of concatenation of empty filteringSelector with some extra selectors. local _filteringSelector = std.stripChars(std.stripChars(filteringSelector, ' '), ','),