Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rendering AlertRulev9 json for possible use in Alerting provisioning API #568

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
36 changes: 29 additions & 7 deletions grafanalib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,18 @@ def to_json_data(self):
}


@attr.s
class DataSource(object):
uid = attr.ib(validator=instance_of(str))
type = attr.ib(validator=instance_of(str))

def to_json_data(self):
return {
'uid': self.uid,
'type': self.type,
}


@attr.s
class ConstantInput(object):
name = attr.ib()
Expand Down Expand Up @@ -1574,13 +1586,15 @@ class AlertRulev9(object):
:param timeRangeTo: Time range interpolation data finish at
:param uid: Alert UID should be unique
:param dashboard_uid: Dashboard UID that should be use for linking on alert message
:param panel_id: Panel ID that should should be use for linking on alert message
:param panel_id: Panel ID that should be use for linking on alert message

Choose a reason for hiding this comment

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

Suggested change
:param panel_id: Panel ID that should be use for linking on alert message
:param panel_id: Panel ID that should be used for linking on alert message
:param annotations: An array of additional annotations, can be used for alert message
:param labels: An array of labels, can be used with Notification Policies
:param folderUid: Folder ID that should be use for linking on alert message
:param ruleGroup: A group for alerts that are evaluated together

"""

title = attr.ib()
triggers = attr.ib(default=[], validator=is_valid_triggersv9)
annotations = attr.ib(default={}, validator=instance_of(dict))
labels = attr.ib(default={}, validator=instance_of(dict))
annotations = attr.ib(factory=dict, validator=instance_of(dict))
labels = attr.ib(factory=dict, validator=instance_of(dict))
folderUid = attr.ib(default=None, validator=attr.validators.optional(instance_of(str)))
ruleGroup = attr.ib(default=None, validator=attr.validators.optional(instance_of(str)))
Comment on lines +1596 to +1597
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add document string for these


evaluateFor = attr.ib(default=DEFAULT_ALERT_EVALUATE_FOR, validator=instance_of(str))
noDataAlertState = attr.ib(
Expand All @@ -1603,8 +1617,8 @@ class AlertRulev9(object):
timeRangeFrom = attr.ib(default=300, validator=instance_of(int))
timeRangeTo = attr.ib(default=0, validator=instance_of(int))
uid = attr.ib(default=None, validator=attr.validators.optional(instance_of(str)))
dashboard_uid = attr.ib(default="", validator=instance_of(str))
panel_id = attr.ib(default=0, validator=instance_of(int))
dashboard_uid = attr.ib(default=None, validator=attr.validators.optional(instance_of(str)))
panel_id = attr.ib(default=None, validator=attr.validators.optional(instance_of(int)))

def to_json_data(self):
data = []
Expand All @@ -1618,12 +1632,18 @@ def to_json_data(self):
"from": self.timeRangeFrom,
"to": self.timeRangeTo
},
"datasourceUid": target.datasource,
"datasourceUid": target.datasource.uid,
"model": target.to_json_data(),
}]
else:
data += [trigger.to_json_data()]

if self.panel_id:
self.annotations['__panelId__'] = str(self.panel_id)

if self.dashboard_uid:
self.annotations['__dashboardUid__'] = self.dashboard_uid

return {
"title": self.title,
"uid": self.uid,
Expand All @@ -1633,7 +1653,9 @@ def to_json_data(self):
"annotations": self.annotations,
"data": data,
"noDataState": self.noDataAlertState,
"execErrState": self.errorAlertState
"execErrState": self.errorAlertState,

Choose a reason for hiding this comment

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

Are you sure that this model is correct? According to this API: https://editor.swagger.io/?url=https://raw.githubusercontent.com/grafana/grafana/main/pkg/services/ngalert/api/tooling/post.json looks that grafana_alert object is required for grafana managed alerts 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please could you remove this part from the PR

Choose a reason for hiding this comment

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

Suggested change
"execErrState": self.errorAlertState,

"folderUid": self.folderUid,
"ruleGroup": self.ruleGroup,
}


Expand Down