Skip to content

Commit

Permalink
fix: correct the queries for metrics with special characters (#140) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Loori-R authored Feb 8, 2024
1 parent 8d50ca1 commit d478ba4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## tip

* BUGFIX: correct the queries for `Label Filters` and `Metrics Browser` for metrics with special characters. See [this issue](https://github.com/VictoriaMetrics/grafana-datasource/issues/140)

## [v0.6.0](https://github.com/VictoriaMetrics/grafana-datasource/releases/tag/v0.6.0)

* FEATURE: add support metrics with special characters in query builder. See [this issue](https://github.com/VictoriaMetrics/grafana-datasource/issues/131)
Expand Down
11 changes: 9 additions & 2 deletions src/datasource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ import { WithTemplate } from "./components/WithTemplateConfig/types";
import { mergeTemplateWithQuery } from "./components/WithTemplateConfig/utils/getArrayFromTemplate";
import { ANNOTATION_QUERY_STEP_DEFAULT, DATASOURCE_TYPE } from "./consts";
import PrometheusLanguageProvider from './language_provider';
import { expandRecordingRules } from './language_utils';
import {
escapeMetricNameSpecialCharacters,
expandRecordingRules,
unescapeMetricNameSpecialCharacters
} from './language_utils';
import { renderLegendFormat } from './legend';
import PrometheusMetricFindQuery from './metric_find_query';
import { getInitHints, getQueryHints } from './query_hints';
Expand Down Expand Up @@ -1019,7 +1023,10 @@ export class PrometheusDatasource
}

interpolateString(string: string) {
return this.templateSrv.replace(string, undefined, this.interpolateQueryExpr);
const operation = string.includes("__name__")
? unescapeMetricNameSpecialCharacters
: escapeMetricNameSpecialCharacters;
return this.templateSrv.replace(operation(string), undefined, this.interpolateQueryExpr);
}

withTemplatesUpdate(withTemplates: WithTemplate[]) {
Expand Down
5 changes: 5 additions & 0 deletions src/language_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ export function escapeMetricNameSpecialCharacters(metricName: string) {
return metricName.replace(specialChars, (match) => '\\' + match);
}

export function unescapeMetricNameSpecialCharacters(escapedMetricName: string) {
const escapedSpecialChars = /\\([-+*\/%^=])/g;
return escapedMetricName.replace(escapedSpecialChars, (match, p1) => p1);
}

export enum AbstractLabelOperator {
Equal = "Equal",
NotEqual = "NotEqual",
Expand Down

0 comments on commit d478ba4

Please sign in to comment.