<commit_before>"""
opbeat.contrib.django.middleware
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2011-2012 Opbeat

Large portions are
:copyright: (c) 2010 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

from __future__ import absolute_import
import time
import logging
import threading
import wrapt

from django.http.request import HttpRequest

from django.conf import settings as django_settings


try:
    from importlib import import_module
except ImportError:
    from django.utils.importlib import import_module

from opbeat.contrib.django.models import client, get_client
from opbeat.utils import disabled_due_to_debug


def _is_ignorable_404(uri):
    """
    Returns True if the given request *shouldn't* notify the site managers.
    """
    urls = getattr(django_settings, 'IGNORABLE_404_URLS', ())
    return any(pattern.search(uri) for pattern in urls)


class Opbeat404CatchMiddleware(object):
    def process_response(self, request, response):
        if response.status_code != 404 or _is_ignorable_404(request.get_full_path()):
            return response
        data = client.get_data_from_request(request)
        data.update({
            'level': logging.INFO,
            'logger': 'http404',
        })
        result = client.capture('Message', param_message={'message':'Page Not Found: %s','params':[request.build_absolute_uri()]}, data=data)
        request.opbeat = {
            'app_id': data.get('app_id', client.app_id),
            'id': client.get_ident(result),
        }
        return response


def process_request_wrapper(wrapped, instance, args, kwargs):
    response = wrapped(*args, **kwargs)
    try:
        if response is not None:
            request = None
            if args and isinstance(args[0], HttpRequest):
                request = args[0]
            elif 'request' in kwargs and isinstance(kwargs['request'], HttpRequest):
                request = kwargs['request']
            if request is not None:
                name = [type(instance).__name__, wrapped.__name__]
                if type(instance).__module__:
                    name.insert(0, type(instance).__module__)
                request.__opbeat_transaction_name = '.'.join(name)
    finally:
        return response


class OpbeatAPMMiddleware(object):
    # Create a thread local variable to store the session in for logging
    thread_local = threading.local()

    def __init__(self):
        self.client = get_client()
        if self.client.wrap_django_middleware:
            for middleware_path in settings.MIDDLEWARE_CLASSES:
                module_path, class_name = middleware_path.rsplit('.', 1)
                try:
                    module = import_module(module_path)
                    middleware_class = getattr(module, class_name)
                    if middleware_class == type(self):
                        # don't patch ourselves
                        continue
                    if hasattr(middleware_class, 'process_request'):
                        wrapt.wrap_function_wrapper(
                            middleware_class,
                            'process_request',
                            process_request_wrapper
                        )
                except ImportError:
                    client.logger.info(
                        "Can't instrument middleware %s", middleware_path
                    )

    def _get_name_from_view_func(self, view_func):
        # If no view was set we ignore the request
        module = self.thread_local.view_func.__module__

        if hasattr(self.thread_local.view_func, '__name__'):
            view_name = self.thread_local.view_func.__name__
        else:  # Fall back if there's no __name__
            view_name = self.thread_local.view_func.__class__.__name__

        return "{0}.{1}".format(module, view_name)

    def process_request(self, request):
        if disabled_due_to_debug(
            getattr(django_settings, 'OPBEAT', {}),
            getattr(django_settings, 'OPBEAT', {}).get('DEBUG', False)
        ):
            return

        self.thread_local.request_start = time.time()

    def process_view(self, request, view_func, view_args, view_kwargs):
        self.thread_local.view_func = view_func

    def process_response(self, request, response):
        try:
            if (hasattr(self.thread_local, "request_start")
                    and hasattr(response, "status_code")):
                elapsed = (time.time() - self.thread_local.request_start)*1000

                if getattr(self.thread_local, "view_func", False):
                    view_func = self._get_name_from_view_func(
                        self.thread_local.view_func)
                else:
                    view_func = getattr(
                        request,
                        '__opbeat_transaction_name',
                        ''
                    )

                status_code = response.status_code
                self.client.captureRequest(elapsed, status_code, view_func)

                self.thread_local.view_func = None
        except Exception:
            self.client.error_logger.error(
                'Exception during timing of request',
                exc_info=True,
            )
        return response


class OpbeatResponseErrorIdMiddleware(object):
    """
    Appends the X-Opbeat-ID response header for referencing a message within
    the Opbeat datastore.
    """
    def process_response(self, request, response):
        if not getattr(request, 'opbeat', None):
            return response
        response['X-Opbeat-ID'] = request.opbeat['id']
        return response


class OpbeatLogMiddleware(object):
    # Create a thread local variable to store the session in for logging
    thread = threading.local()<commit_msg>store the request timings data on the request object instead of a threadlocal<commit_after>"""
opbeat.contrib.django.middleware
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2011-2012 Opbeat

Large portions are
:copyright: (c) 2010 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

from __future__ import absolute_import
import time
import logging
import threading
import wrapt

from django.http.request import HttpRequest

from django.conf import settings as django_settings


try:
    from importlib import import_module
except ImportError:
    from django.utils.importlib import import_module

from opbeat.contrib.django.models import client, get_client
from opbeat.utils import disabled_due_to_debug


def _is_ignorable_404(uri):
    """
    Returns True if the given request *shouldn't* notify the site managers.
    """
    urls = getattr(django_settings, 'IGNORABLE_404_URLS', ())
    return any(pattern.search(uri) for pattern in urls)


class Opbeat404CatchMiddleware(object):
    def process_response(self, request, response):
        if response.status_code != 404 or _is_ignorable_404(request.get_full_path()):
            return response
        data = client.get_data_from_request(request)
        data.update({
            'level': logging.INFO,
            'logger': 'http404',
        })
        result = client.capture('Message', param_message={'message':'Page Not Found: %s','params':[request.build_absolute_uri()]}, data=data)
        request.opbeat = {
            'app_id': data.get('app_id', client.app_id),
            'id': client.get_ident(result),
        }
        return response


def process_request_wrapper(wrapped, instance, args, kwargs):
    response = wrapped(*args, **kwargs)
    try:
        if response is not None:
            request = None
            if args and isinstance(args[0], HttpRequest):
                request = args[0]
            elif 'request' in kwargs and isinstance(kwargs['request'], HttpRequest):
                request = kwargs['request']
            if request is not None:
                name = [type(instance).__name__, wrapped.__name__]
                if type(instance).__module__:
                    name.insert(0, type(instance).__module__)
                request.__opbeat_transaction_name = '.'.join(name)
    finally:
        return response


class OpbeatAPMMiddleware(object):
    def __init__(self):
        self.client = get_client()
        if self.client.wrap_django_middleware:
            for middleware_path in django_settings.MIDDLEWARE_CLASSES:
                module_path, class_name = middleware_path.rsplit('.', 1)
                try:
                    module = import_module(module_path)
                    middleware_class = getattr(module, class_name)
                    if middleware_class == type(self):
                        # don't patch ourselves
                        continue
                    if hasattr(middleware_class, 'process_request'):
                        wrapt.wrap_function_wrapper(
                            middleware_class,
                            'process_request',
                            process_request_wrapper
                        )
                except ImportError:
                    client.logger.info(
                        "Can't instrument middleware %s", middleware_path
                    )

    def _get_name_from_view_func(self, view_func):
        # If no view was set we ignore the request
        module = view_func.__module__

        if hasattr(view_func, '__name__'):
            view_name = view_func.__name__
        else:  # Fall back if there's no __name__
            view_name = view_func.__class__.__name__

        return "{0}.{1}".format(module, view_name)

    def process_request(self, request):
        if disabled_due_to_debug(
            getattr(django_settings, 'OPBEAT', {}),
            getattr(django_settings, 'OPBEAT', {}).get('DEBUG', False)
        ):
            return

        request.__opbeat_request_start = time.time()

    def process_view(self, request, view_func, view_args, view_kwargs):
        request.__opbeat_view_func = view_func

    def process_response(self, request, response):
        try:
            if (hasattr(request, "request_start")
                    and hasattr(response, "status_code")):
                elapsed = (time.time() - request.__opbeat_request_start)*1000

                if getattr(request, "__opbeat_view_func", False):
                    view_func = self._get_name_from_view_func(
                        request.__opbeat_view_func)
                else:
                    view_func = getattr(
                        request,
                        '__opbeat_transaction_name',
                        ''
                    )

                status_code = response.status_code
                self.client.captureRequest(elapsed, status_code, view_func)
        except Exception:
            self.client.error_logger.error(
                'Exception during timing of request',
                exc_info=True,
            )
        return response


class OpbeatResponseErrorIdMiddleware(object):
    """
    Appends the X-Opbeat-ID response header for referencing a message within
    the Opbeat datastore.
    """
    def process_response(self, request, response):
        if not getattr(request, 'opbeat', None):
            return response
        response['X-Opbeat-ID'] = request.opbeat['id']
        return response


class OpbeatLogMiddleware(object):
    # Create a thread local variable to store the session in for logging
    thread = threading.local()
{'fraction_non_alphanumeric': 0.09090119057964717, 'fraction_numerical': 0.006952289910489268, 'mean_word_length': 2.1178542400433487, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2640821', 'n_tokens_mistral': 3140, 'n_tokens_neox': 2979, 'n_words': 749}