Skip to content

[BUGFIX] Prometheus: escape label values in metrics finder filters - #970

Open
rida-ghreizy wants to merge 1 commit into
perses:mainfrom
rida-ghreizy:fix/metrics-finder-escape-label-values
Open

rida-ghreizy wants to merge 1 commit into
perses:mainfrom
rida-ghreizy:fix/metrics-finder-escape-label-values

Conversation

@rida-ghreizy

@rida-ghreizy rida-ghreizy commented Nov 27, 2026 •

Copy link
Copy Markdown

Description

The Prometheus metrics finder turns its label filters into PromQL with computeFilterExpr (used for the match[] of the label names/values and series requests, and for the metric overview queries):

`${filter.label}${filter.operator}"${filter.labelValues.join('|')}"`

The values are put between double quotes without escaping, so:

  • a label value containing " or \ (Windows paths, JSON fragments) gives an invalid selector, e.g. path="C:\data" (\d is not a valid escape), and the finder's requests fail;
  • a regex typed in =~ mode such as \d+ is sent as "\d+", which Prometheus rejects as an invalid escape sequence, instead of "\d+".

This PR escapes backslashes, double quotes and line breaks in the joined values, as for any PromQL double-quoted string. Values without those characters are unchanged, and regexes keep their meaning (a backslash typed in the field reaches the regex engine as one backslash).

Added types.test.ts with cases for joined regex values, several filters, and escaping. I couldn't install the monorepo here, so I ran these three tests against the transpiled types.ts with a small harness: they pass, and the escaping case fails without the fix. CI will run them with Vitest.

Checklist

  • Pull request has a descriptive title and context useful to a reviewer.
  • Pull request title follows the [<catalog_entry>] <commit message> naming convention using one of the
    following catalog_entry values: FEATURE, ENHANCEMENT, BUGFIX, BREAKINGCHANGE, DOC,IGNORE.
  • All commits have DCO signoffs.

computeFilterExpr put the label values of the metrics finder filters
between double quotes without escaping them, so a value containing a
double quote, a backslash or a line break produced an invalid PromQL
selector, and the finder's queries failed. Escape them like a PromQL
double-quoted string.

Signed-off-by: rida <ghreizyrida1@gmail.com>
@rida-ghreizy
rida-ghreizy requested a review from a team as a code owner November 27, 2026 09:39
@rida-ghreizy
rida-ghreizy requested review from Gladorme and removed request for a team November 27, 2026 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant