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

feat(alerts): Add analytics to opsgenie issue action #56700

Merged
merged 2 commits into from
Sep 22, 2023
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
1 change: 1 addition & 0 deletions src/sentry/integrations/opsgenie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from sentry.rules import rules

from .actions import OpsgenieNotifyTeamAction
from .analytics import * # noqa: F401,F403
from .integration import * # noqa: F401,F403

rules.add(OpsgenieNotifyTeamAction)
4 changes: 3 additions & 1 deletion src/sentry/integrations/opsgenie/actions/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def send_notification(event, futures):
)
try:
rules = [f.rule for f in futures]
resp = client.send_notification(event, rules)
resp = client.send_notification(event, rules, notification_uuid)
except ApiError as e:
logger.info(
"rule.fail.opsgenie_notification",
Expand All @@ -86,6 +86,8 @@ def send_notification(event, futures):
"team_id": team["id"],
},
)
rule = rules[0] if rules else None
self.record_notification_sent(event, team["id"], rule, notification_uuid)

key = f"opsgenie:{integration.id}:{team['id']}"
yield self.future(send_notification, key=key)
Expand Down
17 changes: 17 additions & 0 deletions src/sentry/integrations/opsgenie/analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from sentry import analytics


class OpsgenieIntegrationNotificationSent(analytics.Event):
type = "integrations.opsgenie.notification_sent"

attributes = (
analytics.Attribute("organization_id"),
analytics.Attribute("project_id"),
analytics.Attribute("category"),
analytics.Attribute("group_id"),
analytics.Attribute("notification_uuid"),
analytics.Attribute("alert_id", required=False),
)


analytics.register(OpsgenieIntegrationNotificationSent)
18 changes: 14 additions & 4 deletions src/sentry/integrations/opsgenie/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ def _get_rule_urls(self, group, rules):
rule_urls.append(organization.absolute_url(path))
return rule_urls

def _get_issue_alert_payload(self, data, rules, event: Event | GroupEvent, group: Group | None):
def _get_issue_alert_payload(
self,
data,
rules,
event: Event | GroupEvent,
group: Group | None,
notification_uuid: str | None = None,
):
payload = {
"message": event.message or event.title,
"source": "Sentry",
Expand All @@ -66,26 +73,29 @@ def _get_issue_alert_payload(self, data, rules, event: Event | GroupEvent, group
rule_urls = self._get_rule_urls(group, rules)
payload["alias"] = f"sentry: {group.id}"
payload["entity"] = group.culprit if group.culprit else ""
group_params = {"referrer": "opsgenie"}
if notification_uuid:
group_params["notification_uuid"] = notification_uuid
payload["details"] = {
"Sentry ID": str(group.id),
"Sentry Group": getattr(group, "title", group.message).encode("utf-8"),
"Project ID": group.project.slug,
"Project Name": group.project.name,
"Logger": group.logger,
"Level": group.get_level_display(),
"Issue URL": group.get_absolute_url(),
"Issue URL": group.get_absolute_url(params=group_params),
"Triggering Rules": ", ".join([rule.label for rule in rules]),
"Triggering Rule URLs": "\n".join(rule_urls),
"Release": data.release,
}
return payload

def send_notification(self, data, rules=None):
def send_notification(self, data, rules=None, notification_uuid: str | None = None):
headers = {"Authorization": "GenieKey " + self.integration_key}
if isinstance(data, (Event, GroupEvent)):
group = data.group
event = data
payload = self._get_issue_alert_payload(data, rules, event, group)
payload = self._get_issue_alert_payload(data, rules, event, group, notification_uuid)
else:
# if we're acknowledging the alert—meaning that the Sentry alert was resolved
if data.get("identifier"):
Expand Down
2 changes: 1 addition & 1 deletion tests/sentry/integrations/opsgenie/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_send_notification(self):
"Logger": "",
"Level": "warning",
"Project ID": "bar",
"Issue URL": "http://example.com/organizations/baz/issues/%s/" % group_id,
"Issue URL": f"http://example.com/organizations/baz/issues/{group_id}/?referrer=opsgenie",
"Release": event.release,
},
"message": "Hello world",
Expand Down
27 changes: 25 additions & 2 deletions tests/sentry/integrations/opsgenie/test_notify_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def setUp(self):
self.installation = self.integration.get_installation(self.organization.id)

@responses.activate
def test_applies_correctly(self):
@patch("sentry.analytics.record")
def test_applies_correctly(self, mock_record):
event = self.store_event(
data={
"message": "Hello world",
Expand All @@ -57,7 +58,10 @@ def test_applies_correctly(self):
)

rule = self.get_rule(data={"account": self.integration.id, "team": self.team1["id"]})
results = list(rule.after(event=event, state=self.get_state()))
notification_uuid = "123e4567-e89b-12d3-a456-426614174000"
results = list(
rule.after(event=event, state=self.get_state(), notification_uuid=notification_uuid)
)
assert len(results) == 1

responses.add(
Expand All @@ -73,6 +77,25 @@ def test_applies_correctly(self):
assert event.group is not None
assert data["message"] == event.message
assert data["details"]["Sentry ID"] == str(event.group.id)
mock_record.assert_called_with(
"alert.sent",
provider="opsgenie",
alert_id="",
alert_type="issue_alert",
organization_id=self.organization.id,
project_id=self.project.id,
external_id=self.team1["id"],
notification_uuid=notification_uuid,
)
mock_record.assert_any_call(
"integrations.opsgenie.notification_sent",
category="issue_alert",
organization_id=self.organization.id,
project_id=self.project.id,
group_id=event.group_id,
notification_uuid=notification_uuid,
alert_id=None,
)

def test_render_label(self):
rule = self.get_rule(data={"account": self.integration.id, "team": self.team1["id"]})
Expand Down
Loading