diff --git a/packages/grafana-prometheus/src/datasource.ts b/packages/grafana-prometheus/src/datasource.ts index 2239b63be54..ad66ab1504f 100644 --- a/packages/grafana-prometheus/src/datasource.ts +++ b/packages/grafana-prometheus/src/datasource.ts @@ -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) { diff --git a/packages/grafana-prometheus/src/escaping.ts b/packages/grafana-prometheus/src/escaping.ts index 01a01ec558c..bb8daadfbd8 100644 --- a/packages/grafana-prometheus/src/escaping.ts +++ b/packages/grafana-prometheus/src/escaping.ts @@ -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(value: T) { if (typeof value !== 'string') { diff --git a/packages/grafana-prometheus/src/index.ts b/packages/grafana-prometheus/src/index.ts index 1afbe8a3fa4..d0539d46591 100644 --- a/packages/grafana-prometheus/src/index.ts +++ b/packages/grafana-prometheus/src/index.ts @@ -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';