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

chore(pagerduty): support event type in routing key #2475

Merged
merged 1 commit into from
Nov 13, 2024
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
25 changes: 20 additions & 5 deletions keep/providers/pagerduty_provider/pagerduty_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,11 @@ def validate_scopes(self):
return scopes

def _build_alert(
self, title: str, alert_body: str, dedup: str
self,
title: str,
alert_body: str,
dedup: str,
event_type: typing.Literal["trigger", "acknowledge", "resolve"] = "trigger",
) -> typing.Dict[str, typing.Any]:
"""
Builds the payload for an event alert.
Expand All @@ -328,13 +332,14 @@ def _build_alert(
title: Title of alert
alert_body: UTF-8 string of custom message for alert. Shown in incident body
dedup: Any string, max 255, characters used to deduplicate alerts
event_type: The type of event to send to PagerDuty

Returns:
Dictionary of alert body for JSON serialization
"""
return {
"routing_key": self.authentication_config.routing_key,
"event_action": "trigger",
"event_action": event_type,
"dedup_key": dedup,
"payload": {
"summary": title,
Expand All @@ -346,22 +351,29 @@ def _build_alert(
},
}

def _send_alert(self, title: str, body: str, dedup: str | None = None):
def _send_alert(
self,
title: str,
body: str,
dedup: str | None = None,
event_type: typing.Literal["trigger", "acknowledge", "resolve"] = "trigger",
):
"""
Sends PagerDuty Alert

Args:
title: Title of the alert.
alert_body: UTF-8 string of custom message for alert. Shown in incident body
dedup: Any string, max 255, characters used to deduplicate alerts
event_type: The type of event to send to PagerDuty
"""
# If no dedup is given, use epoch timestamp
if dedup is None:
dedup = str(datetime.datetime.now().timestamp())

url = "https://events.pagerduty.com/v2/enqueue"

payload = self._build_alert(title, body, dedup)
payload = self._build_alert(title, body, dedup, event_type)
result = requests.post(url, json=payload)
result.raise_for_status()

Expand Down Expand Up @@ -496,6 +508,7 @@ def _notify(
service_id: str = "",
requester: str = "",
incident_id: str = "",
event_type: typing.Literal["trigger", "acknowledge", "resolve"] = "trigger",
**kwargs: dict,
):
"""
Expand All @@ -507,7 +520,9 @@ def _notify(
kwargs (dict): The providers with context
"""
if self.authentication_config.routing_key:
return self._send_alert(title, alert_body, dedup=dedup)
return self._send_alert(
title, alert_body, dedup=dedup, event_type=event_type
)
else:
return self._trigger_incident(
service_id, title, alert_body, requester, incident_id
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "keep"
version = "0.28.8"
version = "0.28.9"
description = "Alerting. for developers, by developers."
authors = ["Keep Alerting LTD"]
readme = "README.md"
Expand Down
Loading