|
3 | 3 | from datetime import datetime, timedelta, timezone
|
4 | 4 |
|
5 | 5 | import sentry_sdk
|
6 |
| -from sentry_sdk.consts import INSTRUMENTER, SPANDATA |
| 6 | +from sentry_sdk.consts import INSTRUMENTER, SPANSTATUS, SPANDATA |
7 | 7 | from sentry_sdk.profiler.continuous_profiler import get_profiler_id
|
8 | 8 | from sentry_sdk.utils import (
|
9 | 9 | get_current_thread_meta,
|
@@ -149,6 +149,45 @@ class TransactionKwargs(SpanKwargs, total=False):
|
149 | 149 | }
|
150 | 150 |
|
151 | 151 |
|
| 152 | +def get_span_status_from_http_code(http_status_code): |
| 153 | + # type: (int) -> str |
| 154 | + """ |
| 155 | + Returns the Sentry status corresponding to the given HTTP status code. |
| 156 | +
|
| 157 | + See: https://develop.sentry.dev/sdk/event-payloads/contexts/#trace-context |
| 158 | + """ |
| 159 | + if http_status_code < 400: |
| 160 | + return SPANSTATUS.OK |
| 161 | + |
| 162 | + elif 400 <= http_status_code < 500: |
| 163 | + if http_status_code == 403: |
| 164 | + return SPANSTATUS.PERMISSION_DENIED |
| 165 | + elif http_status_code == 404: |
| 166 | + return SPANSTATUS.NOT_FOUND |
| 167 | + elif http_status_code == 429: |
| 168 | + return SPANSTATUS.RESOURCE_EXHAUSTED |
| 169 | + elif http_status_code == 413: |
| 170 | + return SPANSTATUS.FAILED_PRECONDITION |
| 171 | + elif http_status_code == 401: |
| 172 | + return SPANSTATUS.UNAUTHENTICATED |
| 173 | + elif http_status_code == 409: |
| 174 | + return SPANSTATUS.ALREADY_EXISTS |
| 175 | + else: |
| 176 | + return SPANSTATUS.INVALID_ARGUMENT |
| 177 | + |
| 178 | + elif 500 <= http_status_code < 600: |
| 179 | + if http_status_code == 504: |
| 180 | + return SPANSTATUS.DEADLINE_EXCEEDED |
| 181 | + elif http_status_code == 501: |
| 182 | + return SPANSTATUS.UNIMPLEMENTED |
| 183 | + elif http_status_code == 503: |
| 184 | + return SPANSTATUS.UNAVAILABLE |
| 185 | + else: |
| 186 | + return SPANSTATUS.INTERNAL_ERROR |
| 187 | + |
| 188 | + return SPANSTATUS.UNKNOWN_ERROR |
| 189 | + |
| 190 | + |
152 | 191 | class _SpanRecorder:
|
153 | 192 | """Limits the number of spans recorded in a transaction."""
|
154 | 193 |
|
@@ -317,7 +356,7 @@ def __enter__(self):
|
317 | 356 | def __exit__(self, ty, value, tb):
|
318 | 357 | # type: (Optional[Any], Optional[Any], Optional[Any]) -> None
|
319 | 358 | if value is not None:
|
320 |
| - self.set_status("internal_error") |
| 359 | + self.set_status(SPANSTATUS.INTERNAL_ERROR) |
321 | 360 |
|
322 | 361 | scope, old_span = self._context_manager_state
|
323 | 362 | del self._context_manager_state
|
@@ -540,37 +579,9 @@ def set_http_status(self, http_status):
|
540 | 579 | # type: (int) -> None
|
541 | 580 | self.set_tag(
|
542 | 581 | "http.status_code", str(http_status)
|
543 |
| - ) # we keep this for backwards compatability |
| 582 | + ) # we keep this for backwards compatibility |
544 | 583 | self.set_data(SPANDATA.HTTP_STATUS_CODE, http_status)
|
545 |
| - |
546 |
| - if http_status < 400: |
547 |
| - self.set_status("ok") |
548 |
| - elif 400 <= http_status < 500: |
549 |
| - if http_status == 403: |
550 |
| - self.set_status("permission_denied") |
551 |
| - elif http_status == 404: |
552 |
| - self.set_status("not_found") |
553 |
| - elif http_status == 429: |
554 |
| - self.set_status("resource_exhausted") |
555 |
| - elif http_status == 413: |
556 |
| - self.set_status("failed_precondition") |
557 |
| - elif http_status == 401: |
558 |
| - self.set_status("unauthenticated") |
559 |
| - elif http_status == 409: |
560 |
| - self.set_status("already_exists") |
561 |
| - else: |
562 |
| - self.set_status("invalid_argument") |
563 |
| - elif 500 <= http_status < 600: |
564 |
| - if http_status == 504: |
565 |
| - self.set_status("deadline_exceeded") |
566 |
| - elif http_status == 501: |
567 |
| - self.set_status("unimplemented") |
568 |
| - elif http_status == 503: |
569 |
| - self.set_status("unavailable") |
570 |
| - else: |
571 |
| - self.set_status("internal_error") |
572 |
| - else: |
573 |
| - self.set_status("unknown_error") |
| 584 | + self.set_status(get_span_status_from_http_code(http_status)) |
574 | 585 |
|
575 | 586 | def is_success(self):
|
576 | 587 | # type: () -> bool
|
|
0 commit comments