|
6 | 6 | from typing import NamedTuple |
7 | 7 |
|
8 | 8 | import sentry_sdk |
| 9 | +from django.db import router, transaction |
| 10 | +from rest_framework import status |
9 | 11 |
|
10 | 12 | from sentry import options |
| 13 | +from sentry.api.exceptions import SentryAPIException |
11 | 14 | from sentry.db.models.manager.base_query_set import BaseQuerySet |
12 | 15 | from sentry.grouping.grouptype import ErrorGroupType |
| 16 | +from sentry.issues import grouptype |
13 | 17 | from sentry.issues.issue_occurrence import IssueOccurrence |
14 | 18 | from sentry.issues.producer import PayloadType, produce_occurrence_to_kafka |
| 19 | +from sentry.locks import locks |
15 | 20 | from sentry.models.group import Group |
| 21 | +from sentry.models.project import Project |
16 | 22 | from sentry.services.eventstore.models import GroupEvent |
17 | 23 | from sentry.utils import metrics |
| 24 | +from sentry.utils.locking import UnableToAcquireLock |
18 | 25 | from sentry.workflow_engine.models import DataPacket, Detector |
19 | 26 | from sentry.workflow_engine.models.detector_group import DetectorGroup |
20 | 27 | from sentry.workflow_engine.types import ( |
| 28 | + ERROR_DETECTOR_NAME, |
| 29 | + ISSUE_STREAM_DETECTOR_NAME, |
21 | 30 | DetectorEvaluationResult, |
22 | 31 | DetectorGroupKey, |
23 | 32 | WorkflowEventData, |
24 | 33 | ) |
| 34 | +from sentry.workflow_engine.typings.grouptype import IssueStreamGroupType |
25 | 35 |
|
26 | 36 | logger = logging.getLogger(__name__) |
27 | 37 |
|
| 38 | +VALID_DEFAULT_DETECTOR_TYPES = [ErrorGroupType.slug, IssueStreamGroupType.slug] |
| 39 | + |
| 40 | + |
| 41 | +class UnableToAcquireLockApiError(SentryAPIException): |
| 42 | + status_code = status.HTTP_400_BAD_REQUEST |
| 43 | + code = "unable_to_acquire_lock" |
| 44 | + message = "Unable to acquire lock for issue alert migration." |
| 45 | + |
| 46 | + |
| 47 | +def _ensure_detector(project: Project, type: str) -> Detector: |
| 48 | + """ |
| 49 | + Ensure that a detector of a given type exists for a project. |
| 50 | + If the Detector doesn't already exist, we try to acquire a lock to avoid double-creating, |
| 51 | + and UnableToAcquireLockApiError if that fails. |
| 52 | + """ |
| 53 | + group_type = grouptype.registry.get_by_slug(type) |
| 54 | + if not group_type: |
| 55 | + raise ValueError(f"Group type {type} not registered") |
| 56 | + slug = group_type.slug |
| 57 | + if slug not in VALID_DEFAULT_DETECTOR_TYPES: |
| 58 | + raise ValueError(f"Invalid default detector type: {slug}") |
| 59 | + |
| 60 | + # If it already exists, life is simple and we can return immediately. |
| 61 | + # If there happen to be duplicates, we prefer the oldest. |
| 62 | + existing = Detector.objects.filter(type=slug, project=project).order_by("id").first() |
| 63 | + if existing: |
| 64 | + return existing |
| 65 | + |
| 66 | + # If we may need to create it, we acquire a lock to avoid double-creating. |
| 67 | + # There isn't a unique constraint on the detector, so we can't rely on get_or_create |
| 68 | + # to avoid duplicates. |
| 69 | + # However, by only locking during the one-time creation, the window for a race condition is small. |
| 70 | + lock = locks.get( |
| 71 | + f"workflow-engine-project-{slug}-detector:{project.id}", |
| 72 | + duration=2, |
| 73 | + name=f"workflow_engine_default_{slug}_detector", |
| 74 | + ) |
| 75 | + try: |
| 76 | + with ( |
| 77 | + # Creation should be fast, so it's worth blocking a little rather |
| 78 | + # than failing a request. |
| 79 | + lock.blocking_acquire(initial_delay=0.1, timeout=3), |
| 80 | + transaction.atomic(router.db_for_write(Detector)), |
| 81 | + ): |
| 82 | + detector, _ = Detector.objects.get_or_create( |
| 83 | + type=slug, |
| 84 | + project=project, |
| 85 | + defaults={ |
| 86 | + "config": {}, |
| 87 | + "name": ( |
| 88 | + ERROR_DETECTOR_NAME |
| 89 | + if slug == ErrorGroupType.slug |
| 90 | + else ISSUE_STREAM_DETECTOR_NAME |
| 91 | + ), |
| 92 | + }, |
| 93 | + ) |
| 94 | + return detector |
| 95 | + except UnableToAcquireLock: |
| 96 | + raise UnableToAcquireLockApiError |
| 97 | + |
| 98 | + |
| 99 | +def ensure_default_detectors(project: Project) -> tuple[Detector, Detector]: |
| 100 | + return _ensure_detector(project, ErrorGroupType.slug), _ensure_detector( |
| 101 | + project, IssueStreamGroupType.slug |
| 102 | + ) |
| 103 | + |
28 | 104 |
|
29 | 105 | def get_detector_by_event(event_data: WorkflowEventData) -> Detector: |
30 | 106 | evt = event_data.event |
|
0 commit comments