Skip to content

fix(sentry apps): Self-heal/regenereate broken ServiceHooks #96712

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

Closed
wants to merge 5 commits into from
Closed
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
53 changes: 42 additions & 11 deletions src/sentry/sentry_apps/tasks/sentry_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,18 +751,18 @@ def send_webhooks(installation: RpcSentryAppInstallation, event: str, **kwargs:
servicehook: ServiceHook | None = _load_service_hook(
installation.organization_id, installation.id
)
lifecycle.add_extras(
{
"installation_uuid": installation.uuid,
"installation_id": installation.id,
"organization": installation.organization_id,
"sentry_app": installation.sentry_app.id,
"events": installation.sentry_app.events,
"webhook_url": installation.sentry_app.webhook_url or "",
}
)

if not servicehook:
lifecycle.add_extra("events", installation.sentry_app.events)
lifecycle.add_extras(
{
"installation_uuid": installation.uuid,
"installation_id": installation.id,
"organization": installation.organization_id,
"sentry_app": installation.sentry_app.id,
"events": installation.sentry_app.events,
"webhook_url": installation.sentry_app.webhook_url or "",
}
)
raise SentryAppSentryError(message=SentryAppWebhookFailureReason.MISSING_SERVICEHOOK)
if event not in servicehook.events:
raise SentryAppSentryError(
Expand Down Expand Up @@ -799,6 +799,37 @@ def send_webhooks(installation: RpcSentryAppInstallation, event: str, **kwargs:
)


@instrumented_task(
"sentry.sentry_apps.tasks.sentry_apps.regenerate_service_hook_for_installation",
taskworker_config=TaskworkerConfig(
Copy link
Member

Choose a reason for hiding this comment

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

just making sure we don't have to add anything extra from TASK_OPTIONS in the taskworker config

Copy link
Contributor Author

Choose a reason for hiding this comment

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

er im not sure if we need to add anything else. O I did bork the namespace though. I highkey just copied this from the other tasks

namespace=sentryapp_tasks, retry=Retry(times=3), processing_deadline_duration=60
),
**TASK_OPTIONS,
)
def regenerate_service_hook_for_installation(installation_id: int) -> None:
installation = app_service.installation_by_id(id=installation_id)
if installation is None:
logger.info(
"regenerate_service_hook_for_installation.could_not_find_installation",
extra={"installation_id": installation_id},
)
return

create_or_update_service_hooks_for_installation(
installation=installation,
events=installation.sentry_app.events,
webhook_url=installation.sentry_app.webhook_url,
)

logger.info(
"regenerate_service_hook_for_installation",
extra={
"installation_id": installation_id,
"sentry_app": installation.sentry_app.id,
},
)


@instrumented_task(
"sentry.sentry_apps.tasks.sentry_apps.create_or_update_service_hooks_for_sentry_app",
taskworker_config=TaskworkerConfig(
Expand Down
5 changes: 4 additions & 1 deletion src/sentry/utils/sentry_apps/service_hook_manager.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from sentry.sentry_apps.models.sentry_app_installation import SentryAppInstallation
from sentry.sentry_apps.services.app.model import RpcSentryAppInstallation
from sentry.sentry_apps.services.hook import hook_service


def create_or_update_service_hooks_for_installation(
installation: SentryAppInstallation, webhook_url: str | None, events: list[str]
installation: SentryAppInstallation | RpcSentryAppInstallation,
webhook_url: str | None,
events: list[str],
) -> None:
"""
This function creates or updates service hooks for a given Sentry app installation.
Expand Down
54 changes: 53 additions & 1 deletion tests/sentry/sentry_apps/tasks/test_sentry_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
installation_webhook,
notify_sentry_app,
process_resource_change_bound,
regenerate_service_hook_for_installation,
send_alert_webhook_v2,
send_webhooks,
workflow_notification,
)
from sentry.sentry_apps.utils.errors import SentryAppSentryError
from sentry.shared_integrations.exceptions import ClientError
from sentry.silo.base import SiloMode
from sentry.tasks.post_process import post_process_group
from sentry.testutils.asserts import (
assert_count_of_metric,
Expand All @@ -44,7 +46,7 @@
from sentry.testutils.helpers import with_feature
from sentry.testutils.helpers.datetime import before_now
from sentry.testutils.helpers.eventprocessing import write_event_to_cache
from sentry.testutils.silo import assume_test_silo_mode_of, control_silo_test
from sentry.testutils.silo import assume_test_silo_mode, assume_test_silo_mode_of, control_silo_test
from sentry.testutils.skips import requires_snuba
from sentry.types.activity import ActivityType
from sentry.types.rules import RuleFuture
Expand Down Expand Up @@ -1659,3 +1661,53 @@ def test_saves_error_event_id_if_in_header(self, safe_urlopen):
assert first_request["organization_id"] == self.install.organization_id
assert first_request["error_id"] == "d5111da2c28645c5889d072017e3445d"
assert first_request["project_id"] == "1"


class TestBackfillServiceHooksEvents(TestCase):
def setUp(self) -> None:
self.sentry_app = self.create_sentry_app(
name="Test App",
webhook_url="https://example.com",
organization=self.organization,
events=["issue.created", "issue.resolved", "error.created"],
)
self.install = self.create_sentry_app_installation(
organization=self.organization, slug=self.sentry_app.slug
)

def test_regenerate_service_hook_for_installation_success(self):
with assume_test_silo_mode(SiloMode.REGION):
hook = ServiceHook.objects.get(installation_id=self.install.id)
hook.events = ["issue.resolved", "error.created"]
hook.save()

with self.tasks(), assume_test_silo_mode(SiloMode.REGION):
regenerate_service_hook_for_installation(installation_id=self.install.id)

with assume_test_silo_mode(SiloMode.REGION):
hook.refresh_from_db()
assert set(hook.events) == {"issue.created", "issue.resolved", "error.created"}

def test_regenerate_service_hook_for_installation_event_not_in_app_events(self):
with self.tasks(), assume_test_silo_mode(SiloMode.REGION):
regenerate_service_hook_for_installation(installation_id=self.install.id)

with assume_test_silo_mode(SiloMode.REGION):
hook = ServiceHook.objects.get(installation_id=self.install.id)
assert set(hook.events) == {"issue.created", "issue.resolved", "error.created"}

def test_regenerate_service_hook_for_installation_with_empty_app_events(self):
with assume_test_silo_mode(SiloMode.CONTROL):
self.sentry_app.update(events=[])
assert self.sentry_app.events == []

with assume_test_silo_mode(SiloMode.REGION):
hook = ServiceHook.objects.get(installation_id=self.install.id)
assert hook.events != []

with self.tasks(), assume_test_silo_mode(SiloMode.REGION):
regenerate_service_hook_for_installation(installation_id=self.install.id)

with assume_test_silo_mode(SiloMode.REGION):
hook.refresh_from_db()
assert hook.events == []
Loading