def process_request(self, request):
        request._errormator_create_report = False
        request.__traceback__ = None

        environ = getattr(request, 'environ', request.META)

        ignored_slow_paths = self.appenlight_client.config.get(
            'ignore_slow_paths', [])
        if any(p in request.path for p in ignored_slow_paths):
            log.debug('appenlight.ignore_slow_path in effect')
            environ['appenlight.ignore_slow'] = True

        ignored_paths = self.appenlight_client.config.get('ignore_paths', [])
        if any(p in request.path for p in ignored_paths):
            log.debug('appenlight.ignore_path in effect')
            environ['appenlight.ignore_error'] = True

        environ['appenlight.request_id'] = str(uuid.uuid4())
        # inject client instance reference to environ
        if 'appenlight.client' not in environ:
            environ['appenlight.client'] = self.appenlight_client
        if 'appenlight.tags' not in environ:
            environ['appenlight.tags'] = {}
        if 'appenlight.extra' not in environ:
            environ['appenlight.extra'] = {}
        environ['appenlight.post_vars'] = request.POST
        appenlight_storage = get_local_storage()
        # clear out thread stats on request start
        appenlight_storage.clear()
        request.__start_time__ = default_timer()
        return None
    def process_exception(self, request, exception):
        if (not getattr(self, 'appenlight_client') or
                not self.appenlight_client.config.get('enabled')):
            return None

        environ = getattr(request, 'environ', request.META)
        if not self.appenlight_client.config['report_errors'] \
                or environ.get('appenlight.ignore_error'):
            return None
        user = getattr(request, 'user', None)
        end_time = default_timer()
        if user and user_is_authenticated(user):
            if 'appenlight.username' not in environ:
                environ['appenlight.username'] = six.text_type(user.pk)
        if not isinstance(exception, Http404):
            http_status = 500
            traceback = self.appenlight_client.get_current_traceback()
            appenlight_storage = get_local_storage()
            appenlight_storage.thread_stats[
                'main'] = end_time - request.__start_time__
            stats, slow_calls = appenlight_storage.get_thread_stats()
            self.appenlight_client.save_request_stats(
                stats, view_name=environ.get('appenlight.view_name', ''))
            self.appenlight_client.py_report(
                environ, traceback, message=None, http_status=http_status,
                start_time=datetime.utcfromtimestamp(
                    request.__start_time__),
                end_time=datetime.utcfromtimestamp(
                    end_time),
                request_stats=stats, slow_calls=slow_calls)
            del traceback
            request._errormator_create_report = True
 def process_exception(self, request, exception):
     if (not getattr(self, 'appenlight_client') or not self.appenlight_client.config.get('enabled')):
         return None
     environ = request.environ
     if not self.appenlight_client.config['report_errors'] or environ.get('appenlight.ignore_error'):
         return None
     user = getattr(request, 'user', None)
     end_time = default_timer()
     if user and user.is_authenticated():
         environ['appenlight.username'] = unicode(user.pk)
     http_status = 500
     request._errormator_create_report = True
     traceback = get_current_traceback(skip=1,
                                       show_hidden_frames=True,
                                       ignore_system_exceptions=True)
     appenlight_storage = get_local_storage(local_timing)
     appenlight_storage.thread_stats['main'] = end_time - request.__start_time__
     stats, slow_calls = appenlight_storage.get_thread_stats()
     self.appenlight_client.save_request_stats(stats, view_name=environ.get('appenlight.view_name',''))
     self.appenlight_client.py_report(environ,
                                      traceback,
                                      message=None,
                                      http_status=http_status,
                                      start_time=datetime.utcfromtimestamp(request.__start_time__),
                                      end_time=datetime.utcfromtimestamp(end_time),
                                      request_stats=stats,
                                      slow_calls=slow_calls)
     del traceback
    def process_response(self, request, response):
        try:
            return response
        finally:
            environ = getattr(request, 'environ', request.META)
            enabled = self.appenlight_client.config.get('enabled')

            if enabled and not request._errormator_create_report and not environ.get(
                    'appenlight.ignore_slow'):
                end_time = default_timer()
                user = getattr(request, 'user', None)
                http_status = response.status_code
                if user and user_is_authenticated(user):
                    if 'appenlight.username' not in environ:
                        environ['appenlight.username'] = six.text_type(user.pk)
                if (http_status == 404 and self.appenlight_client.config[
                    'report_404']):
                    request._errormator_create_report = True
                delta = timedelta(seconds=(end_time - request.__start_time__))
                appenlight_storage = get_local_storage()
                appenlight_storage.thread_stats[
                    'main'] = end_time - request.__start_time__
                stats, slow_calls = appenlight_storage.get_thread_stats()
                self.appenlight_client.save_request_stats(
                    stats, view_name=environ.get('appenlight.view_name', ''))
                if self.appenlight_client.config['slow_requests']:
                    if (delta >= self.appenlight_client.config[
                        'slow_request_time'] or slow_calls):
                        request._errormator_create_report = True
                if request._errormator_create_report:
                    self.appenlight_client.py_report(
                        environ,
                        None,
                        message=None,
                        http_status=http_status,
                        start_time=datetime.utcfromtimestamp(
                            request.__start_time__),
                        end_time=datetime.utcfromtimestamp(end_time),
                        request_stats=stats,
                        slow_calls=slow_calls)

                if self.appenlight_client.config['logging']:
                    records = self.appenlight_client.log_handlers_get_records()
                    self.appenlight_client.log_handlers_clear_records()
                    self.appenlight_client.py_log(
                        environ,
                        records=records,
                        r_uuid=environ[
                            'appenlight.request_id'],
                        created_report=request._errormator_create_report)
            if self.appenlight_client.config.get('enabled'):
                self.appenlight_client.check_if_deliver(
                    self.appenlight_client.config['force_send'] or
                    environ.get('appenlight.force_send'))
 def process_request(self, request):
     request._errormator_create_report = False
     request.__traceback__ = None
     environ = request.environ
     environ['appenlight.request_id'] = str(uuid.uuid4())
     # inject client instance reference to environ
     if 'appenlight.client' not in environ:
         environ['appenlight.client'] = self.appenlight_client
     environ['appenlight.post_vars'] = request.POST
     appenlight_storage = get_local_storage(local_timing)
     # clear out thread stats on request start
     appenlight_storage.clear()
     request.__start_time__ = default_timer()
     return None
Beispiel #6
0
 def process_request(self, request):
     request._errormator_create_report = False
     request.__traceback__ = None
     environ = request.environ
     environ['appenlight.request_id'] = str(uuid.uuid4())
     # inject client instance reference to environ
     if 'appenlight.client' not in environ:
         environ['appenlight.client'] = self.appenlight_client
     if 'appenlight.tags' not in environ:
         environ['appenlight.tags'] = {}
     if 'appenlight.extra' not in environ:
         environ['appenlight.extra'] = {}
     environ['appenlight.post_vars'] = request.POST
     appenlight_storage = get_local_storage()
     # clear out thread stats on request start
     appenlight_storage.clear()
     request.__start_time__ = default_timer()
     return None
    def __call__(self, environ, start_response):
        """Run the application and conserve the traceback frames.
        also determine if we got 404
        """
        environ['appenlight.request_id'] = str(uuid.uuid4())
        appenlight_storage = get_local_storage(local_timing)
        # clear out thread stats on request start
        appenlight_storage.clear()
        app_iter = None
        detected_data = []
        create_report = False
        traceback = None
        http_status = 200
        start_time = default_timer()

        def detect_headers(status, headers, *k, **kw):
            detected_data[:] = status[:3], headers
            return start_response(status, headers, *k, **kw)

        # inject client instance reference to environ
        if 'appenlight.client' not in environ:
            environ['appenlight.client'] = self.appenlight_client
            # some bw. compat stubs

            def local_report(message, include_traceback=True,
                             http_status=200):
                environ['appenlight.force_send'] = True

            def local_log(level, message):
                environ['appenlight.force_send'] = True

            environ['appenlight.report'] = local_report
            environ['appenlight.log'] = local_log

        try:
            app_iter = self.app(environ, detect_headers)
            return app_iter
        except Exception as exc:
            if hasattr(app_iter, 'close'):
                app_iter.close()
                # we need that here

            traceback = get_current_traceback(skip=1, show_hidden_frames=True,
                                              ignore_system_exceptions=True)
            # by default reraise exceptions for app/FW to handle
            if self.appenlight_client.config['reraise_exceptions']:
                raise
            try:
                start_response('500 INTERNAL SERVER ERROR',
                               [('Content-Type', 'text/html; charset=utf-8')])
            except Exception as exc:
                environ['wsgi.errors'].write(
                    'AppenlightWSGIWrapper middleware catched exception '
                    'in streamed response at a point where response headers '
                    'were already sent.\n')
            else:
                return 'Server Error'
        finally:
            # report 500's and 404's
            # report slowness
            end_time = default_timer()
            appenlight_storage.thread_stats['main'] = end_time - start_time
            delta = datetime.timedelta(seconds=(end_time - start_time))
            stats, slow_calls = appenlight_storage.get_thread_stats()
            if 'appenlight.view_name' not in environ:
                environ['appenlight.view_name'] = getattr(appenlight_storage,'view_name', '')
            if detected_data and detected_data[0]:
                http_status = int(detected_data[0])
            if self.appenlight_client.config['slow_requests'] and not environ.get('appenlight.ignore_slow'):
                # do we have slow calls/request ?
                if (delta >= self.appenlight_client.config['slow_request_time'] or slow_calls):
                    create_report = True
            if 'appenlight.__traceback' in environ and not environ.get('appenlight.ignore_error'):
                # get traceback gathered by pyramid tween
                traceback = environ['appenlight.__traceback']
                del environ['appenlight.__traceback']
                http_status = 500
                create_report = True
            if traceback and self.appenlight_client.config['report_errors'] and not environ.get('appenlight.ignore_error'):
                http_status = 500
                create_report = True
            elif (self.appenlight_client.config['report_404'] and http_status == 404):
                create_report = True
            if create_report:
                self.appenlight_client.py_report(environ, traceback,
                                                 message=None,
                                                 http_status=http_status,
                                                 start_time=datetime.datetime.utcfromtimestamp(start_time),
                                                 end_time=datetime.datetime.utcfromtimestamp(end_time),
                                                 request_stats=stats,
                                                 slow_calls=slow_calls)
                # dereference
                del traceback
                # force log fetching
                traceback = True
            self.appenlight_client.save_request_stats(stats, view_name=environ.get('appenlight.view_name',''))
            if self.appenlight_client.config['logging']:
                records = self.appenlight_client.log_handler.get_records()
                self.appenlight_client.log_handler.clear_records()
                self.appenlight_client.py_log(environ,
                                              records=records,
                                              r_uuid=environ['appenlight.request_id'],
                                              created_report=create_report)
                # send all data we gathered immediately at the end of request
            self.appenlight_client.check_if_deliver(self.appenlight_client.config['force_send'] or
                                                    environ.get('appenlight.force_send'))
Beispiel #8
0
    def __call__(self, environ, start_response):
        """Run the application and conserve the traceback frames.
        also determine if we got 404
        """
        environ['appenlight.request_id'] = str(uuid.uuid4())
        appenlight_storage = get_local_storage()
        # clear out thread stats on request start
        appenlight_storage.clear()
        app_iter = None
        detected_data = []
        create_report = False
        traceback = None
        http_status = 200
        start_time = default_timer()

        def detect_headers(status, headers, *k, **kw):
            detected_data[:] = status[:3], headers
            return start_response(status, headers, *k, **kw)

        # inject client instance reference to environ
        if 'appenlight.client' not in environ:
            environ['appenlight.client'] = self.appenlight_client

            # some bw. compat stubs

            def local_report(message, include_traceback=True, http_status=200):
                environ['appenlight.force_send'] = True

            def local_log(level, message):
                environ['appenlight.force_send'] = True

            environ['appenlight.report'] = local_report
            environ['appenlight.log'] = local_log
        if 'appenlight.tags' not in environ:
            environ['appenlight.tags'] = {}
        if 'appenlight.extra' not in environ:
            environ['appenlight.extra'] = {}

        try:
            app_iter = self.app(environ, detect_headers)
            return app_iter
        except Exception:
            if hasattr(app_iter, 'close'):
                app_iter.close()
                # we need that here

            traceback = self.appenlight_client.get_current_traceback()
            # by default reraise exceptions for app/FW to handle
            if self.appenlight_client.config['reraise_exceptions']:
                raise
            try:
                start_response('500 INTERNAL SERVER ERROR',
                               [('Content-Type', 'text/html; charset=utf-8')])
            except Exception:
                environ['wsgi.errors'].write(
                    'AppenlightWSGIWrapper middleware catched exception '
                    'in streamed response at a point where response headers '
                    'were already sent.\n')
            else:
                return 'Server Error'
        finally:
            # report 500's and 404's
            # report slowness
            end_time = default_timer()
            appenlight_storage.thread_stats['main'] = end_time - start_time
            delta = datetime.timedelta(seconds=(end_time - start_time))
            stats, slow_calls = appenlight_storage.get_thread_stats()
            if 'appenlight.view_name' not in environ:
                environ['appenlight.view_name'] = getattr(
                    appenlight_storage, 'view_name', '')
            if detected_data and detected_data[0]:
                http_status = int(detected_data[0])
            if self.appenlight_client.config[
                    'slow_requests'] and not environ.get(
                        'appenlight.ignore_slow'):
                # do we have slow calls/request ?
                if (delta >= self.appenlight_client.config['slow_request_time']
                        or slow_calls):
                    create_report = True
            if 'appenlight.__traceback' in environ and not environ.get(
                    'appenlight.ignore_error'):
                # get traceback gathered by pyramid tween
                traceback = environ['appenlight.__traceback']
                del environ['appenlight.__traceback']
                http_status = 500
                create_report = True
            if traceback and self.appenlight_client.config[
                    'report_errors'] and not environ.get(
                        'appenlight.ignore_error'):
                http_status = 500
                create_report = True
            elif (self.appenlight_client.config['report_404']
                  and http_status == 404):
                create_report = True
            if create_report:
                self.appenlight_client.py_report(
                    environ,
                    traceback,
                    message=None,
                    http_status=http_status,
                    start_time=datetime.datetime.utcfromtimestamp(start_time),
                    end_time=datetime.datetime.utcfromtimestamp(end_time),
                    request_stats=stats,
                    slow_calls=slow_calls)
                # dereference
                del traceback
            self.appenlight_client.save_request_stats(
                stats, view_name=environ.get('appenlight.view_name', ''))
            if self.appenlight_client.config['logging']:
                records = self.appenlight_client.log_handlers_get_records()
                self.appenlight_client.log_handlers_clear_records()
                self.appenlight_client.py_log(
                    environ,
                    records=records,
                    r_uuid=environ['appenlight.request_id'],
                    created_report=create_report)
                # send all data we gathered immediately at the end of request
            self.appenlight_client.check_if_deliver(
                self.appenlight_client.config['force_send']
                or environ.get('appenlight.force_send'))