|
1 | 1 | import logging
|
2 | 2 | from dataclasses import dataclass
|
| 3 | +from enum import StrEnum |
3 | 4 | from typing import Any
|
4 | 5 | from urllib.parse import urlparse
|
5 | 6 | from uuid import uuid4
|
|
10 | 11 | from sentry.http import safe_urlread
|
11 | 12 | from sentry.models.group import Group
|
12 | 13 | from sentry.sentry_apps.external_requests.utils import send_and_save_sentry_app_request, validate
|
| 14 | +from sentry.sentry_apps.metrics import ( |
| 15 | + SentryAppEventType, |
| 16 | + SentryAppExternalRequestHaltReason, |
| 17 | + SentryAppInteractionEvent, |
| 18 | + SentryAppInteractionType, |
| 19 | +) |
13 | 20 | from sentry.sentry_apps.services.app import RpcSentryAppInstallation
|
14 | 21 | from sentry.sentry_apps.utils.errors import SentryAppIntegratorError
|
15 | 22 | from sentry.users.models.user import User
|
|
18 | 25 |
|
19 | 26 | logger = logging.getLogger("sentry.sentry_apps.external_requests")
|
20 | 27 | ACTION_TO_PAST_TENSE = {"create": "created", "link": "linked"}
|
| 28 | +FAILURE_REASON_BASE = f"{SentryAppEventType.EXTERNAL_ISSUE_LINKED}.{{}}" |
| 29 | +BAD_RESPONSE_HALT_REASON = FAILURE_REASON_BASE.format( |
| 30 | + SentryAppExternalRequestHaltReason.BAD_RESPONSE |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +class IssueRequestActionType(StrEnum): |
| 35 | + CREATE = "create" |
| 36 | + LINK = "link" |
21 | 37 |
|
22 | 38 |
|
23 | 39 | @dataclass
|
@@ -56,64 +72,74 @@ class IssueLinkRequester:
|
56 | 72 | group: Group
|
57 | 73 | fields: dict[str, Any]
|
58 | 74 | user: RpcUser | User
|
59 |
| - action: str |
| 75 | + action: IssueRequestActionType |
60 | 76 |
|
61 | 77 | def run(self) -> dict[str, Any]:
|
62 | 78 | response: dict[str, str] = {}
|
63 |
| - |
64 |
| - try: |
65 |
| - request = send_and_save_sentry_app_request( |
66 |
| - self._build_url(), |
67 |
| - self.sentry_app, |
68 |
| - self.install.organization_id, |
69 |
| - f"external_issue.{ACTION_TO_PAST_TENSE[self.action]}", |
70 |
| - headers=self._build_headers(), |
71 |
| - method="POST", |
72 |
| - data=self.body, |
73 |
| - ) |
74 |
| - body = safe_urlread(request) |
75 |
| - response = json.loads(body) |
76 |
| - except (json.JSONDecodeError, TypeError): |
77 |
| - raise SentryAppIntegratorError( |
78 |
| - message=f"Unable to parse response from {self.sentry_app.slug}", |
79 |
| - webhook_context={ |
80 |
| - "error_type": "issue-link-requester.invalid-json", |
81 |
| - "uri": self.uri, |
82 |
| - "response": body, |
83 |
| - "installation_uuid": self.install.uuid, |
84 |
| - }, |
85 |
| - status_code=500, |
86 |
| - ) |
87 |
| - except RequestException as e: |
88 |
| - error_type = "issue-link-requester.error" |
89 |
| - extras = { |
90 |
| - "sentry_app": self.sentry_app.slug, |
91 |
| - "installation_uuid": self.install.uuid, |
92 |
| - "project": self.group.project.slug, |
93 |
| - "group": self.group.id, |
| 79 | + event = SentryAppEventType(f"external_issue.{ACTION_TO_PAST_TENSE[self.action]}") |
| 80 | + with SentryAppInteractionEvent( |
| 81 | + operation_type=SentryAppInteractionType.EXTERNAL_REQUEST, |
| 82 | + event_type=event, |
| 83 | + ).capture() as lifecycle: |
| 84 | + extras: dict[str, Any] = { |
94 | 85 | "uri": self.uri,
|
95 |
| - "error_message": str(e), |
| 86 | + "installation_uuid": self.install.uuid, |
| 87 | + "sentry_app_slug": self.sentry_app.slug, |
| 88 | + "project_slug": self.group.project.slug, |
| 89 | + "group_id": self.group.id, |
96 | 90 | }
|
97 |
| - logger.info(error_type, extra=extras) |
98 |
| - raise SentryAppIntegratorError( |
99 |
| - message=f"Issue occured while trying to contact {self.sentry_app.slug} to link issue", |
100 |
| - webhook_context={"error_type": error_type, **extras}, |
101 |
| - status_code=500, |
102 |
| - ) |
103 |
| - |
104 |
| - if not self._validate_response(response): |
105 |
| - raise SentryAppIntegratorError( |
106 |
| - message=f"Invalid response format from sentry app {self.sentry_app} when linking issue", |
107 |
| - webhook_context={ |
108 |
| - "error_type": "issue-link-requester.invalid-response", |
109 |
| - "response": response, |
110 |
| - "installation_uuid": self.install.uuid, |
111 |
| - "uri": self.uri, |
112 |
| - }, |
113 |
| - status_code=500, |
114 |
| - ) |
115 |
| - |
116 |
| - return response |
| 91 | + try: |
| 92 | + request = send_and_save_sentry_app_request( |
| 93 | + self._build_url(), |
| 94 | + self.sentry_app, |
| 95 | + self.install.organization_id, |
| 96 | + event, |
| 97 | + headers=self._build_headers(), |
| 98 | + method="POST", |
| 99 | + data=self.body, |
| 100 | + ) |
| 101 | + response_body = safe_urlread(request) |
| 102 | + |
| 103 | + response = json.loads(response_body) |
| 104 | + except (json.JSONDecodeError, TypeError) as e: |
| 105 | + extras["response_body"] = response_body |
| 106 | + lifecycle.record_halt( |
| 107 | + halt_reason=e, |
| 108 | + extra={"halt_reason": BAD_RESPONSE_HALT_REASON, **extras}, |
| 109 | + ) |
| 110 | + |
| 111 | + raise SentryAppIntegratorError( |
| 112 | + message=f"Unable to parse response from {self.sentry_app.slug}", |
| 113 | + webhook_context={"error_type": BAD_RESPONSE_HALT_REASON, **extras}, |
| 114 | + status_code=500, |
| 115 | + ) |
| 116 | + except RequestException as e: |
| 117 | + extras["error_message"] = str(e) |
| 118 | + lifecycle.record_halt( |
| 119 | + halt_reason=e, |
| 120 | + extra={"halt_reason": BAD_RESPONSE_HALT_REASON, **extras}, |
| 121 | + ) |
| 122 | + |
| 123 | + raise SentryAppIntegratorError( |
| 124 | + message=f"Issue occured while trying to contact {self.sentry_app.slug} to link issue", |
| 125 | + webhook_context={"error_type": BAD_RESPONSE_HALT_REASON, **extras}, |
| 126 | + status_code=500, |
| 127 | + ) |
| 128 | + |
| 129 | + if not self._validate_response(response): |
| 130 | + extras["response"] = response |
| 131 | + lifecycle.record_halt( |
| 132 | + halt_reason=BAD_RESPONSE_HALT_REASON, |
| 133 | + extra={"halt_reason": BAD_RESPONSE_HALT_REASON, **extras}, |
| 134 | + ) |
| 135 | + |
| 136 | + raise SentryAppIntegratorError( |
| 137 | + message=f"Invalid response format from sentry app {self.sentry_app} when linking issue", |
| 138 | + webhook_context={"error_type": BAD_RESPONSE_HALT_REASON, **extras}, |
| 139 | + status_code=500, |
| 140 | + ) |
| 141 | + |
| 142 | + return response |
117 | 143 |
|
118 | 144 | def _build_url(self) -> str:
|
119 | 145 | urlparts = urlparse(self.sentry_app.webhook_url)
|
|
0 commit comments