Prometheus: Fix ad hoc filters when using explore table column filtering (#111141)

* add support for ad hoc filtering from explore

* fix test

* remove helper function and keep check in if statement
This commit is contained in:
jcolladokuri
2025-09-16 08:48:52 -07:00
committed by GitHub
parent 571b3226ba
commit 7e13882a1a
2 changed files with 19 additions and 1 deletions
@@ -66,6 +66,15 @@ describe('addLabelToQuery()', () => {
);
});
it('should modify existing labels if the operator is different', () => {
expect(addLabelToQuery(addLabelToQuery('foo{x="yy"}', 'bar', 'baz', '!='), 'bar', 'baz', '=')).toBe(
'foo{x="yy", bar="baz"}'
);
expect(addLabelToQuery(addLabelToQuery('foo{x="yy"}', 'bar', 'baz', '='), 'bar', 'baz', '!=')).toBe(
'foo{x="yy", bar!="baz"}'
);
});
it('should not remove filters', () => {
expect(addLabelToQuery('{x="y"} |="yy"', 'bar', 'baz')).toBe('{x="y", bar="baz"} |="yy"');
expect(addLabelToQuery('{x="y"} |="yy" !~"xx"', 'bar', 'baz')).toBe('{x="y", bar="baz"} |="yy" !~"xx"');
@@ -79,8 +79,17 @@ function addFilter(
const start = query.substring(prev, match.from);
const end = isLast ? query.substring(match.to) : '';
if (!labelExists(match.query.labels, filter)) {
const labelToMatch = labelExists(match.query.labels, filter);
if (labelToMatch) {
// if label exists, check the operator, if it is different, update it.
// We don't want to add duplicate labels.
if (labelToMatch.op !== filter.op) {
match.query.labels = match.query.labels.map((label) =>
label.label === filter.label && label.value === filter.value ? filter : label
);
}
} else {
// label does not exist, add as is.
match.query.labels.push(filter);
}
const newLabels = renderQuery(match.query);