File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff 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 },
You can’t perform that action at this time.
0 commit comments