ui: escape label values in PromQL autocomplete

Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
This commit is contained in:
Asish Kumar 2026-05-11 11:25:29 +05:30
parent cf4505c6cd
commit 7c6bab8c58
No known key found for this signature in database
GPG key ID: 0EF072B1E0BA6FBA
3 changed files with 16 additions and 2 deletions

View file

@ -1568,6 +1568,14 @@ describe('autocomplete promQL test', () => {
label: 'demo',
type: 'text',
},
{
label: '\\\\x2d',
type: 'text',
},
{
label: 'quoted\\"value',
type: 'text',
},
],
from: 25,
to: 25,

View file

@ -168,6 +168,10 @@ function arrayToCompletionResult(data: Completion[], from: number, to: number, i
} as CompletionResult;
}
function escapePromQLString(str: string): string {
return str.replace(/([\\"])/g, '\\$1');
}
// computeEndCompletePosition calculates the end position for autocompletion replacement.
// When the cursor is in the middle of a token, this ensures the entire token is replaced,
// not just the portion before the cursor. This fixes issue #15839.
@ -794,7 +798,7 @@ export class HybridComplete implements CompleteStrategy {
return result;
}
return this.prometheusClient.labelValues(context.labelName, context.metricName, context.matchers).then((labelValues: string[]) => {
return result.concat(labelValues.map((value) => ({ label: value, type: 'text' })));
return result.concat(labelValues.map((value) => ({ label: escapePromQLString(value), type: 'text' })));
});
}
}

View file

@ -1,6 +1,8 @@
{
"status": "success",
"data": [
"demo"
"demo",
"\\x2d",
"quoted\"value"
]
}