diff --git a/public/app/plugins/datasource/prometheus/querybuilder/components/LabelFilterItem.tsx b/public/app/plugins/datasource/prometheus/querybuilder/components/LabelFilterItem.tsx index 8492059b533..bdbe4795f9e 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/components/LabelFilterItem.tsx +++ b/public/app/plugins/datasource/prometheus/querybuilder/components/LabelFilterItem.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 []; diff --git a/public/app/plugins/datasource/prometheus/querybuilder/components/LabelFilters.test.tsx b/public/app/plugins/datasource/prometheus/querybuilder/components/LabelFilters.test.tsx index 4248b73ed46..19c9fb4cafc 100644 --- a/public/app/plugins/datasource/prometheus/querybuilder/components/LabelFilters.test.tsx +++ b/public/app/plugins/datasource/prometheus/querybuilder/components/LabelFilters.test.tsx @@ -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();