-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
post_process.py
1548 lines (1302 loc) · 57.9 KB
/
post_process.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import logging
import uuid
from collections.abc import MutableMapping, Sequence
from datetime import datetime, timedelta
from time import time
from typing import TYPE_CHECKING, Any, TypedDict
import sentry_sdk
from django.conf import settings
from django.db.models.signals import post_save
from django.utils import timezone
from google.api_core.exceptions import ServiceUnavailable
from sentry import features, projectoptions
from sentry.exceptions import PluginError
from sentry.issues.grouptype import GroupCategory
from sentry.issues.issue_occurrence import IssueOccurrence
from sentry.killswitches import killswitch_matches_context
from sentry.replays.lib.event_linking import transform_event_for_linking_payload
from sentry.replays.lib.kafka import initialize_replays_publisher
from sentry.sentry_metrics.client import generic_metrics_backend
from sentry.sentry_metrics.use_case_id_registry import UseCaseID
from sentry.signals import event_processed, issue_unignored, transaction_processed
from sentry.silo.base import SiloMode
from sentry.tasks.base import instrumented_task
from sentry.types.group import GroupSubStatus
from sentry.utils import json, metrics
from sentry.utils.cache import cache
from sentry.utils.event_frames import get_sdk_name
from sentry.utils.locking import UnableToAcquireLock
from sentry.utils.locking.backends import LockBackend
from sentry.utils.locking.manager import LockManager
from sentry.utils.retries import ConditionalRetryPolicy, exponential_delay
from sentry.utils.safe import get_path, safe_execute
from sentry.utils.sdk import bind_organization_context, set_current_event_project
from sentry.utils.sdk_crashes.sdk_crash_detection_config import build_sdk_crash_detection_configs
from sentry.utils.services import build_instance_from_options_of_type
if TYPE_CHECKING:
from sentry.eventstore.models import Event, GroupEvent
from sentry.eventstream.base import GroupState
from sentry.models.group import Group
from sentry.models.project import Project
from sentry.models.team import Team
from sentry.ownership.grammar import Rule
from sentry.services.hybrid_cloud.user import RpcUser
logger = logging.getLogger(__name__)
locks = LockManager(
build_instance_from_options_of_type(
LockBackend, settings.SENTRY_POST_PROCESS_LOCKS_BACKEND_OPTIONS
)
)
ISSUE_OWNERS_PER_PROJECT_PER_MIN_RATELIMIT = 50
HIGHER_ISSUE_OWNERS_PER_PROJECT_PER_MIN_RATELIMIT = 200
class PostProcessJob(TypedDict, total=False):
event: GroupEvent
group_state: GroupState
is_reprocessed: bool
has_reappeared: bool
has_alert: bool
has_escalated: bool
def _get_service_hooks(project_id):
from sentry.models.servicehook import ServiceHook
cache_key = f"servicehooks:1:{project_id}"
result = cache.get(cache_key)
if result is None:
hooks = ServiceHook.objects.filter(servicehookproject__project_id=project_id)
result = [(h.id, h.events) for h in hooks]
cache.set(cache_key, result, 60)
return result
def _should_send_error_created_hooks(project):
from sentry.models.organization import Organization
from sentry.models.servicehook import ServiceHook
cache_key = f"servicehooks-error-created:1:{project.id}"
result = cache.get(cache_key)
if result is None:
org = Organization.objects.get_from_cache(id=project.organization_id)
if not features.has("organizations:integrations-event-hooks", organization=org):
cache.set(cache_key, 0, 60)
return False
result = (
ServiceHook.objects.filter(organization_id=org.id)
.extra(where=["events @> '{error.created}'"])
.exists()
)
cache_value = 1 if result else 0
cache.set(cache_key, cache_value, 60)
return result
def should_write_event_stats(event: Event | GroupEvent):
# For now, we only want to write these stats for error events. If we start writing them for
# other event types we'll throw off existing stats and potentially cause various alerts to fire.
# We might decide to write these stats for other event types later, either under different keys
# or with differentiating tags.
return (
event.group
and event.group.issue_category == GroupCategory.ERROR
and event.group.platform is not None
)
def format_event_platform(event: Event | GroupEvent):
if not event.group:
logger.error(
"Group not found on event during formatting", extra={"event_id": event.event_id}
)
return
if not event.group.platform:
logger.error(
"Platform not found on group during formatting",
extra={"event_id": event.event_id, "group_id": event.group.id},
)
return
platform = event.group.platform
return platform.split("-", 1)[0].split("_", 1)[0]
def _capture_event_stats(event: Event) -> None:
if not should_write_event_stats(event):
return
platform = format_event_platform(event)
tags = {"platform": platform}
metrics.incr("events.processed", tags={"platform": platform}, skip_internal=False)
metrics.incr(f"events.processed.{platform}", skip_internal=False)
metrics.distribution("events.size.data", event.size, tags=tags, unit="byte")
def _update_escalating_metrics(event: Event) -> None:
"""
Update metrics for escalating issues when an event is processed.
"""
generic_metrics_backend.counter(
UseCaseID.ESCALATING_ISSUES,
org_id=event.project.organization_id,
project_id=event.project.id,
metric_name="event_ingested",
value=1,
tags={"group": str(event.group_id)},
unit=None,
)
def _capture_group_stats(job: PostProcessJob) -> None:
event = job["event"]
if not job["group_state"]["is_new"] or not should_write_event_stats(event):
return
if not event.group:
logger.error(
"Group not found on event while capturing group stats",
extra={"event_id": event.event_id},
)
return
platform = format_event_platform(event)
metrics.incr("events.unique", tags={"platform": platform}, skip_internal=False)
def should_issue_owners_ratelimit(project_id: int, group_id: int, organization_id: int | None):
"""
Make sure that we do not accept more groups than the enforced_limit at the project level.
"""
from sentry.models.organization import Organization
enforced_limit = ISSUE_OWNERS_PER_PROJECT_PER_MIN_RATELIMIT
organization = Organization.objects.get_from_cache(id=organization_id)
if features.has("organizations:increased-issue-owners-rate-limit", organization=organization):
enforced_limit = HIGHER_ISSUE_OWNERS_PER_PROJECT_PER_MIN_RATELIMIT
cache_key = f"issue_owner_assignment_ratelimiter:{project_id}"
data = cache.get(cache_key)
if data is None:
groups = {group_id}
window_start = datetime.now()
cache.set(cache_key, (groups, window_start), 60)
else:
groups = set(data[0])
groups.add(group_id)
window_start = data[1]
timeout = max(60 - (datetime.now() - window_start).total_seconds(), 0)
cache.set(cache_key, (groups, window_start), timeout)
return len(groups) > enforced_limit
def handle_owner_assignment(job):
if job["is_reprocessed"]:
return
with sentry_sdk.start_span(op="tasks.post_process_group.handle_owner_assignment"):
try:
from sentry.models.groupowner import (
ASSIGNEE_DOES_NOT_EXIST_DURATION,
ASSIGNEE_EXISTS_DURATION,
ASSIGNEE_EXISTS_KEY,
ISSUE_OWNERS_DEBOUNCE_DURATION,
ISSUE_OWNERS_DEBOUNCE_KEY,
)
from sentry.models.projectownership import ProjectOwnership
event = job["event"]
project, group = event.project, event.group
# We want to debounce owner assignment when:
# - GroupOwner of type Ownership Rule || CodeOwner exist with TTL 1 day
# - we tried to calculate and could not find issue owners with TTL 1 day
# - an Assignee has been set with TTL of infinite
with metrics.timer("post_process.handle_owner_assignment"):
with sentry_sdk.start_span(op="post_process.handle_owner_assignment.ratelimited"):
if should_issue_owners_ratelimit(
project_id=project.id,
group_id=group.id,
organization_id=event.project.organization_id,
):
metrics.incr("sentry.task.post_process.handle_owner_assignment.ratelimited")
return
with sentry_sdk.start_span(
op="post_process.handle_owner_assignment.cache_set_assignee"
):
# Is the issue already assigned to a team or user?
assignee_key = ASSIGNEE_EXISTS_KEY(group.id)
assignees_exists = cache.get(assignee_key)
if assignees_exists is None:
assignees_exists = group.assignee_set.exists()
# Cache for 1 day if it's assigned. We don't need to move that fast.
cache.set(
assignee_key,
assignees_exists,
(
ASSIGNEE_EXISTS_DURATION
if assignees_exists
else ASSIGNEE_DOES_NOT_EXIST_DURATION
),
)
if assignees_exists:
metrics.incr(
"sentry.task.post_process.handle_owner_assignment.assignee_exists"
)
return
with sentry_sdk.start_span(
op="post_process.handle_owner_assignment.debounce_issue_owners"
):
issue_owners_key = ISSUE_OWNERS_DEBOUNCE_KEY(group.id)
debounce_issue_owners = cache.get(issue_owners_key)
if debounce_issue_owners:
metrics.incr("sentry.tasks.post_process.handle_owner_assignment.debounce")
return
with metrics.timer("post_process.process_owner_assignments.duration"):
with sentry_sdk.start_span(
op="post_process.handle_owner_assignment.get_issue_owners"
):
if killswitch_matches_context(
"post_process.get-autoassign-owners",
{
"project_id": project.id,
},
):
# see ProjectOwnership.get_issue_owners
issue_owners: Sequence[tuple[Rule, Sequence[Team | RpcUser], str]] = []
else:
issue_owners = ProjectOwnership.get_issue_owners(project.id, event.data)
# Cache for 1 day after we calculated. We don't need to move that fast.
cache.set(
issue_owners_key,
True,
ISSUE_OWNERS_DEBOUNCE_DURATION,
)
with sentry_sdk.start_span(
op="post_process.handle_owner_assignment.handle_group_owners"
):
if issue_owners:
try:
handle_group_owners(project, group, issue_owners)
except Exception:
logger.exception("Failed to store group owners")
else:
handle_invalid_group_owners(group)
except Exception:
logger.exception("Failed to handle owner assignments")
def handle_invalid_group_owners(group):
from sentry.models.groupowner import GroupOwner, GroupOwnerType
invalid_group_owners = GroupOwner.objects.filter(
group=group,
type__in=[GroupOwnerType.OWNERSHIP_RULE.value, GroupOwnerType.CODEOWNERS.value],
)
for owner in invalid_group_owners:
owner.delete()
logger.info(
"handle_invalid_group_owners.delete_group_owner",
extra={"group": group.id, "group_owner_id": owner.id, "project": group.project_id},
)
def handle_group_owners(
project: Project,
group: Group,
issue_owners: Sequence[tuple[Rule, Sequence[Team | RpcUser], str]],
):
"""
Stores group owners generated by `ProjectOwnership.get_issue_owners` in the
`GroupOwner` model, and handles any diffing/changes of which owners we're keeping.
:return:
"""
from sentry.models.groupowner import GroupOwner, GroupOwnerType, OwnerRuleType
from sentry.models.team import Team
from sentry.models.user import User
from sentry.services.hybrid_cloud.user import RpcUser
lock = locks.get(f"groupowner-bulk:{group.id}", duration=10, name="groupowner_bulk")
logging_params = {
"group": group.id,
"project": project.id,
"organization": project.organization_id,
"issue_owners_length": len(issue_owners) if issue_owners else 0,
}
try:
logger.info("handle_group_owners.start", extra=logging_params)
with (
sentry_sdk.start_span(op="post_process.handle_group_owners"),
lock.acquire(),
):
current_group_owners = GroupOwner.objects.filter(
group=group,
type__in=[GroupOwnerType.OWNERSHIP_RULE.value, GroupOwnerType.CODEOWNERS.value],
)
new_owners: dict = {}
for rule, owners, source in issue_owners:
for owner in owners:
# Can potentially have multiple rules pointing to the same owner
if new_owners.get((type(owner), owner.id, source)):
new_owners[(type(owner), owner.id, source)].append(rule)
else:
new_owners[(type(owner), owner.id, source)] = [rule]
# Owners already in the database that we'll keep
keeping_owners = set()
for group_owner in current_group_owners:
local_logging_params = logging_params.copy()
local_logging_params["group_owner_id"] = group_owner.id
owner_rule_type = (
OwnerRuleType.CODEOWNERS.value
if group_owner.type == GroupOwnerType.CODEOWNERS.value
else OwnerRuleType.OWNERSHIP_RULE.value
)
lookup_key = (
(Team, group_owner.team_id, owner_rule_type)
if group_owner.team_id is not None
else (User, group_owner.user_id, owner_rule_type)
)
# Old groupowner assignments get deleted
lookup_key_value = None
if lookup_key not in new_owners:
group_owner.delete()
logger.info(
"handle_group_owners.delete_group_owner",
extra={**local_logging_params, "reason": "assignment_deleted"},
)
else:
lookup_key_value = new_owners.get(lookup_key)
# Old groupowner assignment from outdated rules get deleted
if (
lookup_key_value
and (group_owner.context or {}).get("rule") not in lookup_key_value
):
group_owner.delete()
logger.info(
"handle_group_owners.delete_group_owner",
extra={**local_logging_params, "reason": "outdated_rule"},
)
else:
keeping_owners.add(lookup_key)
new_group_owners = []
for key in new_owners.keys():
if key not in keeping_owners:
owner_type, owner_id, owner_source = key
rules = new_owners[key]
group_owner_type = (
GroupOwnerType.OWNERSHIP_RULE.value
if owner_source == OwnerRuleType.OWNERSHIP_RULE.value
else GroupOwnerType.CODEOWNERS.value
)
user_id = None
team_id = None
if owner_type is RpcUser:
user_id = owner_id
if owner_type is Team:
team_id = owner_id
for rule in rules:
new_group_owners.append(
GroupOwner(
group=group,
type=group_owner_type,
user_id=user_id,
team_id=team_id,
project=project,
organization=project.organization,
context={"rule": str(rule)},
)
)
if new_group_owners:
GroupOwner.objects.bulk_create(new_group_owners)
for go in new_group_owners:
post_save.send_robust(
sender=GroupOwner,
instance=go,
created=True,
)
logging_params["count"] = len(new_group_owners)
logger.info("group_owners.bulk_create", extra=logging_params)
except UnableToAcquireLock:
logger.info("handle_group_owners.lock_failed", extra=logging_params)
pass
def update_existing_attachments(job):
"""
Attaches the group_id to all event attachments that were either:
1) ingested prior to the event via the standalone attachment endpoint.
2) part of a different group before reprocessing started.
"""
# Patch attachments that were ingested on the standalone path.
with sentry_sdk.start_span(op="tasks.post_process_group.update_existing_attachments"):
from sentry.models.eventattachment import EventAttachment
event = job["event"]
EventAttachment.objects.filter(project_id=event.project_id, event_id=event.event_id).update(
group_id=event.group_id
)
def fetch_buffered_group_stats(group):
"""
Fetches buffered increments to `times_seen` for this group and adds them to the current
`times_seen`.
"""
from sentry import buffer
from sentry.models.group import Group
result = buffer.backend.get(Group, ["times_seen"], {"id": group.id})
group.times_seen_pending = result["times_seen"]
MAX_FETCH_ATTEMPTS = 3
def should_retry_fetch(attempt: int, e: Exception) -> bool:
from sentry.issues.occurrence_consumer import EventLookupError
return not attempt > MAX_FETCH_ATTEMPTS and (
isinstance(e, ServiceUnavailable) or isinstance(e, EventLookupError)
)
fetch_retry_policy = ConditionalRetryPolicy(should_retry_fetch, exponential_delay(1.00))
def should_update_escalating_metrics(event: Event, is_transaction_event: bool) -> bool:
return (
features.has("organizations:escalating-metrics-backend", event.project.organization)
and not is_transaction_event
and event.group is not None
and event.group.issue_type.should_detect_escalation()
)
@instrumented_task(
name="sentry.tasks.post_process.post_process_group",
time_limit=120,
soft_time_limit=110,
silo_mode=SiloMode.REGION,
)
def post_process_group(
is_new,
is_regression,
is_new_group_environment,
cache_key,
group_id=None,
occurrence_id: str | None = None,
*,
project_id: int,
**kwargs,
):
"""
Fires post processing hooks for a group.
"""
from sentry.utils import snuba
with snuba.options_override({"consistent": True}):
from sentry import eventstore
from sentry.eventstore.processing import event_processing_store
from sentry.ingest.transaction_clusterer.datasource.redis import (
record_span_descriptions as record_span_descriptions_for_clustering,
)
from sentry.ingest.transaction_clusterer.datasource.redis import (
record_transaction_name as record_transaction_name_for_clustering,
)
from sentry.issues.occurrence_consumer import EventLookupError
from sentry.models.organization import Organization
from sentry.models.project import Project
from sentry.reprocessing2 import is_reprocessed_event
if occurrence_id is None:
# We use the data being present/missing in the processing store
# to ensure that we don't duplicate work should the forwarding consumers
# need to rewind history.
data = event_processing_store.get(cache_key)
if not data:
logger.info(
"post_process.skipped",
extra={"cache_key": cache_key, "reason": "missing_cache"},
)
return
with metrics.timer("tasks.post_process.delete_event_cache"):
event_processing_store.delete_by_key(cache_key)
occurrence = None
event = process_event(data, group_id)
else:
# Note: We attempt to acquire the lock here, but we don't release it and instead just
# rely on the ttl. The goal here is to make sure we only ever run post process group
# at most once per occurrence. Even though we don't use retries on the task, this is
# still necessary since the consumer that sends these might reprocess a batch.
# TODO: It might be better to instead set a value that we delete here, similar to what
# we do with `event_processing_store`. If we could do this *before* the occurrence ends
# up in Kafka (IE via the api that will sit in front of it), then we could guarantee at
# most once running of post process group.
lock = locks.get(
f"ppg:{occurrence_id}-once",
duration=600,
name="post_process_w_o",
)
try:
lock.acquire()
except Exception:
# If we fail to acquire the lock, we've already run post process group for this
# occurrence
return
occurrence = (
IssueOccurrence.fetch(occurrence_id, project_id=project_id) if project_id else None
)
if not occurrence:
logger.error(
"Failed to fetch occurrence",
extra={"occurrence_id": occurrence_id, "project_id": project_id},
)
return
# Issue platform events don't use `event_processing_store`. Fetch from eventstore
# instead.
def get_event_raise_exception() -> Event:
assert occurrence is not None
retrieved = eventstore.backend.get_event_by_id(
project_id,
occurrence.event_id,
group_id=group_id,
skip_transaction_groupevent=True,
occurrence_id=occurrence_id,
)
if retrieved is None:
raise EventLookupError(
f"failed to retrieve event(project_id={project_id}, event_id={occurrence.event_id}, group_id={group_id}) from eventstore"
)
return retrieved
event = fetch_retry_policy(get_event_raise_exception)
set_current_event_project(event.project_id)
# Re-bind Project and Org since we're reading the Event object
# from cache which may contain stale parent models.
with sentry_sdk.start_span(op="tasks.post_process_group.project_get_from_cache"):
try:
event.project = Project.objects.get_from_cache(id=event.project_id)
except Project.DoesNotExist:
# project probably got deleted while this task was sitting in the queue
return
event.project.set_cached_field_value(
"organization",
Organization.objects.get_from_cache(id=event.project.organization_id),
)
is_reprocessed = is_reprocessed_event(event.data)
sentry_sdk.set_tag("is_reprocessed", is_reprocessed)
is_transaction_event = event.get_event_type() == "transaction"
# Simplified post processing for transaction events.
# This should eventually be completely removed and transactions
# will not go through any post processing.
if is_transaction_event:
record_transaction_name_for_clustering(event.project, event.data)
record_span_descriptions_for_clustering(event.project, event.data)
with sentry_sdk.start_span(op="tasks.post_process_group.transaction_processed_signal"):
transaction_processed.send_robust(
sender=post_process_group,
project=event.project,
event=event,
)
metric_tags = {}
if group_id:
group_state: GroupState = {
"id": group_id,
"is_new": is_new,
"is_regression": is_regression,
"is_new_group_environment": is_new_group_environment,
}
group_event = update_event_group(event, group_state)
bind_organization_context(event.project.organization)
_capture_event_stats(event)
if should_update_escalating_metrics(event, is_transaction_event):
_update_escalating_metrics(event)
group_event.occurrence = occurrence
run_post_process_job(
{
"event": group_event,
"group_state": group_state,
"is_reprocessed": is_reprocessed,
"has_reappeared": bool(not group_state["is_new"]),
"has_alert": False,
"has_escalated": False,
}
)
metric_tags["occurrence_type"] = group_event.group.issue_type.slug
if not is_reprocessed and event.data.get("received"):
duration = time() - event.data["received"]
metrics.timing(
"events.time-to-post-process",
duration,
instance=event.data["platform"],
tags=metric_tags,
)
# We see occasional metrics being recorded with very old data,
# temporarily log some information about these groups to help
# investigate.
if duration and duration > 432_000: # 5 days (5*24*60*60)
logger.warning(
"tasks.post_process.old_time_to_post_process",
extra={
"group_id": group_id,
"project_id": project_id,
"duration": duration,
"received": event.data["received"],
"platform": event.data["platform"],
"reprocessing": json.dumps(
get_path(event.data, "contexts", "reprocessing")
),
"original_issue_id": json.dumps(
get_path(event.data, "contexts", "reprocessing", "original_issue_id")
),
},
)
def run_post_process_job(job: PostProcessJob) -> None:
group_event = job["event"]
issue_category = group_event.group.issue_category if group_event.group else None
issue_category_metric = issue_category.name.lower() if issue_category else None
if group_event.group and not group_event.group.issue_type.allow_post_process_group(
group_event.group.organization
):
return
if issue_category not in GROUP_CATEGORY_POST_PROCESS_PIPELINE:
# pipeline for generic issues
pipeline = GENERIC_POST_PROCESS_PIPELINE
else:
# specific pipelines for issue types
pipeline = GROUP_CATEGORY_POST_PROCESS_PIPELINE[issue_category]
for pipeline_step in pipeline:
try:
with (
metrics.timer(
"tasks.post_process.run_post_process_job.pipeline.duration",
tags={
"pipeline": pipeline_step.__name__,
"issue_category": issue_category_metric,
"is_reprocessed": job["is_reprocessed"],
},
),
sentry_sdk.start_span(op=f"tasks.post_process_group.{pipeline_step.__name__}"),
):
pipeline_step(job)
except Exception:
metrics.incr(
"sentry.tasks.post_process.post_process_group.exception",
tags={
"issue_category": issue_category_metric,
"pipeline": pipeline_step.__name__,
},
)
logger.exception(
"Failed to process pipeline step %s",
pipeline_step.__name__,
extra={"event": group_event, "group": group_event.group},
)
else:
metrics.incr(
"sentry.tasks.post_process.post_process_group.completed",
tags={
"issue_category": issue_category_metric,
"pipeline": pipeline_step.__name__,
},
)
def process_event(data: MutableMapping[str, Any], group_id: int | None) -> Event:
from sentry.eventstore.models import Event
from sentry.models.event import EventDict
event = Event(
project_id=data["project"], event_id=data["event_id"], group_id=group_id, data=data
)
# Re-bind node data to avoid renormalization. We only want to
# renormalize when loading old data from the database.
event.data = EventDict(event.data, skip_renormalization=True) # type: ignore[assignment] # python/mypy#3004
return event
def update_event_group(event: Event, group_state: GroupState) -> GroupEvent:
# NOTE: we must pass through the full Event object, and not an
# event_id since the Event object may not actually have been stored
# in the database due to sampling.
from sentry.models.group import get_group_with_redirect
# Re-bind Group since we're reading the Event object
# from cache, which may contain a stale group and project
rebound_group = get_group_with_redirect(group_state["id"])[0]
# We buffer updates to last_seen, assume it's at least >= the event datetime
rebound_group.last_seen = max(event.datetime, rebound_group.last_seen)
# We fetch buffered updates to group aggregates here and populate them on the Group. This
# helps us avoid problems with processing group ignores and alert rules that rely on these
# stats.
with sentry_sdk.start_span(op="tasks.post_process_group.fetch_buffered_group_stats"):
fetch_buffered_group_stats(rebound_group)
rebound_group.project = event.project
rebound_group.project.set_cached_field_value("organization", event.project.organization)
group_state["id"] = rebound_group.id
if event.group_id is not None:
# deprecated event.group and event.group_id usage, kept here for backwards compatibility
event.group = rebound_group
event.groups = [rebound_group]
return event.for_group(rebound_group)
def process_inbox_adds(job: PostProcessJob) -> None:
from sentry.models.group import Group, GroupStatus
from sentry.types.group import GroupSubStatus
with sentry_sdk.start_span(op="tasks.post_process_group.add_group_to_inbox"):
event = job["event"]
is_reprocessed = job["is_reprocessed"]
is_new = job["group_state"]["is_new"]
is_regression = job["group_state"]["is_regression"]
has_reappeared = job["has_reappeared"]
from sentry.models.groupinbox import GroupInboxReason, add_group_to_inbox
if not event.group:
logger.error(
"Group not found on event while processing inbox adds",
extra={"event_id": event.event_id},
)
return
if is_reprocessed and is_new:
# keep Group.status=UNRESOLVED and Group.substatus=ONGOING if its reprocessed
add_group_to_inbox(event.group, GroupInboxReason.REPROCESSED)
elif (
not is_reprocessed and not has_reappeared
): # If true, we added the .ONGOING reason already
if is_new:
updated = (
Group.objects.filter(id=event.group.id)
.exclude(substatus=GroupSubStatus.NEW)
.update(status=GroupStatus.UNRESOLVED, substatus=GroupSubStatus.NEW)
)
if updated:
event.group.status = GroupStatus.UNRESOLVED
event.group.substatus = GroupSubStatus.NEW
add_group_to_inbox(event.group, GroupInboxReason.NEW)
elif is_regression:
# we don't need to update the group since that should've already been
# handled on event ingest
event.group.status = GroupStatus.UNRESOLVED
event.group.substatus = GroupSubStatus.REGRESSED
add_group_to_inbox(event.group, GroupInboxReason.REGRESSION)
def process_snoozes(job: PostProcessJob) -> None:
"""
Set has_reappeared to True if the group is transitioning from "resolved" to "unresolved",
otherwise set to False.
"""
# we process snoozes before rules as it might create a regression
# but not if it's new because you can't immediately snooze a new group
if job["is_reprocessed"] or not job["has_reappeared"]:
return
from sentry.issues.escalating import is_escalating, manage_issue_states
from sentry.models.group import GroupStatus
from sentry.models.groupinbox import GroupInboxReason
from sentry.models.groupsnooze import GroupSnooze
from sentry.types.group import GroupSubStatus
event = job["event"]
group = event.group
if not group:
logger.error(
"Group not found on event while processing snoozes", extra={"event_id": event.event_id}
)
return
if not group.issue_type.should_detect_escalation():
return
# groups less than a day old should use the new -> escalating logic
group_age_hours = (timezone.now() - group.first_seen).total_seconds() / 3600
should_use_new_escalation_logic = (
group_age_hours < MAX_NEW_ESCALATION_AGE_HOURS
and features.has("projects:first-event-severity-new-escalation", group.project)
)
# Check if group is escalating
if (
not should_use_new_escalation_logic
and group.status == GroupStatus.IGNORED
and group.substatus == GroupSubStatus.UNTIL_ESCALATING
):
escalating, forecast = is_escalating(group)
if escalating:
manage_issue_states(
group, GroupInboxReason.ESCALATING, event, activity_data={"forecast": forecast}
)
job["has_reappeared"] = True
return
with metrics.timer("post_process.process_snoozes.duration"):
key = GroupSnooze.get_cache_key(group.id)
snooze = cache.get(key)
if snooze is None:
try:
snooze = GroupSnooze.objects.get(group=group)
except GroupSnooze.DoesNotExist:
snooze = False
# This cache is also set in post_save|delete.
cache.set(key, snooze, 3600)
if not snooze:
job["has_reappeared"] = False
return
# GroupSnooze row exists but the Group.status isn't ignored
# this shouldn't be possible, if this fires, there may be a race or bug
if snooze is not None and group.status is not GroupStatus.IGNORED:
# log a metric for now, we can potentially set the status and substatus but that might mask some other bug
metrics.incr(
"post_process.process_snoozes.mismatch_status",
tags={
"group_status": group.status,
"group_substatus": group.substatus,
},
)
snooze_condition_still_applies = snooze.is_valid(
group, test_rates=True, use_pending_data=True
)
if not snooze_condition_still_applies:
snooze_details = {
"until": snooze.until,
"count": snooze.count,
"window": snooze.window,
"user_count": snooze.user_count,
"user_window": snooze.user_window,
}
# issues snoozed with a specific time duration should be marked ONGOING when the window expires
reason = (
GroupInboxReason.ONGOING
if snooze.until is not None
else GroupInboxReason.ESCALATING
)
manage_issue_states(group, reason, event, snooze_details)
snooze.delete()
issue_unignored.send_robust(
project=group.project,
user_id=None,
group=group,
transition_type="automatic",
sender="process_snoozes",
)
job["has_reappeared"] = True
return
job["has_reappeared"] = False
return
def process_replay_link(job: PostProcessJob) -> None:
def _get_replay_id(event):
# replay ids can either come as a context, or a tag.
# right now they come as a context on non-js events,
# and javascript transaction (through DSC context)
# It comes as a tag on js errors.
# TODO: normalize this upstream in relay and javascript SDK. and eventually remove the tag
# logic.
context_replay_id = get_path(event.data, "contexts", "replay", "replay_id")
return context_replay_id or event.get_tag("replayId")
if job["is_reprocessed"]:
return
metrics.incr("post_process.process_replay_link.id_sampled")
group_event = job["event"]
replay_id = _get_replay_id(group_event)
if not replay_id:
return
# Validate the UUID.
try:
uuid.UUID(replay_id)
except (ValueError, TypeError):
return None
metrics.incr("post_process.process_replay_link.id_exists")
publisher = initialize_replays_publisher(is_async=True)
try:
kafka_payload = transform_event_for_linking_payload(replay_id, group_event)
except ValueError:
metrics.incr("post_process.process_replay_link.id_invalid")
else:
publisher.publish(
"ingest-replay-events",
json.dumps(kafka_payload),
)
def process_rules(job: PostProcessJob) -> None:
if job["is_reprocessed"]:
return
from sentry.rules.processing.processor import RuleProcessor
group_event = job["event"]
is_new = job["group_state"]["is_new"]