Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.
Merged
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
8 changes: 8 additions & 0 deletions contrib/opencensus-ext-django/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 2 additions & 11 deletions contrib/opencensus-ext-django/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/census-instrumentation/opencensus-python#customization>`_
for a complete reference.
Expand Down
6 changes: 2 additions & 4 deletions contrib/opencensus-ext-django/examples/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions contrib/opencensus-ext-django/examples/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion contrib/opencensus-ext-django/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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={},
Expand Down
50 changes: 0 additions & 50 deletions contrib/opencensus-ext-django/tests/test_django_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion contrib/opencensus-ext-django/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
21 changes: 6 additions & 15 deletions docs/trace/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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__)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 1 addition & 19 deletions tests/system/trace/django/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions tests/system/trace/django/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down