New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loki: Fix wrongly escaped label values when using LabelFilter #59812
Conversation
let value = getString(expr, node.getChild(String)); | ||
// `value` is wrapped in double quotes, so we need to remove them. As a value can contain double quotes, we can't use RegEx here. | ||
value = value.substring(1, value.length - 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matyax @ivanahuckova that was the issue which caused wrong visuals in the query builder when switching from code. Since it also messed up my testing I fixed it here directly.
@@ -35,6 +35,10 @@ export function escapeLabelValueInExactSelector(labelValue: string): string { | |||
return labelValue.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/"/g, '\\"'); | |||
} | |||
|
|||
export function unescapeLabelValue(labelValue: string): string { | |||
return labelValue.replace(/\\n/g, '\n').replace(/\\"/g, '"').replace(/\\\\/g, '\\'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we care about \n
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically I am just blindly doing the opposite as
grafana/public/app/plugins/datasource/loki/languageUtils.ts
Lines 34 to 36 in 18b2495
export function escapeLabelValueInExactSelector(labelValue: string): string { | |
return labelValue.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/"/g, '\\"'); | |
} |
I am also unsure if a label value will ever contain newlines. Guess that's a good question which can be answered in #59824.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verified that it fixes the bug 👍
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new branch
git switch --create backport-59812-to-v9.3.x origin/v9.3.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x 932349b5ab4c91df3b78f99d78d20e7c5b851032
# Push it to GitHub
git push --set-upstream origin backport-59812-to-v9.3.x
git switch main
# Remove the local backport branch
git branch -D backport-59812-to-v9.3.x Then, create a pull request where the |
* change backticks to quotes * add unescaping to `addFilterAsLabelFilter` * fix removal of wrong quotes * change unescape order (cherry picked from commit 932349b)
grafana#59876) Loki: Fix wrongly escaped label values when using LabelFilter (grafana#59812) * change backticks to quotes * add unescaping to `addFilterAsLabelFilter` * fix removal of wrong quotes * change unescape order (cherry picked from commit 932349b)
What is this feature?
As reported in #59333 using the filter-in and out buttons in Loki is currently broken when the label value contains special characters. This PR fixes that by not using backticks but quotes as identifier for a value.
Which issue(s) does this PR fix?:
Fixes #59333
Special notes for your reviewer:
{place="luna"} | logfmt
queryinstance
label containingserver\2
or ajob
label containing"grafana/data"
.