Prometheus: Export interpolateQueryExpr for metrics drilldown links (#109713)
export interpolateQueryExpr for metrics drilldown links
This commit is contained in:
@@ -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') {
|
||||
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user