From 13d67be0a9503ea38a2bcdb94070bb8245524059 Mon Sep 17 00:00:00 2001 From: Brendan O'Handley Date: Fri, 17 Nov 2023 21:59:47 -0500 Subject: [PATCH] Prometheus: query builder, handle regex in parentheses for label filters value (#78238) handle regex in parentheses for label filter value --- .../querybuilder/components/LabelFilterItem.tsx | 8 ++++++++ .../querybuilder/components/LabelFilters.test.tsx | 12 ++++++++++++ 2 files changed, 20 insertions(+) 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();