Skip to content

feat(alerts): Add alert.sent analytics for issue alerts #56151

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

Merged
merged 1 commit into from
Sep 13, 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
16 changes: 15 additions & 1 deletion src/sentry/notifications/notifications/digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import TYPE_CHECKING, Any, Mapping, MutableMapping, Sequence
from urllib.parse import urlencode

from sentry import features
from sentry import analytics, features
from sentry.db.models import Model
from sentry.digests import Digest
from sentry.digests.utils import (
Expand Down Expand Up @@ -229,3 +229,17 @@ def get_log_params(self, recipient: RpcActor) -> Mapping[str, Any]:
"alert_id": alert_id,
**super().get_log_params(recipient),
}

def record_notification_sent(self, recipient: RpcActor, provider: ExternalProviders) -> None:
super().record_notification_sent(recipient, provider)
log_params = self.get_log_params(recipient)
analytics.record(
"alert.sent",
organization_id=self.organization.id,
project_id=self.project.id,
provider=provider.name,
alert_id=log_params["alert_id"] if log_params["alert_id"] else "",
alert_type="issue_alert",
external_id=str(recipient.id),
notification_uuid=self.notification_uuid,
)
16 changes: 15 additions & 1 deletion src/sentry/notifications/notifications/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pytz

from sentry import features
from sentry import analytics, features
from sentry.db.models import Model
from sentry.eventstore.models import GroupEvent
from sentry.issues.grouptype import GROUP_CATEGORIES_CUSTOM_EMAIL, GroupCategory
Expand Down Expand Up @@ -294,3 +294,17 @@ def get_log_params(self, recipient: RpcActor) -> Mapping[str, Any]:
"alert_id": self.rules[0].id if self.rules else None,
**super().get_log_params(recipient),
}

def record_notification_sent(self, recipient: RpcActor, provider: ExternalProviders) -> None:
super().record_notification_sent(recipient, provider)
log_params = self.get_log_params(recipient)
analytics.record(
"alert.sent",
organization_id=self.organization.id,
project_id=self.project.id,
provider=provider.name,
alert_id=log_params["alert_id"] if log_params["alert_id"] else "",
alert_type="issue_alert",
external_id=str(recipient.id),
notification_uuid=self.notification_uuid,
)
12 changes: 11 additions & 1 deletion tests/sentry/mail/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def test_simple_notification(self, mock_record):
assert isinstance(msg.alternatives[0][0], str)
assert "my rule" in msg.alternatives[0][0]
assert "notification_uuid" in msg.body
mock_record.assert_called_with(
mock_record.assert_any_call(
"integrations.email.notification_sent",
category="issue_alert",
target_type=ANY,
Expand All @@ -206,6 +206,16 @@ def test_simple_notification(self, mock_record):
notification_uuid=ANY,
alert_id=rule.id,
)
mock_record.assert_called_with(
"alert.sent",
organization_id=self.organization.id,
project_id=self.project.id,
provider="email",
alert_id=rule.id,
alert_type="issue_alert",
external_id=ANY,
notification_uuid=ANY,
)

def test_simple_snooze(self):
"""Test that notification for alert snoozed by user is not send to that user."""
Expand Down
12 changes: 11 additions & 1 deletion tests/sentry/notifications/notifications/test_digests.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_sends_digest_to_every_member(self, mock_record):
assert "N+1 Query" in mail.outbox[0].body
assert "oh no" in mail.outbox[0].body
assert self.build_occurrence_data()["issue_title"] in mail.outbox[0].body
mock_record.assert_called_with(
mock_record.assert_any_call(
"integrations.email.notification_sent",
category="digest",
notification_uuid=ANY,
Expand All @@ -118,6 +118,16 @@ def test_sends_digest_to_every_member(self, mock_record):
group_id=None,
user_id=ANY,
)
mock_record.assert_called_with(
"alert.sent",
organization_id=self.organization.id,
project_id=self.project.id,
provider="email",
alert_id=self.rule.id,
alert_type="issue_alert",
external_id=ANY,
notification_uuid=ANY,
)

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