Skip to content

Commit

Permalink
feat: add grafana v10 AlertRule support
Browse files Browse the repository at this point in the history
  • Loading branch information
desmaraisp committed Jan 25, 2024
1 parent bc9e6e3 commit a623e61
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
41 changes: 36 additions & 5 deletions grafanalib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1620,10 +1620,8 @@ def to_json_data(self):


@attr.s
class AlertRulev9(object):
class _BaseAlertRule(object):
"""
Create a Grafana 9.x+ Alert Rule
:param title: The alert's title, must be unique per folder
:param triggers: A list of Targets and AlertConditions.
The Target specifies the query, and the AlertCondition specifies how this is used to alert.
Expand Down Expand Up @@ -1678,7 +1676,7 @@ class AlertRulev9(object):
dashboard_uid = attr.ib(default="", validator=instance_of(str))
panel_id = attr.ib(default=0, validator=instance_of(int))

def to_json_data(self):
def _render_triggers(self):
data = []

for trigger in self.triggers:
Expand All @@ -1696,6 +1694,19 @@ def to_json_data(self):
else:
data += [trigger.to_json_data()]

return data

def to_json_data(self):
pass


@attr.s
class AlertRulev9(_BaseAlertRule):
"""
Create a Grafana 9.x+ Alert Rule
"""

def to_json_data(self):
return {
"uid": self.uid,
"for": self.evaluateFor,
Expand All @@ -1704,13 +1715,33 @@ def to_json_data(self):
"grafana_alert": {
"title": self.title,
"condition": self.condition,
"data": data,
"data": self._render_triggers(),
"no_data_state": self.noDataAlertState,
"exec_err_state": self.errorAlertState,
},
}


@attr.s
class AlertRulev10(_BaseAlertRule):
"""
Create a Grafana 10.x+ Alert Rule
"""

def to_json_data(self):
return {
"uid": self.uid,
"for": self.evaluateFor,
"labels": self.labels,
"annotations": self.annotations,
"title": self.title,
"condition": self.condition,
"data": self._render_triggers(),
"no_data_state": self.noDataAlertState,
"exec_err_state": self.errorAlertState,
}


@attr.s
class AlertFileBasedProvisioning(object):
"""
Expand Down
41 changes: 41 additions & 0 deletions grafanalib/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,47 @@ def test_alertrulev9():
assert data['grafana_alert']['condition'] == condition


def test_alertrulev10():
title = "My Important Alert!"
annotations = {"summary": "this alert fires when prod is down!!!"}
labels = {"severity": "serious"}
condition = 'C'
rule = G.AlertRulev10(
title=title,
uid='alert1',
condition=condition,
triggers=[
G.Target(
expr='query',
refId='A',
datasource='Prometheus',
),
G.AlertExpression(
refId='B',
expressionType=G.EXP_TYPE_CLASSIC,
expression='A',
conditions=[
G.AlertCondition(
evaluator=G.GreaterThan(3),
operator=G.OP_AND,
reducerType=G.RTYPE_LAST
)
]
),
],
annotations=annotations,
labels=labels,
evaluateFor="3m",
)

data = rule.to_json_data()
assert data['annotations'] == annotations
assert data['labels'] == labels
assert data['for'] == "3m"
assert data['title'] == title
assert data['condition'] == condition


def test_alertexpression():
refId = 'D'
expression = 'C'
Expand Down

0 comments on commit a623e61

Please sign in to comment.