Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mixin-utils/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ vendor jsonnetfile.lock.json: jsonnetfile.json
jb install

tests: jsonnetfile.lock.json vendor
jsonnet -J vendor/ test_*.libsonnet
jsonnet -J vendor/ ./test_native-classic-histogram.libsonnet
jsonnet -J vendor/ ./test_remove_rules.libsonnet
284 changes: 284 additions & 0 deletions mixin-utils/test/test_remove_rules.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
local utils = import '../utils.libsonnet';
local test = import 'github.com/jsonnet-libs/testonnet/main.libsonnet';

local config = {
prometheusAlerts: {
groups: [
{
name: 'group1',
rules: [
{
alert: 'alert1',
},
{
alert: 'alert2',
},
],
},
{
name: 'group2',
rules: [
{
alert: 'alert3',
},
{
alert: 'alert4',
},
],
},
],
},
prometheusRules: {
groups: [
{
name: 'group3',
rules: [
{
record: 'record1',
},
{
record: 'record2',
},
],
},
{
name: 'group4',
rules: [
{
record: 'record3',
},
{
record: 'record4',
},
],
},
],
},
};

test.new(std.thisFile)

+ test.case.new(
'removeAlertRuleGroup',
test.expect.eq(
actual=config + utils.removeAlertRuleGroup('group1'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding tests. This also shows how these functions are supposed to be used! That's very useful!

expected={
prometheusAlerts: {
groups: [
{
name: 'group2',
rules: [
{
alert: 'alert3',
},
{
alert: 'alert4',
},
],
},
],
},
prometheusRules: {
groups: [
{
name: 'group3',
rules: [
{
record: 'record1',
},
{
record: 'record2',
},
],
},
{
name: 'group4',
rules: [
{
record: 'record3',
},
{
record: 'record4',
},
],
},
],
},
}
)
)

+ test.case.new(
'removeRecordingRuleGroup',
test.expect.eq(
actual=config + utils.removeRecordingRuleGroup('group4'),
expected={
prometheusAlerts: {
groups: [
{
name: 'group1',
rules: [
{
alert: 'alert1',
},
{
alert: 'alert2',
},
],
},
{
name: 'group2',
rules: [
{
alert: 'alert3',
},
{
alert: 'alert4',
},
],
},
],
},
prometheusRules: {
groups: [
{
name: 'group3',
rules: [
{
record: 'record1',
},
{
record: 'record2',
},
],
},
],
},
}
)
)

+ test.case.new(
'removeAlertRuleGroup with groupname from recording rules (noop)',
test.expect.eq(
actual=config + utils.removeAlertRuleGroup('group4'),
expected=config
)
)
+ test.case.new(
'removeRecordingRuleGroup with groupname from alert rules (noop)',
test.expect.eq(
actual=config + utils.removeRecordingRuleGroup('group2'),
expected=config
)
)

+ test.case.new(
'removeAlerts',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran all but this test case against main. This test case represents a new feature, removeAlerts now accepts an array.

test.expect.eq(
actual=config + utils.removeAlerts(['alert1', 'alert4']),
expected={
prometheusAlerts: {
groups: [
{
name: 'group1',
rules: [
{
alert: 'alert2',
},
],
},
{
name: 'group2',
rules: [
{
alert: 'alert3',
},
],
},
],
},
prometheusRules: {
groups: [
{
name: 'group3',
rules: [
{
record: 'record1',
},
{
record: 'record2',
},
],
},
{
name: 'group4',
rules: [
{
record: 'record3',
},
{
record: 'record4',
},
],
},
],
},
}
)
)

+ test.case.new(
'removeAlerts - object (backwards compatible)',
test.expect.eq(
actual=config + utils.removeAlerts({ alert1: {}, alert4: {} }),
expected={
prometheusAlerts: {
groups: [
{
name: 'group1',
rules: [
{
alert: 'alert2',
},
],
},
{
name: 'group2',
rules: [
{
alert: 'alert3',
},
],
},
],
},
prometheusRules: {
groups: [
{
name: 'group3',
rules: [
{
record: 'record1',
},
{
record: 'record2',
},
],
},
{
name: 'group4',
rules: [
{
record: 'record3',
},
{
record: 'record4',
},
],
},
],
},
}
)
)
27 changes: 13 additions & 14 deletions mixin-utils/utils.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,16 @@ local g = import 'grafana-builder/grafana.libsonnet';
for group in groups
],

removeRuleGroup(ruleName):: {
local removeRuleGroup(rule) = if rule.name == ruleName then null else rule,
local currentRuleGroups = super.groups,
groups: std.prune(std.map(removeRuleGroup, currentRuleGroups)),
removeRuleGroup(groupName):: {
groups: std.filter(function(group) group.name != groupName, super.groups),
},

removeAlertRuleGroup(ruleName):: {
prometheusAlerts+:: $.removeRuleGroup(ruleName),
prometheusAlerts+: $.removeRuleGroup(ruleName),
Copy link
Contributor Author

@Duologic Duologic May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+: means keep visiblity as it was, so if the field was hidden upstream, then it will stay hidden. I opted for this change to avoid making the test cases more complicated.

},

removeRecordingRuleGroup(ruleName):: {
prometheusRules+:: $.removeRuleGroup(ruleName),
prometheusRules+: $.removeRuleGroup(ruleName),
},

overrideAlerts(overrides):: {
Expand All @@ -412,19 +410,20 @@ local g = import 'grafana-builder/grafana.libsonnet';
then rule + overrides[rule.alert]
else rule,
local overrideInGroup(group) = group { rules: std.map(overrideRule, super.rules) },
prometheusAlerts+:: {
prometheusAlerts+: {
groups: std.map(overrideInGroup, super.groups),
},
},

removeAlerts(alerts):: {
local removeRule(rule) =
if 'alert' in rule && std.objectHas(alerts, rule.alert)
then {}
else rule,
local removeInGroup(group) = group { rules: std.map(removeRule, super.rules) },
prometheusAlerts+:: {
groups: std.prune(std.map(removeInGroup, super.groups)),
local alertNames =
if std.isObject(alerts)
then std.objectFields(alerts)
else alerts,
local removeRule(rule) = !std.member(alertNames, std.get(rule, 'alert', '')),
local removeInGroup(group) = group { rules: std.filter(removeRule, super.rules) },
prometheusAlerts+: {
groups: std.map(removeInGroup, super.groups),
},
},
}
Loading