Skip to content

Commit

Permalink
comma handling in PromQL/LoqQL queries for commonlib/logs-lib (#1372)
Browse files Browse the repository at this point in the history
* Fix trailing comma issue in logs lib when filteringSelector is ""

* Strip starting comma (and trailing too) in commonlib variables

* strip from logs-lib

* Update common-lib/common/variables/variables.libsonnet

Co-authored-by: Emily <1282515+Dasomeone@users.noreply.github.com>

---------

Co-authored-by: Emily <1282515+Dasomeone@users.noreply.github.com>
  • Loading branch information
v-zhuravlev and Dasomeone authored Dec 11, 2024
1 parent 3aa6fb6 commit 9162ba8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
13 changes: 9 additions & 4 deletions common-lib/common/variables/variables.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -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 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, ' '), ','),

local varMetricTemplate(varMetric, chainSelector) =
// check if chainSelector is not empty string (case when filtering selector is empty):
if std.type(varMetric) == 'array' && chainSelector != ''
Expand Down Expand Up @@ -57,24 +62,24 @@ 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),
])
),
queriesSelector:
std.join(
',',
std.filter(function(x) std.length(x) > 0, [
filteringSelector,
_filteringSelector,
utils.labelsToPromQLSelector(groupLabels + instanceLabels),
])
),
Expand Down
18 changes: 12 additions & 6 deletions logs-lib/logs/variables.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand All @@ -34,7 +37,7 @@ function(
;
[
chainVarProto(chainVar)
for chainVar in utils.chainLabels(labels, [filterSelector])
for chainVar in utils.chainLabels(labels, [_filteringSelector])
],

datasource:
Expand All @@ -48,12 +51,15 @@ function(

toArray:
[self.datasource]
+ variablesFromLabels(labels, filterSelector)
+ variablesFromLabels(labels, _filteringSelector)
+ [self.regex_search],

queriesSelector:
'%s,%s' % [
filterSelector,
utils.labelsToPromQLSelector(labels),
],
std.join(
',',
std.filter(function(x) std.length(x) > 0, [
_filteringSelector,
utils.labelsToPromQLSelector(labels),
])
),
}

0 comments on commit 9162ba8

Please sign in to comment.