Skip to content

Commit b0386da

Browse files
authored
feat: add withLabels function to utils for enhanced rule labeling (#1459)
* feat: add withLabels function to utils for enhanced rule labeling * add comment
1 parent bbd9afa commit b0386da

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

mixin-utils/utils.libsonnet

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,31 @@ local g = import 'grafana-builder/grafana.libsonnet';
392392
for group in groups
393393
],
394394

395+
// withLabels adds custom labels to all alert rules in a group or groups
396+
// Parameters:
397+
// labels: A map of label names to values to add to each alert
398+
// groups: The alert groups to modify
399+
// filter_func: Optional function that returns true for alerts that should be modified
400+
withLabels(labels, groups, filter_func=null)::
401+
local defaultFilter = function(rule) true;
402+
local filterToUse = if filter_func != null then filter_func else defaultFilter;
403+
404+
std.map(
405+
function(group)
406+
group {
407+
rules: std.map(
408+
function(rule)
409+
if std.objectHas(rule, 'alert') && filterToUse(rule)
410+
then rule {
411+
labels+: labels,
412+
}
413+
else rule,
414+
group.rules
415+
),
416+
},
417+
groups
418+
),
419+
395420
removeRuleGroup(groupName):: {
396421
groups: std.filter(function(group) group.name != groupName, super.groups),
397422
},

0 commit comments

Comments
 (0)