UNPKG

@grafana/alerting

Version:

Grafana Alerting Library – Build vertical integrations on top of the industry-leading alerting solution

77 lines (72 loc) 2.46 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var data = require('@grafana/data'); "use strict"; function matchLabels(matchers, labels) { const matches = matchLabelsSet(matchers, labels); const details = labels.map((_label, index) => ({ labelIndex: index, match: false, matcher: null })); matchers.forEach((matcher) => { const matchingLabelIndex = labels.findIndex((label) => isLabelMatch(matcher, label)); if (matchingLabelIndex > -1) { details[matchingLabelIndex].match = true; details[matchingLabelIndex].matcher = matcher; } }); return { matches, details }; } function isLabelMatch(matcher, label) { const [labelKey, labelValue] = label; const { label: matcherLabel, type: matcherType, value: matcherValue } = matcher; if (labelKey !== matcherLabel) { return false; } const matchFunction = OperatorFunctions[matcherType]; return matchFunction(labelValue, matcherValue); } function matchLabelsSet(matchers, labels) { for (const matcher of matchers) { if (!isLabelMatchInSet(matcher, labels)) { return false; } } return true; } function isLabelMatchInSet(matcher, labels) { const { label, type, value } = matcher; let labelValue = ""; const labelForMatcher = Object.fromEntries(labels)[label]; if (labelForMatcher) { labelValue = labelForMatcher; } const matchFunction = OperatorFunctions[type]; try { return matchFunction(labelValue, value); } catch (err) { return false; } } const OperatorFunctions = { "=": (lv, mv) => lv === mv, "!=": (lv, mv) => lv !== mv, // At the time of writing, Alertmanager compiles to another (anchored) Regular Expression, // so we should also anchor our UI matches for consistency with this behaviour // https://github.com/prometheus/alertmanager/blob/fd37ce9c95898ca68be1ab4d4529517174b73c33/pkg/labels/matcher.go#L69 "=~": (lv, mv) => { const valueWithFlagsParsed = data.parseFlags(`^(?:${mv})$`); const re = new RegExp(valueWithFlagsParsed.cleaned, valueWithFlagsParsed.flags); return re.test(lv); }, "!~": (lv, mv) => { const valueWithFlagsParsed = data.parseFlags(`^(?:${mv})$`); const re = new RegExp(valueWithFlagsParsed.cleaned, valueWithFlagsParsed.flags); return !re.test(lv); } }; exports.isLabelMatch = isLabelMatch; exports.matchLabels = matchLabels; exports.matchLabelsSet = matchLabelsSet; //# sourceMappingURL=utils.cjs.map