Prometheus: Export interpolateQueryExpr for metrics drilldown links (#109713)

export interpolateQueryExpr for metrics drilldown links
This commit is contained in:
Brendan O'Handley
2025-08-15 09:47:19 -05:00
committed by GitHub
parent 44b7d941b4
commit 79f4510260
3 changed files with 26 additions and 17 deletions
+2 -17
View File
@@ -47,7 +47,7 @@ import {
InstantQueryRefIdIndex,
SUGGESTIONS_LIMIT,
} from './constants';
import { prometheusRegularEscape, prometheusSpecialRegexEscape } from './escaping';
import { interpolateQueryExpr, prometheusRegularEscape } from './escaping';
import {
exportToAbstractQuery,
importFromAbstractQuery,
@@ -379,22 +379,7 @@ export class PrometheusDatasource
}
interpolateQueryExpr(value: string | string[] = [], variable: QueryVariableModel | CustomVariableModel) {
// if no multi or include all do not regexEscape
if (!variable.multi && !variable.includeAll) {
return prometheusRegularEscape(value);
}
if (typeof value === 'string') {
return prometheusSpecialRegexEscape(value);
}
const escapedValues = value.map((val) => prometheusSpecialRegexEscape(val));
if (escapedValues.length === 1) {
return escapedValues[0];
}
return '(' + escapedValues.join('|') + ')';
return interpolateQueryExpr(value, variable);
}
targetContainsTemplate(target: PromQuery) {
@@ -1,8 +1,31 @@
// NOTE: these two functions are similar to the escapeLabelValueIn* functions
// in language_utils.ts, but they are not exactly the same algorithm, and we found
import { QueryVariableModel, CustomVariableModel } from '@grafana/data';
import { config } from '@grafana/runtime';
export function interpolateQueryExpr(
value: string | string[] = [],
variable: QueryVariableModel | CustomVariableModel
) {
// if no multi or include all do not regexEscape
if (!variable.multi && !variable.includeAll) {
return prometheusRegularEscape(value);
}
if (typeof value === 'string') {
return prometheusSpecialRegexEscape(value);
}
const escapedValues = value.map((val) => prometheusSpecialRegexEscape(val));
if (escapedValues.length === 1) {
return escapedValues[0];
}
return '(' + escapedValues.join('|') + ')';
}
// no way to reuse one in the another or vice versa.
export function prometheusRegularEscape<T>(value: T) {
if (typeof value !== 'string') {
+1
View File
@@ -58,6 +58,7 @@ export { PrometheusDatasource } from './datasource';
// The parts
export { addLabelToQuery } from './add_label_to_query';
export { type QueryEditorMode, type PromQueryFormat, type Prometheus } from './dataquery';
export { interpolateQueryExpr } from './escaping';
export { loadResources } from './loadResources';
export { PrometheusMetricFindQuery } from './metric_find_query';
export { promqlGrammar } from './promql';