Prometheus: query builder, handle regex in parentheses for label filters value (#78238)

handle regex in parentheses for label filter value
pull/78334/head^2
Brendan O'Handley 2 years ago committed by GitHub
parent e5c2e123ef
commit 13d67be0a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      public/app/plugins/datasource/prometheus/querybuilder/components/LabelFilterItem.tsx
  2. 12
      public/app/plugins/datasource/prometheus/querybuilder/components/LabelFilters.test.tsx

@ -52,9 +52,17 @@ export function LabelFilterItem({
const getSelectOptionsFromString = (item?: string): string[] => {
if (item) {
const regExp = /\(([^)]+)\)/;
const matches = item?.match(regExp);
if (matches && matches[0].indexOf('|') > 0) {
return [item];
}
if (item.indexOf('|') > 0) {
return item.split('|');
}
return [item];
}
return [];

@ -90,6 +90,18 @@ describe('LabelFilters', () => {
expect(getAddButton()).toBeInTheDocument();
});
it('does split regex in the middle of a label value when the value contains the char |', () => {
setup({ labelsFilters: [{ label: 'foo', op: '=~', value: 'boop|par' }] });
expect(screen.getByText('boop')).toBeInTheDocument();
expect(screen.getByText('par')).toBeInTheDocument();
});
it('does not split regex in between parentheses inside of a label value that contains the char |', () => {
setup({ labelsFilters: [{ label: 'foo', op: '=~', value: '(b|p)ar' }] });
expect(screen.getByText('(b|p)ar')).toBeInTheDocument();
});
it('shows error when filter with empty strings and label filter is required', async () => {
setup({ labelsFilters: [{ label: '', op: '=', value: '' }], labelFilterRequired: true });
expect(screen.getByText(MISSING_LABEL_FILTER_ERROR_MESSAGE)).toBeInTheDocument();

Loading…
Cancel
Save