This repository was archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Add Sentry integration #57
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad6532e
Add option to log to Sentry
codetheweb 7b68d0b
Add decorator
codetheweb 24eebfb
Add breadcrumb
codetheweb 08452af
Fix decorator
codetheweb 992c6fa
Add get_sentry_dsn()
codetheweb 31afbfc
Ignore changes to DSN file
codetheweb 96153fe
Embed Sentry DSN during release
codetheweb 9215192
Don't use singleton
codetheweb 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| dist | ||
| seamapi/utils/get_sentry_dsn.py | ||
| .env | ||
| __pycache__ | ||
| */__pycache__ |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import os | ||
|
|
||
| value = os.environ.get("SENTRY_DSN", "None") | ||
| if value != "None": | ||
| value = f'"{value}"' | ||
|
|
||
| f = open("seamapi/utils/get_sentry_dsn.py", "w") | ||
| f.write(f""" | ||
| def get_sentry_dsn(): | ||
| return {value} | ||
| """) | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| import os | ||
|
|
||
| from seamapi.utils.get_sentry_dsn import get_sentry_dsn | ||
| from .routes import Routes | ||
| import requests | ||
| import sentry_sdk | ||
| import pkg_resources | ||
| from typing import Optional, cast | ||
| from .types import AbstractSeam, SeamAPIException | ||
|
|
@@ -43,6 +46,7 @@ def __init__( | |
| self, | ||
| api_key: Optional[str] = None, | ||
| api_url: Optional[str] = None, | ||
| should_report_exceptions: Optional[bool] = False, | ||
| ): | ||
| """ | ||
| Parameters | ||
|
|
@@ -51,6 +55,8 @@ def __init__( | |
| API key | ||
| api_url : str, optional | ||
| API url | ||
| should_report_exceptions : bool, optional | ||
| Defaults to False. If true, thrown exceptions will be reported to Seam. | ||
| """ | ||
| Routes.__init__(self) | ||
|
|
||
|
|
@@ -64,6 +70,17 @@ def __init__( | |
| api_url = os.environ.get("SEAM_API_URL", self.api_url) | ||
| self.api_key = api_key | ||
| self.api_url = cast(str, api_url) | ||
| self.should_report_exceptions = should_report_exceptions | ||
|
|
||
| if self.should_report_exceptions: | ||
| self.sentry_client = sentry_sdk.Hub(sentry_sdk.Client( | ||
| dsn=get_sentry_dsn(), | ||
| )) | ||
|
Contributor
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. nice! |
||
| self.sentry_client.scope.set_context("sdk_info", { | ||
| "repository": "https://github.com/seamapi/python", | ||
| "version": pkg_resources.get_distribution("seamapi").version, | ||
| "endpoint": self.api_url, | ||
| }) | ||
|
|
||
| def make_request(self, method: str, path: str, **kwargs): | ||
| """ | ||
|
|
@@ -87,6 +104,20 @@ def make_request(self, method: str, path: str, **kwargs): | |
| } | ||
| response = requests.request(method, url, headers=headers, **kwargs) | ||
|
|
||
| if self.should_report_exceptions and response.status_code: | ||
| # Add breadcrumb | ||
| self.sentry_client.add_breadcrumb( | ||
| category="http", | ||
| level="info", | ||
| data={ | ||
| "method": method, | ||
| "url": url, | ||
| "status_code": response.status_code, | ||
| "request_id": response.headers.get("seam-request-id", "unknown"), | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| parsed_response = response.json() | ||
|
|
||
| if response.status_code != 200: | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import os | ||
|
|
||
| def get_sentry_dsn(): | ||
| # This is replaced with a hard-coded value during the build process (see scripts/prebuild.py) | ||
| return os.environ.get("SENTRY_DSN", None) |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from seamapi.types import SeamAPIException | ||
|
|
||
| """ | ||
| A decorator for model methods that will report errors to Sentry (if enabled). | ||
| Expects that the model has a `seam` attribute that is a `Seam` instance. | ||
| """ | ||
| def report_error(f): | ||
| def wrapper(self, *args, **kwargs): | ||
| try: | ||
| return f(self, *args, **kwargs) | ||
| except Exception as error: | ||
| if self.seam.should_report_exceptions and type(error) is not SeamAPIException: | ||
| self.seam.sentry_client.capture_exception(error) | ||
|
|
||
| raise error | ||
| return wrapper | ||
codetheweb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.