diff --git a/contrib/opencensus-ext-django/CHANGELOG.md b/contrib/opencensus-ext-django/CHANGELOG.md index aa06979d2..6d882bd43 100644 --- a/contrib/opencensus-ext-django/CHANGELOG.md +++ b/contrib/opencensus-ext-django/CHANGELOG.md @@ -2,6 +2,14 @@ ## Unreleased +## 0.3.2 +Released 2019-07-26 + +- Removed support for Django < 1.11 + ([#694](https://github.com/census-instrumentation/opencensus-python/pull/694)) +- Allow installing with Django 2.0 and later + ([#697](https://github.com/census-instrumentation/opencensus-python/pull/697)) + ## 0.3.1 Released 2019-06-04 diff --git a/contrib/opencensus-ext-django/README.rst b/contrib/opencensus-ext-django/README.rst index 9fe978fc6..13601efb1 100644 --- a/contrib/opencensus-ext-django/README.rst +++ b/contrib/opencensus-ext-django/README.rst @@ -17,24 +17,15 @@ Usage ----- For tracing Django requests, you will need to add the following line to -the ``MIDDLEWARE_CLASSES`` section in the Django ``settings.py`` file. +the ``MIDDLEWARE`` section in the Django ``settings.py`` file. .. code:: python - MIDDLEWARE_CLASSES = [ + MIDDLEWARE = [ ... 'opencensus.ext.django.middleware.OpencensusMiddleware', ] -And add this line to the ``INSTALLED_APPS`` section: - -.. code:: python - - INSTALLED_APPS = [ - ... - 'opencensus.ext.django', - ] - Additional configuration can be provided, please read `Customization `_ for a complete reference. diff --git a/contrib/opencensus-ext-django/examples/app/settings.py b/contrib/opencensus-ext-django/examples/app/settings.py index ec039902b..3a391eb8a 100644 --- a/contrib/opencensus-ext-django/examples/app/settings.py +++ b/contrib/opencensus-ext-django/examples/app/settings.py @@ -29,19 +29,17 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'opencensus.trace.ext.django', ) -MIDDLEWARE_CLASSES = ( +MIDDLEWARE = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', - 'opencensus.trace.ext.django.middleware.OpencensusMiddleware', + 'opencensus.ext.django.middleware.OpencensusMiddleware', ) ROOT_URLCONF = 'app.urls' diff --git a/contrib/opencensus-ext-django/examples/app/urls.py b/contrib/opencensus-ext-django/examples/app/urls.py index 7d3f35b22..cbe75510d 100644 --- a/contrib/opencensus-ext-django/examples/app/urls.py +++ b/contrib/opencensus-ext-django/examples/app/urls.py @@ -27,14 +27,14 @@ 1. Add an import: from blog import urls as blog_urls 2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls)) """ -from django.conf.urls import include, url +from django.conf.urls import url from django.contrib import admin import app.views urlpatterns = [ - url(r'^admin/', include(admin.site.urls)), + url(r'^admin/', admin.site.urls), url(r'^$', app.views.home), url(r'^greetings$', app.views.greetings), url(r'^_ah/health$', app.views.health_check), diff --git a/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py b/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py index 886e6b04f..e48bf814b 100644 --- a/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py +++ b/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py @@ -17,6 +17,7 @@ import six import django.conf +from django.utils.deprecation import MiddlewareMixin from opencensus.common import configuration from opencensus.trace import attributes_helper @@ -28,11 +29,6 @@ from opencensus.trace import utils from opencensus.trace.propagation import trace_context_http_header_format -try: - from django.utils.deprecation import MiddlewareMixin -except ImportError: # pragma: NO COVER - MiddlewareMixin = object - HTTP_METHOD = attributes_helper.COMMON_ATTRIBUTES['HTTP_METHOD'] HTTP_URL = attributes_helper.COMMON_ATTRIBUTES['HTTP_URL'] HTTP_STATUS_CODE = attributes_helper.COMMON_ATTRIBUTES['HTTP_STATUS_CODE'] @@ -90,12 +86,7 @@ def _set_django_attributes(span, request): return user_id = django_user.pk - try: - user_name = django_user.get_username() - except AttributeError: - # AnonymousUser in some older versions of Django doesn't implement - # get_username - return + user_name = django_user.get_username() # User id is the django autofield for User model as the primary key if user_id is not None: diff --git a/contrib/opencensus-ext-django/setup.py b/contrib/opencensus-ext-django/setup.py index 1b9576a8b..25303ec01 100644 --- a/contrib/opencensus-ext-django/setup.py +++ b/contrib/opencensus-ext-django/setup.py @@ -26,6 +26,10 @@ 'Development Status :: 3 - Alpha', 'Intended Audience :: Developers', 'License :: OSI Approved :: Apache Software License', + 'Framework :: Django', + 'Framework :: Django :: 1.11', + 'Framework :: Django :: 2.1', + 'Framework :: Django :: 2.2', 'Programming Language :: Python', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', @@ -39,7 +43,7 @@ include_package_data=True, long_description=open('README.rst').read(), install_requires=[ - 'Django >= 1.11.0, <= 1.11.20', + 'Django >= 1.11', 'opencensus >= 0.6.0, < 1.0.0', ], extras_require={}, diff --git a/contrib/opencensus-ext-django/tests/test_django_middleware.py b/contrib/opencensus-ext-django/tests/test_django_middleware.py index 8b415d4a4..e037b8fad 100644 --- a/contrib/opencensus-ext-django/tests/test_django_middleware.py +++ b/contrib/opencensus-ext-django/tests/test_django_middleware.py @@ -230,56 +230,6 @@ def test_process_response(self): self.assertEqual(span.attributes, expected_attributes) - def test_process_response_no_get_username(self): - from opencensus.ext.django import middleware - - trace_id = '2dd43a1d6b2549c6bc2a1a54c2fc0b05' - span_id = '6e0c63257de34c92' - django_trace_id = '00-{}-{}-00'.format(trace_id, span_id) - - django_request = RequestFactory().get('/', **{ - 'traceparent': django_trace_id, - }) - - # Force the test request to be sampled - settings = type('Test', (object,), {}) - settings.OPENCENSUS = { - 'TRACE': { - 'SAMPLER': 'opencensus.trace.samplers.AlwaysOnSampler()', # noqa - } - } - patch_settings = mock.patch( - 'django.conf.settings', - settings) - - with patch_settings: - middleware_obj = middleware.OpencensusMiddleware() - - middleware_obj.process_request(django_request) - tracer = middleware._get_current_tracer() - span = tracer.current_span() - - exporter_mock = mock.Mock() - tracer.exporter = exporter_mock - - django_response = mock.Mock() - django_response.status_code = 200 - - expected_attributes = { - 'http.url': u'/', - 'http.method': 'GET', - 'http.status_code': '200', - } - - mock_user = mock.Mock() - mock_user.pk = 123 - mock_user.get_username.side_effect = AttributeError - django_request.user = mock_user - - middleware_obj.process_response(django_request, django_response) - - self.assertEqual(span.attributes, expected_attributes) - def test_process_response_unfinished_child_span(self): from opencensus.ext.django import middleware diff --git a/contrib/opencensus-ext-django/version.py b/contrib/opencensus-ext-django/version.py index c3649df71..4a7cfb652 100644 --- a/contrib/opencensus-ext-django/version.py +++ b/contrib/opencensus-ext-django/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = '0.3.1' +__version__ = '0.3.2' diff --git a/docs/trace/usage.rst b/docs/trace/usage.rst index 0c0db69b2..8c9bb85c8 100644 --- a/docs/trace/usage.rst +++ b/docs/trace/usage.rst @@ -159,7 +159,7 @@ for a Flask application: .. code:: python - from opencensus.trace.ext.flask.flask_middleware import FlaskMiddleware + from opencensus.ext.flask.flask_middleware import FlaskMiddleware app = flask.Flask(__name__) @@ -195,7 +195,7 @@ requests will be automatically traced. .. code:: python - from opencensus.trace.ext.flask.flask_middleware import FlaskMiddleware + from opencensus.ext.flask.flask_middleware import FlaskMiddleware app = flask.Flask(__name__) @@ -208,22 +208,13 @@ Django ~~~~~~ For tracing Django requests, you will need to add the following line to -the ``MIDDLEWARE_CLASSES`` section in the Django ``settings.py`` file. +the ``MIDDLEWARE`` section in the Django ``settings.py`` file. .. code:: python - MIDDLEWARE_CLASSES = [ + MIDDLEWARE = [ ... - 'opencensus.trace.ext.django.middleware.OpencensusMiddleware', - ] - -And add this line to the ``INSTALLED_APPS`` section: - -.. code:: python - - INSTALLED_APPS = [ - ... - 'opencensus.trace.ext.django', + 'opencensus.ext.django.middleware.OpencensusMiddleware', ] You can configure the sampler, exporter, propagator using the ``OPENCENSUS_TRACE`` setting in @@ -265,7 +256,7 @@ traced. def main(global_config, **settings): config = Configurator(settings=settings) - config.add_tween('opencensus.trace.ext.pyramid' + config.add_tween('opencensus.ext.pyramid' '.pyramid_middleware.OpenCensusTweenFactory') To configure the sampler, exporter, and propagator, pass the instances diff --git a/tests/system/trace/django/app/settings.py b/tests/system/trace/django/app/settings.py index b77d2b2b5..3bba77f31 100644 --- a/tests/system/trace/django/app/settings.py +++ b/tests/system/trace/django/app/settings.py @@ -16,8 +16,6 @@ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os -import django - BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = 'secret_key_for_test' @@ -31,29 +29,13 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'opencensus.ext.django', ) -if django.VERSION >= (1, 10): - MIDDLEWARE = ( - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', - 'django.middleware.security.SecurityMiddleware', - 'opencensus.ext.django.middleware.OpencensusMiddleware', - ) - -# Middleware interface for Django version before 1.10 -MIDDLEWARE_CLASSES = ( +MIDDLEWARE = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', diff --git a/tests/system/trace/django/app/urls.py b/tests/system/trace/django/app/urls.py index b111d1894..9eda96a51 100644 --- a/tests/system/trace/django/app/urls.py +++ b/tests/system/trace/django/app/urls.py @@ -27,14 +27,14 @@ 1. Add an import: from blog import urls as blog_urls 2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls)) """ -from django.conf.urls import include, url +from django.conf.urls import url from django.contrib import admin import app.views urlpatterns = [ - url(r'^admin/', include(admin.site.urls)), + url(r'^admin/', admin.site.urls), url(r'^$', app.views.home), url(r'^greetings$', app.views.greetings), url(r'^_ah/health$', app.views.health_check),