Skip to content

Commit 82f346b

Browse files
committed
feat(alerts): Add alert.sent analytics for issue alerts
1 parent 2db32ef commit 82f346b

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

src/sentry/notifications/notifications/digest.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import TYPE_CHECKING, Any, Mapping, MutableMapping, Sequence
66
from urllib.parse import urlencode
77

8-
from sentry import features
8+
from sentry import analytics, features
99
from sentry.db.models import Model
1010
from sentry.digests import Digest
1111
from sentry.digests.utils import (
@@ -229,3 +229,17 @@ def get_log_params(self, recipient: RpcActor) -> Mapping[str, Any]:
229229
"alert_id": alert_id,
230230
**super().get_log_params(recipient),
231231
}
232+
233+
def record_notification_sent(self, recipient: RpcActor, provider: ExternalProviders) -> None:
234+
super().record_notification_sent(recipient, provider)
235+
log_params = self.get_log_params(recipient)
236+
analytics.record(
237+
"alert.sent",
238+
organization_id=self.organization.id,
239+
project_id=self.project.id,
240+
provider=provider.name,
241+
alert_id=log_params["alert_id"] if log_params["alert_id"] else "",
242+
alert_type="issue_alert",
243+
external_id=str(recipient.id),
244+
notification_uuid=self.notification_uuid,
245+
)

src/sentry/notifications/notifications/rules.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pytz
99

10-
from sentry import features
10+
from sentry import analytics, features
1111
from sentry.db.models import Model
1212
from sentry.eventstore.models import GroupEvent
1313
from sentry.issues.grouptype import GROUP_CATEGORIES_CUSTOM_EMAIL, GroupCategory
@@ -294,3 +294,17 @@ def get_log_params(self, recipient: RpcActor) -> Mapping[str, Any]:
294294
"alert_id": self.rules[0].id if self.rules else None,
295295
**super().get_log_params(recipient),
296296
}
297+
298+
def record_notification_sent(self, recipient: RpcActor, provider: ExternalProviders) -> None:
299+
super().record_notification_sent(recipient, provider)
300+
log_params = self.get_log_params(recipient)
301+
analytics.record(
302+
"alert.sent",
303+
organization_id=self.organization.id,
304+
project_id=self.project.id,
305+
provider=provider.name,
306+
alert_id=log_params["alert_id"] if log_params["alert_id"] else "",
307+
alert_type="issue_alert",
308+
external_id=str(recipient.id),
309+
notification_uuid=self.notification_uuid,
310+
)

tests/sentry/mail/test_adapter.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def test_simple_notification(self, mock_record):
191191
assert isinstance(msg.alternatives[0][0], str)
192192
assert "my rule" in msg.alternatives[0][0]
193193
assert "notification_uuid" in msg.body
194-
mock_record.assert_called_with(
194+
mock_record.assert_any_call(
195195
"integrations.email.notification_sent",
196196
category="issue_alert",
197197
target_type=ANY,
@@ -206,6 +206,16 @@ def test_simple_notification(self, mock_record):
206206
notification_uuid=ANY,
207207
alert_id=rule.id,
208208
)
209+
mock_record.assert_called_with(
210+
"alert.sent",
211+
organization_id=self.organization.id,
212+
project_id=self.project.id,
213+
provider="email",
214+
alert_id=rule.id,
215+
alert_type="issue_alert",
216+
external_id=ANY,
217+
notification_uuid=ANY,
218+
)
209219

210220
def test_simple_snooze(self):
211221
"""Test that notification for alert snoozed by user is not send to that user."""

tests/sentry/notifications/notifications/test_digests.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def test_sends_digest_to_every_member(self, mock_record):
103103
assert "N+1 Query" in mail.outbox[0].body
104104
assert "oh no" in mail.outbox[0].body
105105
assert self.build_occurrence_data()["issue_title"] in mail.outbox[0].body
106-
mock_record.assert_called_with(
106+
mock_record.assert_any_call(
107107
"integrations.email.notification_sent",
108108
category="digest",
109109
notification_uuid=ANY,
@@ -118,6 +118,16 @@ def test_sends_digest_to_every_member(self, mock_record):
118118
group_id=None,
119119
user_id=ANY,
120120
)
121+
mock_record.assert_called_with(
122+
"alert.sent",
123+
organization_id=self.organization.id,
124+
project_id=self.project.id,
125+
provider="email",
126+
alert_id=self.rule.id,
127+
alert_type="issue_alert",
128+
external_id=ANY,
129+
notification_uuid=ANY,
130+
)
121131

122132
def test_sends_alert_rule_notification_to_each_member(self):
123133
"""Test that if there is only one event it is sent as a regular alert rule notification"""

0 commit comments

Comments
 (0)