|
|
|
@ -41,29 +41,44 @@ export const findAlertInstancesWithMatchers = ( |
|
|
|
|
instances: Alert[], |
|
|
|
|
matchers: MatcherFieldValue[] |
|
|
|
|
): MatchedInstance[] => { |
|
|
|
|
const hasMatcher = (instance: Alert, matcher: MatcherFieldValue) => { |
|
|
|
|
const anchorRegex = (regexpString: string): RegExp => { |
|
|
|
|
// Silence matchers are always fully anchored in the Alertmanager: https://github.com/prometheus/alertmanager/pull/748
|
|
|
|
|
if (!regexpString.startsWith('^')) { |
|
|
|
|
regexpString = '^' + regexpString; |
|
|
|
|
} |
|
|
|
|
if (!regexpString.endsWith('$')) { |
|
|
|
|
regexpString = regexpString + '$'; |
|
|
|
|
} |
|
|
|
|
return new RegExp(regexpString); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const matchesInstance = (instance: Alert, matcher: MatcherFieldValue) => { |
|
|
|
|
return Object.entries(instance.labels).some(([key, value]) => { |
|
|
|
|
if (!matcher.name || !matcher.value) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
if (matcher.operator === MatcherOperator.equal) { |
|
|
|
|
return matcher.name === key && matcher.value === value; |
|
|
|
|
} |
|
|
|
|
if (matcher.operator === MatcherOperator.notEqual) { |
|
|
|
|
return matcher.name === key && matcher.value !== value; |
|
|
|
|
} |
|
|
|
|
if (matcher.operator === MatcherOperator.regex) { |
|
|
|
|
return matcher.name === key && matcher.value.match(value); |
|
|
|
|
if (matcher.name !== key) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
if (matcher.operator === MatcherOperator.notRegex) { |
|
|
|
|
return matcher.name === key && !matcher.value.match(value); |
|
|
|
|
switch (matcher.operator) { |
|
|
|
|
case MatcherOperator.equal: |
|
|
|
|
return matcher.value === value; |
|
|
|
|
case MatcherOperator.notEqual: |
|
|
|
|
return matcher.value !== value; |
|
|
|
|
case MatcherOperator.regex: |
|
|
|
|
const regex = anchorRegex(matcher.value); |
|
|
|
|
return regex.test(value); |
|
|
|
|
case MatcherOperator.notRegex: |
|
|
|
|
const negregex = anchorRegex(matcher.value); |
|
|
|
|
return !negregex.test(value); |
|
|
|
|
default: |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const filteredInstances = instances.filter((instance) => { |
|
|
|
|
return matchers.every((matcher) => hasMatcher(instance, matcher)); |
|
|
|
|
return matchers.every((matcher) => matchesInstance(instance, matcher)); |
|
|
|
|
}); |
|
|
|
|
const mappedInstances = filteredInstances.map((instance) => ({ |
|
|
|
|
id: `${instance.activeAt}-${instance.value}`, |
|
|
|
|