@@ -119,7 +119,7 @@ def _webhook_event_data(
119119@instrumented_task (name = "sentry.sentry_apps.tasks.sentry_apps.send_alert_webhook" , ** TASK_OPTIONS )
120120@retry_decorator
121121def send_alert_webhook (
122- rule_label : str ,
122+ rule : str ,
123123 sentry_app_id : int ,
124124 instance_id : str ,
125125 group_id : int ,
@@ -128,72 +128,79 @@ def send_alert_webhook(
128128 additional_payload : Mapping [str , Any ] | None = None ,
129129 ** kwargs : Any ,
130130):
131- with SentryAppInteractionEvent (
132- operation_type = SentryAppInteractionType .PREPARE_WEBHOOK ,
133- event_type = SentryAppEventType .EVENT_ALERT_TRIGGERED ,
134- ).capture () as lifecycle :
135- group = Group .objects .get_from_cache (id = group_id )
136- assert group , "Group must exist to get related attributes"
137- project = Project .objects .get_from_cache (id = group .project_id )
138- organization = Organization .objects .get_from_cache (id = project .organization_id )
139- extra : dict [str , int | str ] = {
140- "sentry_app_id" : sentry_app_id ,
141- "project_id" : project .id ,
142- "organization_slug" : organization .slug ,
143- "rule" : rule_label ,
144- }
145- lifecycle .add_extras (extra )
146-
147- sentry_app = app_service .get_sentry_app_by_id (id = sentry_app_id )
148- if sentry_app is None :
149- raise SentryAppSentryError (message = SentryAppWebhookFailureReason .MISSING_SENTRY_APP )
150-
151- installations = app_service .get_many (
152- filter = dict (
153- organization_id = organization .id ,
154- app_ids = [sentry_app .id ],
155- status = SentryAppInstallationStatus .INSTALLED ,
156- )
157- )
158- if not installations :
159- raise SentryAppSentryError (message = SentryAppWebhookFailureReason .MISSING_INSTALLATION )
160- (install ,) = installations
131+ group = Group .objects .get_from_cache (id = group_id )
132+ assert group , "Group must exist to get related attributes"
133+ project = Project .objects .get_from_cache (id = group .project_id )
134+ organization = Organization .objects .get_from_cache (id = project .organization_id )
135+ extra = {
136+ "sentry_app_id" : sentry_app_id ,
137+ "project_slug" : project .slug ,
138+ "organization_slug" : organization .slug ,
139+ "rule" : rule ,
140+ }
141+
142+ sentry_app = app_service .get_sentry_app_by_id (id = sentry_app_id )
143+ if sentry_app is None :
144+ logger .info ("event_alert_webhook.missing_sentry_app" , extra = extra )
145+ return
161146
162- nodedata = nodestore .backend .get (
163- BaseEvent .generate_node_id (project_id = project .id , event_id = instance_id )
147+ installations = app_service .get_many (
148+ filter = dict (
149+ organization_id = organization .id ,
150+ app_ids = [sentry_app .id ],
151+ status = SentryAppInstallationStatus .INSTALLED ,
164152 )
153+ )
154+ if not installations :
155+ logger .info ("event_alert_webhook.missing_installation" , extra = extra )
156+ return
157+ (install ,) = installations
165158
166- if not nodedata :
167- raise SentryAppSentryError (message = SentryAppWebhookFailureReason .MISSING_EVENT )
159+ nodedata = nodestore .backend .get (
160+ BaseEvent .generate_node_id (project_id = project .id , event_id = instance_id )
161+ )
168162
169- occurrence = None
170- if occurrence_id :
171- occurrence = IssueOccurrence .fetch (occurrence_id , project_id = project .id )
163+ if not nodedata :
164+ extra = {
165+ "event_id" : instance_id ,
166+ "occurrence_id" : occurrence_id ,
167+ "rule" : rule ,
168+ "sentry_app" : sentry_app .slug ,
169+ "group_id" : group_id ,
170+ }
171+ logger .info ("send_alert_event.missing_event" , extra = extra )
172+ return
172173
173- if not occurrence :
174- raise SentryAppSentryError (
175- message = SentryAppWebhookFailureReason .MISSING_ISSUE_OCCURRENCE
176- )
174+ occurrence = None
175+ if occurrence_id :
176+ occurrence = IssueOccurrence .fetch (occurrence_id , project_id = project .id )
177177
178- group_event = GroupEvent (
179- project_id = project .id ,
180- event_id = instance_id ,
181- group = group ,
182- data = nodedata ,
183- occurrence = occurrence ,
184- )
178+ if not occurrence :
179+ logger .info (
180+ "send_alert_event.missing_occurrence" ,
181+ extra = {"occurrence_id" : occurrence_id , "project_id" : project .id },
182+ )
183+ return
184+
185+ group_event = GroupEvent (
186+ project_id = project .id ,
187+ event_id = instance_id ,
188+ group = group ,
189+ data = nodedata ,
190+ occurrence = occurrence ,
191+ )
185192
186- event_context = _webhook_event_data (group_event , group .id , project .id )
193+ event_context = _webhook_event_data (group_event , group .id , project .id )
187194
188- data = {"event" : event_context , "triggered_rule" : rule_label }
195+ data = {"event" : event_context , "triggered_rule" : rule }
189196
190- # Attach extra payload to the webhook
191- if additional_payload_key and additional_payload :
192- data [additional_payload_key ] = additional_payload
197+ # Attach extra payload to the webhook
198+ if additional_payload_key and additional_payload :
199+ data [additional_payload_key ] = additional_payload
193200
194- request_data = AppPlatformEvent (
195- resource = "event_alert" , action = "triggered" , install = install , data = data
196- )
201+ request_data = AppPlatformEvent (
202+ resource = "event_alert" , action = "triggered" , install = install , data = data
203+ )
197204
198205 send_and_save_webhook_request (sentry_app , request_data )
199206
@@ -203,7 +210,7 @@ def send_alert_webhook(
203210 "alert_rule_ui_component_webhook.sent" ,
204211 organization_id = organization .id ,
205212 sentry_app_id = sentry_app_id ,
206- event = SentryAppEventType . EVENT_ALERT_TRIGGERED ,
213+ event = f" { request_data . resource } . { request_data . action } " ,
207214 )
208215
209216
@@ -491,10 +498,6 @@ def send_resource_change_webhook(
491498def notify_sentry_app (event : GroupEvent , futures : Sequence [RuleFuture ]):
492499 for f in futures :
493500 if not f .kwargs .get ("sentry_app" ):
494- logger .error (
495- "notify_sentry_app.future_missing_sentry_app" ,
496- extra = {"event" : event .as_dict (), "future" : f , "event_id" : event .event_id },
497- )
498501 continue
499502
500503 extra_kwargs : dict [str , Any ] = {
@@ -516,7 +519,7 @@ def notify_sentry_app(event: GroupEvent, futures: Sequence[RuleFuture]):
516519 instance_id = event .event_id ,
517520 group_id = event .group_id ,
518521 occurrence_id = event .occurrence_id if hasattr (event , "occurrence_id" ) else None ,
519- rule_label = f .rule .label ,
522+ rule = f .rule .label ,
520523 sentry_app_id = f .kwargs ["sentry_app" ].id ,
521524 ** extra_kwargs ,
522525 )
0 commit comments