-
Notifications
You must be signed in to change notification settings - Fork 515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved handling of span status #3261
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
from datetime import datetime, timedelta, timezone | ||
|
||
import sentry_sdk | ||
from sentry_sdk.consts import INSTRUMENTER, SPANDATA | ||
from sentry_sdk.consts import INSTRUMENTER, SPANSTATUS, SPANDATA | ||
from sentry_sdk.profiler.continuous_profiler import get_profiler_id | ||
from sentry_sdk.utils import ( | ||
get_current_thread_meta, | ||
|
@@ -151,6 +151,45 @@ class TransactionKwargs(SpanKwargs, total=False): | |
} | ||
|
||
|
||
def get_span_status_from_http_code(http_status_code): | ||
# type: (int) -> str | ||
""" | ||
Returns the Sentry status corresponding to the given HTTP status code. | ||
|
||
See: https://develop.sentry.dev/sdk/event-payloads/contexts/#trace-context | ||
""" | ||
if http_status_code < 400: | ||
return SPANSTATUS.OK | ||
|
||
elif 400 <= http_status_code < 500: | ||
if http_status_code == 403: | ||
return SPANSTATUS.PERMISSION_DENIED | ||
elif http_status_code == 404: | ||
return SPANSTATUS.NOT_FOUND | ||
elif http_status_code == 429: | ||
return SPANSTATUS.RESOURCE_EXHAUSTED | ||
elif http_status_code == 413: | ||
return SPANSTATUS.FAILED_PRECONDITION | ||
elif http_status_code == 401: | ||
return SPANSTATUS.UNAUTHENTICATED | ||
elif http_status_code == 409: | ||
return SPANSTATUS.ALREADY_EXISTS | ||
else: | ||
return SPANSTATUS.INVALID_ARGUMENT | ||
|
||
elif 500 <= http_status_code < 600: | ||
if http_status_code == 504: | ||
return SPANSTATUS.DEADLINE_EXCEEDED | ||
elif http_status_code == 501: | ||
return SPANSTATUS.UNIMPLEMENTED | ||
elif http_status_code == 503: | ||
return SPANSTATUS.UNAVAILABLE | ||
else: | ||
return SPANSTATUS.INTERNAL_ERROR | ||
Comment on lines
+162
to
+186
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to sort the if statements in increasing order by status code. Although, perhaps this would better belong in a separate PR |
||
|
||
return SPANSTATUS.UNKNOWN_ERROR | ||
|
||
|
||
class _SpanRecorder: | ||
"""Limits the number of spans recorded in a transaction.""" | ||
|
||
|
@@ -319,7 +358,7 @@ def __enter__(self): | |
def __exit__(self, ty, value, tb): | ||
# type: (Optional[Any], Optional[Any], Optional[Any]) -> None | ||
if value is not None: | ||
self.set_status("internal_error") | ||
self.set_status(SPANSTATUS.INTERNAL_ERROR) | ||
|
||
scope, old_span = self._context_manager_state | ||
del self._context_manager_state | ||
|
@@ -542,37 +581,9 @@ def set_http_status(self, http_status): | |
# type: (int) -> None | ||
self.set_tag( | ||
"http.status_code", str(http_status) | ||
) # we keep this for backwards compatability | ||
) # we keep this for backwards compatibility | ||
self.set_data(SPANDATA.HTTP_STATUS_CODE, http_status) | ||
|
||
if http_status < 400: | ||
self.set_status("ok") | ||
elif 400 <= http_status < 500: | ||
if http_status == 403: | ||
self.set_status("permission_denied") | ||
elif http_status == 404: | ||
self.set_status("not_found") | ||
elif http_status == 429: | ||
self.set_status("resource_exhausted") | ||
elif http_status == 413: | ||
self.set_status("failed_precondition") | ||
elif http_status == 401: | ||
self.set_status("unauthenticated") | ||
elif http_status == 409: | ||
self.set_status("already_exists") | ||
else: | ||
self.set_status("invalid_argument") | ||
elif 500 <= http_status < 600: | ||
if http_status == 504: | ||
self.set_status("deadline_exceeded") | ||
elif http_status == 501: | ||
self.set_status("unimplemented") | ||
elif http_status == 503: | ||
self.set_status("unavailable") | ||
else: | ||
self.set_status("internal_error") | ||
else: | ||
self.set_status("unknown_error") | ||
self.set_status(get_span_status_from_http_code(http_status)) | ||
|
||
def is_success(self): | ||
# type: () -> bool | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we make this an enum?