Skip to content
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

Fixed support for Django 4.1 #1159

Conversation

JeremyVriens
Copy link
Contributor

This PR fixes the support for Django 4.1 according to this bug report: #1154

The issue was a missing call to init of the super class, which worked fine before, but broke as of this change in Django: django/django#15216

@lzchen
Copy link
Contributor

lzchen commented Aug 25, 2022

@JeremyVriens
Is this a breaking change for users who used MiddleWareMixin previously and didn't supply a get_response in the constructor?

@JeremyVriens
Copy link
Contributor Author

When used correctly as described in the README, it should not be as Django passes the request through all the registered middlewares (see also their documentation: https://docs.djangoproject.com/en/4.1/topics/http/middleware/). The default value of None for the get_response argument was in that sense never correct.

@lzchen lzchen merged commit 92655e1 into census-instrumentation:master Aug 26, 2022
@ghost
Copy link

ghost commented Aug 29, 2022

When a release will be available for this fix?

@oscarhermoso
Copy link

If anyone is still waiting for this fix to be released, here is a snippet you can use to patch the middleware for now:

# myapp/settings.py

MIDDLEWARE = [
    # 'opencensus.ext.django.middleware.OpencensusMiddleware',
    'myapp.middleware.PatchedOpencensusMiddleware',
]
# myapp/middleware.py

import django
from opencensus.ext.django.middleware import (
    OpencensusMiddleware,
    EXCLUDELIST_PATHS,
    EXCLUDELIST_HOSTNAMES,
    _trace_db_call
)
import six
from django.db import connection
from opencensus.common import configuration
from opencensus.trace import (
    integrations,
    print_exporter,
    samplers,
)
from opencensus.trace.propagation import trace_context_http_header_format


class PatchedOpencensusMiddleware(OpencensusMiddleware):
    def __init__(self, get_response=None):
        super(OpencensusMiddleware, self).__init__(get_response)
        settings = getattr(django.conf.settings, 'OPENCENSUS', {})
        settings = settings.get('TRACE', {})

        self.sampler = (settings.get('SAMPLER', None)
                        or samplers.ProbabilitySampler())
        if isinstance(self.sampler, six.string_types):
            self.sampler = configuration.load(self.sampler)

        self.exporter = settings.get('EXPORTER', None) or \
            print_exporter.PrintExporter()
        if isinstance(self.exporter, six.string_types):
            self.exporter = configuration.load(self.exporter)

        self.propagator = settings.get('PROPAGATOR', None) or \
            trace_context_http_header_format.TraceContextPropagator()
        if isinstance(self.propagator, six.string_types):
            self.propagator = configuration.load(self.propagator)

        self.excludelist_paths = settings.get(EXCLUDELIST_PATHS, None)

        self.excludelist_hostnames = settings.get(EXCLUDELIST_HOSTNAMES, None)

        if django.VERSION >= (2,):  # pragma: NO COVER
            connection.execute_wrappers.append(_trace_db_call)

        # pylint: disable=protected-access
        integrations.add_integration(integrations._Integrations.DJANGO)

@lzchen
Copy link
Contributor

lzchen commented Oct 17, 2022

https://github.com/census-instrumentation/opencensus-python/releases/tag/opencensus-ext-django%400.8.0 released.

@oscarhermoso
Copy link

Thanks @lzchen, not the first time you've helped me out. Super appreciated

inirudebwoy pushed a commit to inirudebwoy/opencensus-python that referenced this pull request Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants