Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comma handling in PromQL/LoqQL queries for commonlib/logs-lib #1372

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
])
),
}
Loading