diff --git a/public/app/features/alerting/unified/utils/alertmanager.test.ts b/public/app/features/alerting/unified/utils/alertmanager.test.ts index c6372994465..b45212275a7 100644 --- a/public/app/features/alerting/unified/utils/alertmanager.test.ts +++ b/public/app/features/alerting/unified/utils/alertmanager.test.ts @@ -1,6 +1,12 @@ import { Matcher, MatcherOperator, Route } from 'app/plugins/datasource/alertmanager/types'; import { Labels } from 'app/types/unified-alerting-dto'; -import { parseMatcher, parseMatchers, labelsMatchMatchers, removeMuteTimingFromRoute } from './alertmanager'; +import { + parseMatcher, + parseMatchers, + labelsMatchMatchers, + removeMuteTimingFromRoute, + matchersToString, +} from './alertmanager'; describe('Alertmanager utils', () => { describe('parseMatcher', () => { @@ -162,4 +168,19 @@ describe('Alertmanager utils', () => { }); }); }); + + describe('matchersToString', () => { + it('Should create a comma-separated list of labels and values wrapped into curly brackets', () => { + const matchers: Matcher[] = [ + { name: 'severity', value: 'critical', isEqual: true, isRegex: false }, + { name: 'resource', value: 'cpu', isEqual: true, isRegex: true }, + { name: 'rule_uid', value: '2Otf8canzz', isEqual: false, isRegex: false }, + { name: 'cluster', value: 'prom', isEqual: false, isRegex: true }, + ]; + + const matchersString = matchersToString(matchers); + + expect(matchersString).toBe('{severity="critical",resource=~"cpu",rule_uid!="2Otf8canzz",cluster!~"prom"}'); + }); + }); }); diff --git a/public/app/features/alerting/unified/utils/alertmanager.ts b/public/app/features/alerting/unified/utils/alertmanager.ts index 72b6d101695..4e93b0824a4 100644 --- a/public/app/features/alerting/unified/utils/alertmanager.ts +++ b/public/app/features/alerting/unified/utils/alertmanager.ts @@ -109,7 +109,7 @@ export function matchersToString(matchers: Matcher[]) { const combinedMatchers = matcherFields.reduce((acc, current) => { const currentMatcherString = `${current.name}${current.operator}"${current.value}"`; - return acc ? `${acc},${currentMatcherString}"` : currentMatcherString; + return acc ? `${acc},${currentMatcherString}` : currentMatcherString; }, ''); return `{${combinedMatchers}}`;