Example #1
0
 def setup_logging(self):
     self.log = logging.getLogger('{}.catcher'.format(__name__))
     self.log.propagate = False
     self.log.setLevel(logging.DEBUG)
     self.log.handlers = []
     self.service = self.get_service()
     self.handler = MonitoringHandler(self.service)
     self.handler.setLevel(logging.DEBUG)
     self.log.addHandler(self.handler)
Example #2
0
 def setup_logging(self):
     self.log = logging.getLogger('{}.catcher'.format(__name__))
     self.log.propagate = False
     self.log.setLevel(logging.DEBUG)
     self.log.handlers = []
     self.service = self.get_service()
     self.handler = MonitoringHandler(self.service)
     self.handler.setLevel(logging.DEBUG)
     self.filter = MonitoringFilter(self.service, FILTER_URLS)
     self.handler.addFilter(self.filter)
     self.log.addHandler(self.handler)
Example #3
0
class MonitoringMiddleware(object):
    def __init__(self):
        self.setup_logging()

    def setup_logging(self):
        self.log = logging.getLogger('{}.catcher'.format(__name__))
        self.log.propagate = False
        self.log.setLevel(logging.DEBUG)
        self.log.handlers = []
        self.service = self.get_service()
        self.handler = MonitoringHandler(self.service)
        self.handler.setLevel(logging.DEBUG)
        self.log.addHandler(self.handler)

    def get_service(self):
        hname = getattr(settings, 'MONITORING_HOST_NAME', None) or 'localhost'
        sname = getattr(settings, 'MONITORING_SERVICE_NAME', None) or 'geonode'
        try:
            host = Host.objects.get(name=hname)
        except Host.DoesNotExist:
            host = None
        if host:
            try:
                service = Service.objects.get(host=host, name=sname)
                return service
            except Service.DoesNotExist:
                service = None

    @staticmethod
    def should_process(request):
        current = request.path

        for skip_url in settings.MONITORING_SKIP_PATHS:
            if isinstance(skip_url, types.StringTypes):
                if current.startswith(skip_url):
                    return False
            elif hasattr(skip_url, 'match'):
                if skip_url.match(current):
                    return False
        return True

    @staticmethod
    def register_event(request, event_type, resource_type, resource_name,
                       resource_id):
        m = getattr(request, '_monitoring', None)
        if not m:
            return
        events = m['events']
        events.append((
            event_type,
            resource_type,
            resource_name,
            resource_id,
        ))

    def register_request(self, request, response):
        if self.service:
            self.log.debug('request',
                           extra={
                               'request': request,
                               'response': response
                           })

    def register_exception(self, request, exception):
        if self.service:
            response = HttpResponse('')
            self.log.debug('request',
                           exc_info=exception,
                           extra={
                               'request': request,
                               'response': response
                           })

    def process_view(self, request, view_func, view_args, view_kwargs):
        m = request.resolver_match
        if m.namespace in (
                'admin',
                'monitoring',
        ):
            request._monitoring = None
            del request._monitoring

    def process_request(self, request):
        if not self.should_process(request):
            return
        utc = pytz.utc
        now = datetime.utcnow().replace(tzinfo=utc)

        # enforce session create
        if not request.session.session_key:
            request.session.create()

        meta = {
            'started': now,
            'resources': {},
            'events': [],
            'finished': None,
        }

        if settings.USER_ANALYTICS_ENABLED:
            meta.update({
                'user_identifier':
                hashlib.sha256(request.session.session_key or '').hexdigest(),
                'user_username':
                request.user.username
                if request.user.is_authenticated() else 'AnonymousUser'
            })

        request._monitoring = meta

        def register_event(event_type, resource_type, name, resource_id):
            self.register_event(request, event_type, resource_type, name,
                                resource_id)

        request.register_event = register_event

    def process_response(self, request, response):
        m = getattr(request, '_monitoring', None)
        if m is None:
            return response
        utc = pytz.utc
        now = datetime.utcnow().replace(tzinfo=utc)
        m['finished'] = now
        self.register_request(request, response)
        return response

    def process_exception(self, request, exception):
        m = getattr(request, '_monitoring', None)
        if m is None:
            return
        utc = pytz.utc
        now = datetime.utcnow().replace(tzinfo=utc)
        m['finished'] = now
        self.register_exception(request, exception)
Example #4
0
class MonitoringMiddleware(object):

    def __init__(self):
        self.setup_logging()

    def setup_logging(self):
        self.log = logging.getLogger('{}.catcher'.format(__name__))
        self.log.propagate = False
        self.log.setLevel(logging.DEBUG)
        self.log.handlers = []
        self.service = self.get_service()
        self.handler = MonitoringHandler(self.service)
        self.handler.setLevel(logging.DEBUG)
        self.filter = MonitoringFilter(self.service, FILTER_URLS)
        self.handler.addFilter(self.filter)
        self.log.addHandler(self.handler)

    def get_service(self):
        hname = getattr(settings, 'MONITORING_HOST_NAME', None) or 'localhost'
        sname = getattr(settings, 'MONITORING_SERVICE_NAME', None) or 'geonode'
        try:
            host = Host.objects.get(name=hname)
        except Host.DoesNotExist:
            host = None
        if host:
            try:
                service = Service.objects.get(host=host, name=sname)
                return service
            except Service.DoesNotExist:
                service = None

    @staticmethod
    def add_resource(request, resource_type, name):
        m = getattr(request, '_monitoring', None)
        if not m:
            return
        res = m['resources']
        res_list = res.get(resource_type) or []
        res_list.append(name)
        res[resource_type] = res_list

    def register_request(self, request, response):
        if self.service:
            self.log.debug('request', extra={'request': request, 'response': response})

    def register_exception(self, request, exception):
        if self.service:
            response = HttpResponse('')
            self.log.debug('request', exc_info=exception, extra={'request': request, 'response': response})

    def process_view(self, request, view_func, view_args, view_kwargs):
        m = request.resolver_match
        if m.namespace in ('admin', 'monitoring',):
            request._monitoring = None
            del request._monitoring

    def process_request(self, request):
        utc = pytz.utc
        now = datetime.utcnow().replace(tzinfo=utc)
        meta = {'started': now,
                'resources': {},
                'finished': None}
        request._monitoring = meta

        def add_resource(resource_type, name):
            return self.add_resource(request, resource_type, name)

        request.add_resource = add_resource

    def process_response(self, request, response):
        m = getattr(request, '_monitoring', None)
        if m is None:
            return response
        utc = pytz.utc
        now = datetime.utcnow().replace(tzinfo=utc)
        m['finished'] = now
        self.register_request(request, response)
        return response

    def process_exception(self, request, exception):
        m = getattr(request, '_monitoring', None)
        if m is None:
            return
        utc = pytz.utc
        now = datetime.utcnow().replace(tzinfo=utc)
        m['finished'] = now
        self.register_exception(request, exception)
Example #5
0
class MonitoringMiddleware(object):
    def __init__(self):
        self.setup_logging()

    def setup_logging(self):
        self.log = logging.getLogger('{}.catcher'.format(__name__))
        self.log.propagate = False
        self.log.setLevel(logging.DEBUG)
        self.log.handlers = []
        self.service = self.get_service()
        self.handler = MonitoringHandler(self.service)
        self.handler.setLevel(logging.DEBUG)
        self.filter = MonitoringFilter(self.service, FILTER_URLS)
        self.handler.addFilter(self.filter)
        self.log.addHandler(self.handler)

    def get_service(self):
        hname = getattr(settings, 'MONITORING_HOST_NAME', None) or 'localhost'
        sname = getattr(settings, 'MONITORING_SERVICE_NAME', None) or 'geonode'
        try:
            host = Host.objects.get(name=hname)
        except Host.DoesNotExist:
            host = None
        if host:
            try:
                service = Service.objects.get(host=host, name=sname)
                return service
            except Service.DoesNotExist:
                service = None

    @staticmethod
    def add_resource(request, resource_type, name):
        m = getattr(request, '_monitoring', None)
        if not m:
            return
        res = m['resources']
        res_list = res.get(resource_type) or []
        res_list.append(name)
        res[resource_type] = res_list

    def register_request(self, request, response):
        if self.service:
            self.log.debug('request',
                           extra={
                               'request': request,
                               'response': response
                           })

    def register_exception(self, request, exception):
        if self.service:
            response = HttpResponse('')
            self.log.debug('request',
                           exc_info=exception,
                           extra={
                               'request': request,
                               'response': response
                           })

    def process_view(self, request, view_func, view_args, view_kwargs):
        m = request.resolver_match
        if m.namespace in (
                'admin',
                'monitoring',
        ):
            request._monitoring = None
            del request._monitoring

    def process_request(self, request):
        utc = pytz.utc
        now = datetime.utcnow().replace(tzinfo=utc)
        meta = {'started': now, 'resources': {}, 'finished': None}
        request._monitoring = meta

        def add_resource(resource_type, name):
            return self.add_resource(request, resource_type, name)

        request.add_resource = add_resource

    def process_response(self, request, response):
        m = getattr(request, '_monitoring', None)
        if m is None:
            return response
        utc = pytz.utc
        now = datetime.utcnow().replace(tzinfo=utc)
        m['finished'] = now
        self.register_request(request, response)
        return response

    def process_exception(self, request, exception):
        m = getattr(request, '_monitoring', None)
        if m is None:
            return
        utc = pytz.utc
        now = datetime.utcnow().replace(tzinfo=utc)
        m['finished'] = now
        self.register_exception(request, exception)