Skip to content

Commit 5c2b9be

Browse files
geoffg-sentryandrewshie-sentry
authored andcommitted
Organization removal analytics (#97672)
Redo of #97378 after endpoints moved to core, easier to start fresh. When performing an org removal, we write the removal event to the organization audit log. That's great if the organization is restored, but after org removal is completed we purge the organization's audit log. Finding the time of deletion and organization slugs when they're gone from the database is more difficult than necessary and this occasionally appears in customer support requests. We have existing organization_created and organization_joined events in analytics, so I'm adding an organization_removed to the mix.
1 parent fad580c commit 5c2b9be

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/sentry/analytics/events/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
from .org_auth_token_deleted import * # noqa: F401,F403
6868
from .organization_created import * # noqa: F401,F403
6969
from .organization_joined import * # noqa: F401,F403
70+
from .organization_removed import * # noqa: F401,F403
7071
from .plugin_enabled import * # noqa: F401,F403
7172
from .project_created import * # noqa: F401,F403
7273
from .project_issue_searched import * # noqa: F401,F403
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from sentry import analytics
2+
from sentry.analytics import Event, eventclass
3+
4+
5+
@eventclass("organization.removed")
6+
class OrganizationRemoved(Event):
7+
organization_id: int
8+
slug: str
9+
user_id: int | None = None
10+
deletion_request_datetime: str | None = None
11+
deletion_datetime: str | None = None
12+
13+
14+
analytics.register(OrganizationRemoved)

src/sentry/core/endpoints/organization_details.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
from django.utils.functional import cached_property
1313
from drf_spectacular.utils import OpenApiResponse, extend_schema, extend_schema_serializer
1414
from rest_framework import serializers, status
15+
from sentry_sdk import capture_exception
1516

1617
from bitfield.types import BitHandler
17-
from sentry import audit_log, features, options, roles
18+
from sentry import analytics, audit_log, features, options, roles
19+
from sentry.analytics.events.organization_removed import OrganizationRemoved
1820
from sentry.api.api_publish_status import ApiPublishStatus
1921
from sentry.api.base import ONE_DAY, region_silo_endpoint
2022
from sentry.api.bases.organization import OrganizationEndpoint
@@ -724,6 +726,19 @@ def post_org_pending_deletion(
724726
transaction_id=org_delete_response.schedule_guid,
725727
)
726728

729+
try:
730+
analytics.record(
731+
OrganizationRemoved(
732+
organization_id=updated_organization.id,
733+
slug=updated_organization.slug,
734+
user_id=request.user.id if request.user.is_authenticated else None,
735+
deletion_request_datetime=entry.datetime.isoformat(),
736+
deletion_datetime=(entry.datetime + timedelta(seconds=ONE_DAY)).isoformat(),
737+
)
738+
)
739+
except Exception as e:
740+
capture_exception(e)
741+
727742
delete_confirmation_args: DeleteConfirmationArgs = {
728743
"username": request.user.get_username(),
729744
"ip_address": entry.ip_address,

0 commit comments

Comments
 (0)