Skip to content
Merged

Ci #3

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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.*

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

script:
- pytest # or py.test for Python versions 3.5 and below
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pytest
pytest-django
django
40 changes: 24 additions & 16 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from __future__ import absolute_import

from threading import Lock, local

from django.apps import AppConfig
from django.conf import settings
from django.core import signals
from django.urls import resolve
try:
from django.urls import resolve
except ImportError:
from django.core.urlresolvers import resolve

from sentry_sdk import get_current_hub, configure_scope, capture_exception

Expand All @@ -25,19 +29,17 @@ def _get_transaction_from_request(request):
# request_started (or any other signal) cannot be used because the request is
# not yet available
class SentryMiddleware(MiddlewareMixin):
def process_view(self, request, func, args, kwargs):
def process_request(self, request):
assert getattr(_request_scope, 'manager', None) is None, 'race condition'
_request_scope.manager = get_current_hub().push_scope().__enter__()

try:
with configure_scope() as scope:
scope.transaction = _get_transaction_from_request(request)
except Exception:
capture_exception()


def _request_started(*args, **kwargs):
assert getattr(_request_scope, 'manager', None) is None, 'race condition'
_request_scope.manager = get_current_hub().push_scope().__enter__()


def _request_finished(*args, **kwargs):
assert getattr(_request_scope, 'manager', None) is not None, 'race condition'
_request_scope.manager.__exit__(None, None, None)
Expand Down Expand Up @@ -86,18 +88,24 @@ def _initialize_impl():
middleware_attr,
[MIDDLEWARE_NAME] + list(middleware))

signals.request_started.connect(_request_started)
signals.request_finished.connect(_request_finished)
signals.got_request_exception.connect(_got_request_exception)


default_app_config = 'sentry_sdk.integrations.django.SentryConfig'


class SentryConfig(AppConfig):
name = 'sentry_sdk.integrations.django'
label = 'sentry_sdk_integrations_django'
verbose_name = 'Sentry'
try:
# Django >= 1.7
from django.apps import AppConfig
except ImportError:
initialize()
else:
class SentryConfig(AppConfig):
name = 'sentry_sdk.integrations.django'
label = 'sentry_sdk_integrations_django'
verbose_name = 'Sentry'

def ready(self):
initialize()

def ready(self):
initialize()
default_app_config = 'sentry_sdk.integrations.django.SentryConfig'
38 changes: 25 additions & 13 deletions tests/django/myapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
"""

import os
import sentry_sdk

try:
# Django >= 1.10
from django.utils.deprecation import MiddlewareMixin
except ImportError:
# Not required for Django <= 1.9, see:
# https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware
MiddlewareMixin = object

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -39,26 +48,29 @@
'sentry_sdk.integrations.django'
]

class TestMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
class TestMiddleware(MiddlewareMixin):
def process_request(self, request):
if 'middleware-exc' in request.path:
1/0
return get_response(request)

with sentry_sdk.configure_scope() as scope:
assert scope._data['transaction'] is not None

def process_response(self, request, response):
with sentry_sdk.configure_scope() as scope:
assert scope._data['transaction'] is not None

return response

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware'
MIDDLEWARE_CLASSES = [
'tests.django.myapp.settings.TestMiddleware'
]

if MiddlewareMixin is not object:
MIDDLEWARE = MIDDLEWARE_CLASSES


ROOT_URLCONF = 'tests.django.myapp.urls'

TEMPLATES = [
Expand Down
10 changes: 7 additions & 3 deletions tests/django/myapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from __future__ import absolute_import

try:
from django.urls import path
except ImportError:
from django.conf.urls import url as path

from . import views

urlpatterns = [
path('self-check', views.self_check, name='self_check'),
path('view-exc', views.view_exc, name='view_exc'),
path('middleware-exc', views.view_exc, name='middleware_exc'),
path('middleware-exc', views.self_check, name='middleware_exc'),
path('get-dsn', views.get_dsn, name='get_dsn')
]
12 changes: 3 additions & 9 deletions tests/django/myapp/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.http import HttpResponse
from django.template import engines
from django.template import Template, Context

import sentry_sdk

Expand All @@ -8,11 +8,6 @@
def self_check(request):
with sentry_sdk.configure_scope() as scope:
assert scope._data['transaction'] == self_check
scope.set_tag('foo', 'bar')

with sentry_sdk.configure_scope() as scope:
# ensure scope did not get popped
assert scope._data['transaction'] == self_check
return HttpResponse("ok")


Expand All @@ -21,11 +16,10 @@ def view_exc(request):


def get_dsn(request):
django_engine = engines['django']
template = django_engine.from_string(
template = Template(
"{% load sentry %}{% sentry_dsn %}!"
)
return HttpResponse(
template.render({}, request),
template.render(Context()),
content_type='application/xhtml+xml'
)
5 changes: 4 additions & 1 deletion tests/django/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

from django.test import Client
from django.test.utils import setup_test_environment
from django.urls import reverse
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse

from sentry_sdk import Hub, Client as SentryClient

Expand Down