-
Notifications
You must be signed in to change notification settings - Fork 643
feat: flask integration #5
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
Merged
Changes from all commits
Commits
Show all changes
3 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 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,70 +1,15 @@ | ||
| language: python | ||
| sudo: false | ||
|
|
||
| matrix: | ||
| include: | ||
| - python: "2.7" | ||
| env: DJANGO_VERSION=1.4.* | ||
| - python: "2.7" | ||
| env: DJANGO_VERSION=1.5.* | ||
| - python: "2.7" | ||
| env: DJANGO_VERSION=1.6.* | ||
| - python: "2.7" | ||
| env: DJANGO_VERSION=1.7.* | ||
| - python: "2.7" | ||
| env: DJANGO_VERSION=1.8.* | ||
| - python: "2.7" | ||
| env: DJANGO_VERSION=1.9.* | ||
|
|
||
| - python: "3.4" | ||
| env: DJANGO_VERSION=1.5.* | ||
| - python: "3.4" | ||
| env: DJANGO_VERSION=1.6.* | ||
| - python: "3.4" | ||
| env: DJANGO_VERSION=1.7.* | ||
| - python: "3.4" | ||
| env: DJANGO_VERSION=1.8.* | ||
| - python: "3.4" | ||
| env: DJANGO_VERSION=1.9.* | ||
| - python: "3.4" | ||
| env: DJANGO_VERSION=2.0.* | ||
|
|
||
| - python: "3.5" | ||
| env: DJANGO_VERSION=1.8.* | ||
| - python: "3.5" | ||
| env: DJANGO_VERSION=1.9.* | ||
| - python: "3.5" | ||
| env: DJANGO_VERSION=2.0.* | ||
|
|
||
| - python: "3.6" | ||
| env: DJANGO_VERSION=1.8.* | ||
| - python: "3.6" | ||
| env: DJANGO_VERSION=1.9.* | ||
| - python: "3.6" | ||
| env: DJANGO_VERSION=2.0.* | ||
|
|
||
| - python: "3.6" | ||
| env: DJANGO_VERSION=1.8.* | ||
| - python: "3.6" | ||
| env: DJANGO_VERSION=1.9.* | ||
| - python: "3.6" | ||
| env: DJANGO_VERSION=2.0.* | ||
|
|
||
| - python: "3.7-dev" | ||
| env: DJANGO_VERSION=1.8.* | ||
| - python: "3.7-dev" | ||
| env: DJANGO_VERSION=1.9.* | ||
| - python: "3.7-dev" | ||
| env: DJANGO_VERSION=2.0.* | ||
| python: | ||
| - "2.7" | ||
| - "3.4" | ||
| - "3.5" | ||
| - "3.6" | ||
| - "3.7-dev" | ||
|
|
||
| install: | ||
| - pip install -e . | ||
| - pip install -Ur dev-requirements.txt | ||
| - pip install -U "django==$DJANGO_VERSION" | ||
| - | | ||
| if [ "$DJANGO_VERSION" = 1.4.* ] || [ "$DJANGO_VERSION" = 1.5.* ] || [ "$DJANGO_VERSION" = 1.6.* ] || [ "$DJANGO_VERSION" = 1.7.* ] || [ "$DJANGO_VERSION" = 1.8.* ]; then | ||
| pip install 'pytest-django<3.0'; | ||
| fi | ||
| - pip install tox | ||
|
|
||
| script: | ||
| - pytest # or py.test for Python versions 3.5 and below | ||
| - tox -e $(tox -l | grep $(echo $TRAVIS_PYTHON_VERSION | sed -e 's/[^0-9]//g') | tr '\n' ',') |
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,3 +1,5 @@ | ||
| pytest | ||
| pytest-django | ||
| django | ||
| flask | ||
| flask-login |
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,2 +1,2 @@ | ||
| [pytest] | ||
| DJANGO_SETTINGS_MODULE = tests.django.myapp.settings | ||
| DJANGO_SETTINGS_MODULE = tests.integrations.django.myapp.settings |
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,7 @@ | ||
| def get_environ(environ): | ||
| """ | ||
| Returns our whitelisted environment variables. | ||
| """ | ||
| for key in ('REMOTE_ADDR', 'SERVER_NAME', 'SERVER_PORT'): | ||
| if key in environ: | ||
| yield key, environ[key] |
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,111 @@ | ||
| from __future__ import absolute_import | ||
|
|
||
| from sentry_sdk import capture_exception, configure_scope, get_current_hub | ||
| from ._wsgi import get_environ | ||
|
|
||
| try: | ||
| from flask_login import current_user | ||
| except ImportError: | ||
| current_user = None | ||
|
|
||
| from flask import current_app, request, _app_ctx_stack | ||
| from flask.signals import appcontext_pushed, appcontext_popped, got_request_exception | ||
|
|
||
| class FlaskSentry(object): | ||
| def __init__(self, app=None): | ||
| self.app = app | ||
| if app is not None: | ||
| self.init_app(app) | ||
|
|
||
| def init_app(self, app): | ||
| if not hasattr(app, 'extensions'): | ||
| app.extensions = {} | ||
| elif app.extensions.get(__name__, None): | ||
| raise RuntimeError('Sentry registration is already registered') | ||
| app.extensions[__name__] = True | ||
|
|
||
| appcontext_pushed.connect(_push_appctx, sender=app) | ||
| app.teardown_appcontext(_pop_appctx) | ||
| got_request_exception.connect(_capture_exception, sender=app) | ||
| app.before_request(_before_request) | ||
|
|
||
|
|
||
| def _push_appctx(*args, **kwargs): | ||
| assert getattr( | ||
| _app_ctx_stack.top, | ||
| '_sentry_app_scope_manager', | ||
| None | ||
| ) is None, 'race condition' | ||
| _app_ctx_stack.top._sentry_app_scope_manager = \ | ||
| get_current_hub().push_scope().__enter__() | ||
|
|
||
|
|
||
| def _pop_appctx(exception): | ||
| assert getattr( | ||
| _app_ctx_stack.top, | ||
| '_sentry_app_scope_manager', | ||
| None | ||
| ) is not None, 'race condition' | ||
| _app_ctx_stack.top._sentry_app_scope_manager.__exit__(None, None, None) | ||
| _app_ctx_stack.top._sentry_app_scope_manager = None | ||
|
|
||
|
|
||
| def _capture_exception(sender, exception, **kwargs): | ||
| capture_exception(exception) | ||
|
|
||
|
|
||
| def _before_request(*args, **kwargs): | ||
| assert getattr( | ||
| _app_ctx_stack.top, | ||
| '_sentry_app_scope_manager', | ||
| None | ||
| ) is not None, 'scope push failed' | ||
|
|
||
| with configure_scope() as scope: | ||
| if request.url_rule: | ||
| scope.transaction = request.url_rule.endpoint | ||
|
|
||
| try: | ||
| scope.request = _get_request_info() | ||
| except Exception: | ||
| get_current_hub().capture_internal_exception() | ||
|
|
||
| try: | ||
| scope.user = _get_user_info() | ||
| except Exception: | ||
| raise | ||
| get_current_hub().capture_internal_exception() | ||
|
|
||
| def _get_request_info(): | ||
| { | ||
| 'url': '%s://%s%s' % (request.scheme, urlparts.host, request.path), | ||
| 'query_string': urlparts.query, | ||
| 'method': request.method, | ||
| 'data': request.get_data(cache=True, as_text=True, parse_form_data=True), | ||
| 'headers': dict(request.headers), | ||
| 'env': get_environ(request.environ) | ||
| } | ||
|
|
||
|
|
||
| def _get_user_info(): | ||
| try: | ||
| ip_address = request.access_route[0] | ||
| except IndexError: | ||
| ip_address = request.remote_addr | ||
|
|
||
| user_info = { | ||
| 'id': None, | ||
| 'ip_address': ip_address | ||
| } | ||
|
|
||
| try: | ||
| user_info['id'] = current_user.get_id() | ||
| # TODO: more configurable user attrs here | ||
| except AttributeError: | ||
| # might happen if: | ||
| # - flask_login could not be imported | ||
| # - flask_login is not configured | ||
| # - no user is logged in | ||
| pass | ||
|
|
||
| return user_info | ||
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 was deleted.
Oops, something went wrong.
File renamed without changes.
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,50 @@ | ||
| import pytest | ||
| import sentry_sdk | ||
| from sentry_sdk.client import Client, Transport | ||
|
|
||
| class TestClient(Client): | ||
| def __init__(self): | ||
| pass | ||
|
|
||
| def capture_internal_exception(self, error=None): | ||
| if not error: | ||
| raise | ||
| raise error | ||
|
|
||
| dsn = 'LOL' | ||
| options = {'with_locals': False, 'release': 'fake_release', 'environment': 'fake_environment', 'server_name': 'fake_servername'} | ||
| _transport = None | ||
|
|
||
|
|
||
| class TestTransport(Transport): | ||
| def __init__(self): | ||
| pass | ||
|
|
||
| def start(self): | ||
| pass | ||
|
|
||
| def close(self): | ||
| pass | ||
|
|
||
| test_client = TestClient() | ||
|
|
||
| sentry_sdk.init() | ||
| sentry_sdk.get_current_hub().bind_client(test_client) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def capture_exceptions(monkeypatch): | ||
| errors = [] | ||
| def capture_exception(error=None): | ||
| errors.append(error or sys.exc_info()[1]) | ||
|
|
||
| monkeypatch.setattr(sentry_sdk.Hub.current, 'capture_exception', capture_exception) | ||
| return errors | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def capture_events(monkeypatch): | ||
| events = [] | ||
| monkeypatch.setattr(test_client, '_transport', TestTransport()) | ||
| monkeypatch.setattr(test_client._transport, 'capture_event', events.append) | ||
| return events |
File renamed without changes.
Empty file.
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
File renamed without changes.
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
File renamed without changes.
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
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.
Unsure about this. The old integration didn't have any protection against too large requests either.