Beispiel #1
0
    def create_from_record(self, record, **kwargs):
        """
        Creates an error log for a ``logging`` module ``record`` instance.
        """
        for k in ('url', 'view', 'request', 'data'):
            if not kwargs.get(k):
                kwargs[k] = record.__dict__.get(k)

        kwargs.update({
            'logger': record.name,
            'level': record.levelno,
            'message': force_unicode(record.msg),
            'server_name': conf.NAME,
        })

        # construct the checksum with the unparsed message
        kwargs['checksum'] = construct_checksum(**kwargs)

        # save the message with included formatting
        kwargs['message'] = record.getMessage()

        # If there's no exception being processed, exc_info may be a 3-tuple of None
        # http://docs.python.org/library/sys.html#sys.exc_info
        if record.exc_info and all(record.exc_info):
            return self.create_from_exception(record.exc_info, **kwargs)

        return self.process(traceback=record.exc_text, **kwargs)
Beispiel #2
0
    def process(self, **kwargs):
        from sentry.helpers import get_filters

        kwargs.setdefault('level', logging.ERROR)
        kwargs.setdefault('server_name', conf.NAME)

        if 'checksum' not in kwargs:
            checksum = construct_checksum(**kwargs)
        else:
            checksum = kwargs['checksum']

        if conf.THRASHING_TIMEOUT and conf.THRASHING_LIMIT:
            cache_key = 'sentry:%s:%s' % (kwargs.get('class_name') or '', checksum)
            added = cache.add(cache_key, 1, conf.THRASHING_TIMEOUT)
            if not added:
                try:
                    thrash_count = cache.incr(cache_key)
                except (KeyError, ValueError):
                    # cache.incr can fail. Assume we aren't thrashing yet, and
                    # if we are, hope that the next error has a successful
                    # cache.incr call.
                    thrash_count = 0
                if thrash_count > conf.THRASHING_LIMIT:
                    return

        for filter_ in get_filters():
            kwargs = filter_(None).process(kwargs) or kwargs
        
        # Make sure all additional data is coerced
        if 'data' in kwargs:
            kwargs['data'] = transform(kwargs['data'])

        return self.send(**kwargs)
Beispiel #3
0
    def process(self, **kwargs):
        from sentry.helpers import get_filters

        for filter_ in get_filters():
            kwargs = filter_(None).process(kwargs) or kwargs

        kwargs.setdefault('level', logging.ERROR)
        kwargs.setdefault('server_name', settings.NAME)

        checksum = construct_checksum(**kwargs)

        if settings.THRASHING_TIMEOUT and settings.THRASHING_LIMIT:
            cache_key = 'sentry:%s:%s' % (kwargs.get('class_name'), checksum)
            added = cache.add(cache_key, 1, settings.THRASHING_TIMEOUT)
            if not added:
                try:
                    thrash_count = cache.incr(cache_key)
                except ValueError:
                    # cache.incr can fail. Assume we aren't thrashing yet, and
                    # if we are, hope that the next error has a successful
                    # cache.incr call.
                    thrash_count = 0
                if thrash_count > settings.THRASHING_LIMIT:
                    return

        return self.send(**kwargs)
Beispiel #4
0
    def from_kwargs(self, **kwargs):
        from sentry.models import Message, GroupedMessage, FilterValue

        URL_MAX_LENGTH = Message._meta.get_field_by_name("url")[0].max_length
        now = datetime.datetime.now()

        view = kwargs.pop("view", None)
        logger_name = kwargs.pop("logger", "root")
        url = kwargs.pop("url", None)
        server_name = kwargs.pop("server_name", conf.CLIENT)
        site = kwargs.pop("site", None)
        data = kwargs.pop("data", {}) or {}

        if url:
            data["url"] = url
            url = url[:URL_MAX_LENGTH]

        checksum = construct_checksum(**kwargs)

        mail = False
        try:
            group, created = GroupedMessage.objects.get_or_create(
                view=view, logger=logger_name, checksum=checksum, defaults=kwargs
            )
            if not created:
                GroupedMessage.objects.filter(pk=group.pk).update(
                    times_seen=models.F("times_seen") + 1, status=0, last_seen=now
                )
                # HACK: maintain appeared state
                if group.status == 1:
                    mail = True
                group.status = 0
                group.last_seen = now
                group.times_seen += 1
                signals.post_save.send(sender=GroupedMessage, instance=group, created=False)
            else:
                mail = True
            instance = Message.objects.create(
                view=view,
                logger=logger_name,
                data=data,
                url=url,
                server_name=server_name,
                site=site,
                checksum=checksum,
                group=group,
                **kwargs
            )
            if server_name:
                FilterValue.objects.get_or_create(key="server_name", value=server_name)
            if site:
                FilterValue.objects.get_or_create(key="site", value=site)
            if logger_name:
                FilterValue.objects.get_or_create(key="logger", value=logger_name)
        except Exception, exc:
            # TODO: should we mail admins when there are failures?
            try:
                logger.exception(u"Unable to process log entry: %s" % (exc,))
            except Exception, exc:
                warnings.warn(u"Unable to process log entry: %s" % (exc,))
Beispiel #5
0
    def process(self, **kwargs):
        from sentry.helpers import get_filters

        kwargs.setdefault("level", logging.ERROR)
        kwargs.setdefault("server_name", conf.NAME)

        checksum = construct_checksum(**kwargs)

        if conf.THRASHING_TIMEOUT and conf.THRASHING_LIMIT:
            cache_key = "sentry:%s:%s" % (kwargs.get("class_name") or "", checksum)
            added = cache.add(cache_key, 1, conf.THRASHING_TIMEOUT)
            try:
                if not added and cache.incr(cache_key) > conf.THRASHING_LIMIT:
                    return
            except KeyError:
                pass

        for filter_ in get_filters():
            kwargs = filter_(None).process(kwargs) or kwargs

        # Make sure all additional data is coerced
        if "data" in kwargs:
            kwargs["data"] = transform(kwargs["data"])

        return self.send(**kwargs)
Beispiel #6
0
    def create_from_record(self, record, **kwargs):
        """
        Creates an error log for a ``logging`` module ``record`` instance.
        """
        for k in ('url', 'view', 'request', 'data'):
            if not kwargs.get(k):
                kwargs[k] = record.__dict__.get(k)

        kwargs.update({
            'logger': record.name,
            'level': record.levelno,
            'message': force_unicode(record.msg),
            'server_name': conf.NAME,
        })

        # construct the checksum with the unparsed message
        kwargs['checksum'] = construct_checksum(**kwargs)

        # save the message with included formatting
        kwargs['message'] = record.getMessage()

        # If there's no exception being processed, exc_info may be a 3-tuple of None
        # http://docs.python.org/library/sys.html#sys.exc_info
        if record.exc_info and all(record.exc_info):
            return self.create_from_exception(record.exc_info, **kwargs)

        return self.process(
            traceback=record.exc_text,
            **kwargs
        )
Beispiel #7
0
    def process(self, **kwargs):
        from sentry.helpers import get_filters

        kwargs.setdefault('level', logging.ERROR)
        kwargs.setdefault('server_name', conf.NAME)

        if 'checksum' not in kwargs:
            checksum = construct_checksum(**kwargs)
        else:
            checksum = kwargs['checksum']

        if conf.THRASHING_TIMEOUT and conf.THRASHING_LIMIT:
            cache_key = 'sentry:%s:%s' % (kwargs.get('class_name') or '', checksum)
            added = cache.add(cache_key, 1, conf.THRASHING_TIMEOUT)
            try:
                if not added and cache.incr(cache_key) > conf.THRASHING_LIMIT:
                    return
            except KeyError:
                pass

        for filter_ in get_filters():
            kwargs = filter_(None).process(kwargs) or kwargs
        
        # Make sure all additional data is coerced
        if 'data' in kwargs:
            kwargs['data'] = transform(kwargs['data'])

        return self.send(**kwargs)
Beispiel #8
0
    def process(self, **kwargs):
        from sentry.helpers import get_filters

        for filter_ in get_filters():
            kwargs = filter_(None).process(kwargs) or kwargs

        kwargs.setdefault('level', logging.ERROR)
        kwargs.setdefault('server_name', socket.gethostname())

        checksum = construct_checksum(**kwargs)

        if settings.THRASHING_TIMEOUT and settings.THRASHING_LIMIT:
            cache_key = 'sentry:%s:%s' % (kwargs.get('class_name'), checksum)
            added = cache.add(cache_key, 1, settings.THRASHING_TIMEOUT)
            if not added and cache.incr(cache_key) > settings.THRASHING_LIMIT:
                return

        if settings.REMOTE_URL:
            data = {
                'data': base64.b64encode(pickle.dumps(transform(kwargs)).encode('zlib')),
                'key': settings.KEY,
            }
            req = urllib2.Request(settings.REMOTE_URL, urllib.urlencode(data))

            try:
                response = urllib2.urlopen(req, None, settings.REMOTE_TIMEOUT).read()
            except urllib2.URLError, e:
                logger.exception('Unable to reach Sentry log server')
            except urllib2.HTTPError, e:
                logger.exception('Unable to reach Sentry log server', extra={'body': e.read()})
Beispiel #9
0
    def from_kwargs(self, **kwargs):
        from sentry.models import Message, GroupedMessage, FilterValue
        
        URL_MAX_LENGTH = Message._meta.get_field_by_name('url')[0].max_length
        now = datetime.datetime.now()

        view = kwargs.pop('view', None)
        logger_name = kwargs.pop('logger', 'root')
        url = kwargs.pop('url', None)
        server_name = kwargs.pop('server_name', settings.CLIENT)
        data = kwargs.pop('data', {}) or {}

        if url:
            data['url'] = url
            url = url[:URL_MAX_LENGTH]

        checksum = construct_checksum(**kwargs)

        mail = False
        try:
            group, created = GroupedMessage.objects.get_or_create(
                view=view,
                logger=logger_name,
                checksum=checksum,
                defaults=kwargs
            )
            if not created:
                GroupedMessage.objects.filter(pk=group.pk).update(
                    times_seen=models.F('times_seen') + 1,
                    status=0,
                    last_seen=now,
                )
                # HACK: maintain appeared state
                if group.status == 1:
                    mail = True
                group.status = 0
                group.last_seen = now
                group.times_seen += 1
                signals.post_save.send(sender=GroupedMessage, instance=group, created=False)
            else: 
                mail = True
            instance = Message.objects.create(
                view=view,
                logger=logger_name,
                data=data,
                url=url,
                server_name=server_name,
                checksum=checksum,
                group=group,
                **kwargs
            )
            FilterValue.objects.get_or_create(key='server_name', value=server_name)
            FilterValue.objects.get_or_create(key='logger', value=logger_name)
        except Exception, exc:
            # TODO: should we mail admins when there are failures?
            try:
                logger.exception(u'Unable to process log entry: %s' % (exc,))
            except Exception, exc:
                warnings.warn(u'Unable to process log entry: %s' % (exc,))
Beispiel #10
0
    def process(self, **kwargs):
        from sentry.helpers import get_filters

        kwargs.setdefault('level', logging.ERROR)
        kwargs.setdefault('server_name', conf.NAME)

        # save versions of all installed apps
        if 'data' not in kwargs or '__sentry__' not in (kwargs['data'] or {}):
            if kwargs.get('data') is None:
                kwargs['data'] = {}
            kwargs['data']['__sentry__'] = {}

        versions = get_versions()
        kwargs['data']['__sentry__']['versions'] = versions

        if kwargs.get('view'):
            # get list of modules from right to left
            parts = kwargs['view'].split('.')
            module_list = ['.'.join(parts[:idx]) for idx in xrange(1, len(parts)+1)][::-1]
            version = None
            for m in module_list:
                if m in versions:
                    version = versions[m]

            # store our "best guess" for application version
            if version:
                kwargs['data']['__sentry__']['version'] = version

        if 'checksum' not in kwargs:
            checksum = construct_checksum(**kwargs)
        else:
            checksum = kwargs['checksum']

        if conf.THRASHING_TIMEOUT and conf.THRASHING_LIMIT:
            cache_key = 'sentry:%s:%s' % (kwargs.get('class_name') or '', checksum)
            added = cache.add(cache_key, 1, conf.THRASHING_TIMEOUT)
            if not added:
                try:
                    thrash_count = cache.incr(cache_key)
                except (KeyError, ValueError):
                    # cache.incr can fail. Assume we aren't thrashing yet, and
                    # if we are, hope that the next error has a successful
                    # cache.incr call.
                    thrash_count = 0
                if thrash_count > conf.THRASHING_LIMIT:
                    return

        for filter_ in get_filters():
            kwargs = filter_(None).process(kwargs) or kwargs
        
        # Make sure all data is coerced
        kwargs = transform(kwargs)

        return self.send(**kwargs)
Beispiel #11
0
    def _create(self, **defaults):
        from models import Message, GroupedMessage
        
        URL_MAX_LENGTH = Message._meta.get_field_by_name('url')[0].max_length
        
        view = defaults.pop('view', None)
        logger_name = defaults.pop('logger', 'root')
        url = defaults.pop('url', None)

        data = defaults.pop('data', {}) or {}
        if url:
            data['url'] = url
            url = url[:URL_MAX_LENGTH]

        instance = Message(
            view=view,
            logger=logger_name,
            data=data,
            url=url,
            server_name=socket.gethostname(),
            **defaults
        )
        instance.checksum = construct_checksum(instance)
        
        if settings.THRASHING_TIMEOUT and settings.THRASHING_LIMIT:
            cache_key = 'sentry:%s:%s' % (instance.class_name, instance.checksum)
            added = cache.add(cache_key, 1, settings.THRASHING_TIMEOUT)
            if not added and cache.incr(cache_key) > settings.THRASHING_LIMIT:
                return

        try:
            group, created = GroupedMessage.objects.get_or_create(
                view=view,
                logger=logger_name,
                checksum=instance.checksum,
                defaults=defaults
            )
            if not created:
                GroupedMessage.objects.filter(pk=group.pk).update(
                    times_seen=models.F('times_seen') + 1,
                    status=0,
                    last_seen=datetime.datetime.now(),
                )
            instance.group = group
            instance.save()
        except Exception, exc:
            try:
                logger.exception(u'Unable to process log entry: %s' % (exc,))
            except Exception, exc:
                warnings.warn(u'Unable to process log entry: %s' % (exc,))
Beispiel #12
0
    def create_from_record(self, record, **kwargs):
        """
        Creates an error log for a `logging` module `record` instance.
        """
        for k in ('url', 'view', 'data'):
            if k not in kwargs:
                kwargs[k] = record.__dict__.get(k)
        
        request = getattr(record, 'request', None)
        if request:
            if not kwargs.get('data'):
                kwargs['data'] = {}
            kwargs['data'].update(dict(
                META=request.META,
                POST=request.POST,
                GET=request.GET,
                COOKIES=request.COOKIES,
                raw_post_data=request.raw_post_data,
            ))

            if not kwargs.get('url'):
                kwargs['url'] = request.build_absolute_uri()
        
        kwargs.update({
            'logger': record.name,
            'level': record.levelno,
            'message': force_unicode(record.msg),
            'server_name': conf.NAME,
        })
        
        # construct the checksum with the unparsed message
        kwargs['checksum'] = construct_checksum(**kwargs)
        
        # save the message with included formatting
        kwargs['message'] = record.getMessage()
        
        # If there's no exception being processed, exc_info may be a 3-tuple of None
        # http://docs.python.org/library/sys.html#sys.exc_info
        if record.exc_info and all(record.exc_info):
            return self.create_from_exception(record.exc_info, **kwargs)

        return self.process(
            traceback=record.exc_text,
            **kwargs
        )
Beispiel #13
0
    def process(self, **kwargs):
        from sentry.helpers import get_filters

        for filter_ in get_filters():
            kwargs = filter_(None).process(kwargs) or kwargs

        kwargs.setdefault("level", logging.ERROR)
        kwargs.setdefault("server_name", settings.NAME)

        checksum = construct_checksum(**kwargs)

        if settings.THRASHING_TIMEOUT and settings.THRASHING_LIMIT:
            cache_key = "sentry:%s:%s" % (kwargs.get("class_name"), checksum)
            added = cache.add(cache_key, 1, settings.THRASHING_TIMEOUT)
            if not added and cache.incr(cache_key) > settings.THRASHING_LIMIT:
                return

        return self.send(**kwargs)
Beispiel #14
0
    def process(self, **kwargs):
        from sentry.helpers import get_filters

        kwargs.setdefault('level', logging.ERROR)
        kwargs.setdefault('server_name', settings.NAME)

        checksum = construct_checksum(**kwargs)

        if settings.THRASHING_TIMEOUT and settings.THRASHING_LIMIT:
            cache_key = 'sentry:%s:%s' % (kwargs.get('class_name'), checksum)
            added = cache.add(cache_key, 1, settings.THRASHING_TIMEOUT)
            if not added and cache.incr(cache_key) > settings.THRASHING_LIMIT:
                return

        for filter_ in get_filters():
            kwargs = filter_(None).process(kwargs) or kwargs

        # Make sure all additional data is coerced
        if 'data' in kwargs:
            kwargs['data'] = transform(kwargs['data'])

        return self.send(**kwargs)
Beispiel #15
0
    def create_from_record(self, record, **kwargs):
        """
        Creates an error log for a `logging` module `record` instance.
        """
        for k in ("url", "view", "data"):
            if k not in kwargs:
                kwargs[k] = record.__dict__.get(k)

        request = getattr(record, "request", None)
        if request:
            if not kwargs.get("data"):
                kwargs["data"] = {}
            kwargs["data"].update(dict(META=request.META, POST=request.POST, GET=request.GET, COOKIES=request.COOKIES))

            if not kwargs.get("url"):
                kwargs["url"] = request.build_absolute_uri()

        kwargs.update(
            {
                "logger": record.name,
                "level": record.levelno,
                "message": force_unicode(record.msg),
                "server_name": conf.NAME,
            }
        )

        # construct the checksum with the unparsed message
        kwargs["checksum"] = construct_checksum(**kwargs)

        # save the message with included formatting
        kwargs["message"] = record.getMessage()

        # If there's no exception being processed, exc_info may be a 3-tuple of None
        # http://docs.python.org/library/sys.html#sys.exc_info
        if record.exc_info and all(record.exc_info):
            return self.create_from_exception(record.exc_info, **kwargs)

        return self.process(traceback=record.exc_text, **kwargs)
Beispiel #16
0
    def process(self, **kwargs):
        "Processes the message before passing it on to the server"
        from sentry.helpers import get_filters

        if kwargs.get('data'):
            # Ensure we're not changing the original data which was passed
            # to Sentry
            kwargs['data'] = kwargs['data'].copy()

        request = kwargs.pop('request', None)
        if request:
            if not kwargs.get('data'):
                kwargs['data'] = {}

            if not request.POST and request.raw_post_data:
                post_data = request.raw_post_data
            else:
                post_data = request.POST

            kwargs['data'].update(dict(
                META=request.META,
                POST=post_data,
                GET=request.GET,
                COOKIES=request.COOKIES,
                REQUEST_USER=request.user,
            ))

            if not kwargs.get('url'):
                kwargs['url'] = request.build_absolute_uri()

        kwargs.setdefault('level', logging.ERROR)
        kwargs.setdefault('server_name', conf.NAME)

        # save versions of all installed apps
        if 'data' not in kwargs or '__sentry__' not in (kwargs['data'] or {}):
            if kwargs.get('data') is None:
                kwargs['data'] = {}
            kwargs['data']['__sentry__'] = {}

        versions = get_versions()
        kwargs['data']['__sentry__']['versions'] = versions

        # Shorten lists/strings
        for k, v in kwargs['data'].iteritems():
            if k == '__sentry__':
                continue
            kwargs['data'][k] = shorten(v)

        if kwargs.get('view'):
            # get list of modules from right to left
            parts = kwargs['view'].split('.')
            module_list = ['.'.join(parts[:idx]) for idx in xrange(1, len(parts)+1)][::-1]
            version = None
            module = None
            for m in module_list:
                if m in versions:
                    module = m
                    version = versions[m]

            # store our "best guess" for application version
            if version:
                kwargs['data']['__sentry__'].update({
                    'version': version,
                    'module': module,
                })

        if 'checksum' not in kwargs:
            checksum = construct_checksum(**kwargs)
        else:
            checksum = kwargs['checksum']

        if conf.THRASHING_TIMEOUT and conf.THRASHING_LIMIT:
            cache_key = 'sentry:%s:%s' % (kwargs.get('class_name') or '', checksum)
            added = cache.add(cache_key, 1, conf.THRASHING_TIMEOUT)
            if not added:
                try:
                    thrash_count = cache.incr(cache_key)
                except (KeyError, ValueError):
                    # cache.incr can fail. Assume we aren't thrashing yet, and
                    # if we are, hope that the next error has a successful
                    # cache.incr call.
                    thrash_count = 0
                if thrash_count > conf.THRASHING_LIMIT:
                    message_id = cache.get('%s:last_message_id' % cache_key)
                    if request:
                        # attach the sentry object to the request
                        request.sentry = {
                            'id': message_id,
                            'thrashed': True,
                        }
                    return message_id

        for filter_ in get_filters():
            kwargs = filter_(None).process(kwargs) or kwargs

        # create ID client-side so that it can be passed to application
        message_id = uuid.uuid4().hex
        kwargs['message_id'] = message_id

        # Make sure all data is coerced
        kwargs = transform(kwargs)

        self.send(**kwargs)

        if request:
            # attach the sentry object to the request
            request.sentry = {
                'id': message_id,
            }

        if conf.THRASHING_TIMEOUT and conf.THRASHING_LIMIT:
            # store the last message_id incase we hit thrashing limits
            cache.set('%s:last_message_id' % cache_key, message_id, conf.THRASHING_LIMIT+5)

        return message_id
Beispiel #17
0
    def process(self, **kwargs):
        "Processes the message before passing it on to the server"
        from sentry.helpers import get_filters

        if kwargs.get("data"):
            # Ensure we're not changing the original data which was passed
            # to Sentry
            kwargs["data"] = kwargs["data"].copy()

        request = kwargs.pop("request", None)
        if request:
            if not kwargs.get("data"):
                kwargs["data"] = {}
            kwargs["data"].update(dict(META=request.META, POST=request.POST, GET=request.GET, COOKIES=request.COOKIES))

            if not kwargs.get("url"):
                kwargs["url"] = request.build_absolute_uri()

        kwargs.setdefault("level", logging.ERROR)
        kwargs.setdefault("server_name", conf.NAME)

        # save versions of all installed apps
        if "data" not in kwargs or "__sentry__" not in (kwargs["data"] or {}):
            if kwargs.get("data") is None:
                kwargs["data"] = {}
            kwargs["data"]["__sentry__"] = {}

        versions = get_versions()
        kwargs["data"]["__sentry__"]["versions"] = versions

        if kwargs.get("view"):
            # get list of modules from right to left
            parts = kwargs["view"].split(".")
            module_list = [".".join(parts[:idx]) for idx in xrange(1, len(parts) + 1)][::-1]
            version = None
            module = None
            for m in module_list:
                if m in versions:
                    module = m
                    version = versions[m]

            # store our "best guess" for application version
            if version:
                kwargs["data"]["__sentry__"].update({"version": version, "module": module})

        if "checksum" not in kwargs:
            checksum = construct_checksum(**kwargs)
        else:
            checksum = kwargs["checksum"]

        if conf.THRASHING_TIMEOUT and conf.THRASHING_LIMIT:
            cache_key = "sentry:%s:%s" % (kwargs.get("class_name") or "", checksum)
            added = cache.add(cache_key, 1, conf.THRASHING_TIMEOUT)
            if not added:
                try:
                    thrash_count = cache.incr(cache_key)
                except (KeyError, ValueError):
                    # cache.incr can fail. Assume we aren't thrashing yet, and
                    # if we are, hope that the next error has a successful
                    # cache.incr call.
                    thrash_count = 0
                if thrash_count > conf.THRASHING_LIMIT:
                    message_id = cache.get("%s:last_message_id" % cache_key)
                    if request:
                        # attach the sentry object to the request
                        request.sentry = {"id": message_id, "thrashed": True}
                    return message_id

        for filter_ in get_filters():
            kwargs = filter_(None).process(kwargs) or kwargs

        # create ID client-side so that it can be passed to application
        message_id = uuid.uuid4().hex
        kwargs["message_id"] = message_id

        # Make sure all data is coerced
        kwargs = transform(kwargs)

        self.send(**kwargs)

        if request:
            # attach the sentry object to the request
            request.sentry = {"id": message_id}

        if conf.THRASHING_TIMEOUT and conf.THRASHING_LIMIT:
            # store the last message_id incase we hit thrashing limits
            cache.set("%s:last_message_id" % cache_key, message_id, conf.THRASHING_LIMIT + 5)

        return message_id
Beispiel #18
0
    def from_kwargs(self, **kwargs):
        from sentry.models import Message, GroupedMessage, FilterValue

        URL_MAX_LENGTH = Message._meta.get_field_by_name('url')[0].max_length
        now = timezone.now()

        view = kwargs.pop('view', None)
        logger_name = kwargs.pop('logger', 'root')
        url = kwargs.pop('url', None)
        server_name = kwargs.pop('server_name', conf.CLIENT)
        site = kwargs.pop('site', None)
        data = kwargs.pop('data', {}) or {}
        message_id = kwargs.pop('message_id', None)

        if url:
            data['url'] = url
            url = url[:URL_MAX_LENGTH]

        checksum = kwargs.pop('checksum', None)
        if not checksum:
            checksum = construct_checksum(**kwargs)

        mail = False
        try:
            kwargs['data'] = {}

            if 'url' in data:
                kwargs['data']['url'] = data['url']
            if 'version' in data.get('__sentry__', {}):
                kwargs['data']['version'] = data['__sentry__']['version']
            if 'module' in data.get('__sentry__', {}):
                kwargs['data']['module'] = data['__sentry__']['module']

            group, created = GroupedMessage.objects.get_or_create(
                view=view,
                logger=logger_name,
                checksum=checksum,
                # we store some sample data for rendering
                defaults=kwargs
            )
            kwargs.pop('data', None)
            if not created:
                GroupedMessage.objects.filter(pk=group.pk).update(
                    times_seen=models.F('times_seen') + 1,
                    status=0,
                    last_seen=now,
                )
                # HACK: maintain appeared state
                if group.status == 1:
                    mail = True
                group.status = 0
                group.last_seen = now
                group.times_seen += 1
                signals.post_save.send(sender=GroupedMessage, instance=group, created=False)
            else:
                mail = True
            instance = Message.objects.create(
                message_id=message_id,
                view=view,
                logger=logger_name,
                data=data,
                url=url,
                server_name=server_name,
                site=site,
                checksum=checksum,
                group=group,
                **kwargs
            )
            if server_name:
                FilterValue.objects.get_or_create(key='server_name', value=server_name)
            if site:
                FilterValue.objects.get_or_create(key='site', value=site)
            if logger_name:
                FilterValue.objects.get_or_create(key='logger', value=logger_name)
        except Exception, exc:
            # TODO: should we mail admins when there are failures?
            try:
                logger.exception(u'Unable to process log entry: %s' % (exc,))
            except Exception, exc:
                warnings.warn(u'Unable to process log entry: %s' % (exc,))
Beispiel #19
0
 def save(self, *args, **kwargs):
     if not self.checksum:
         self.checksum = construct_checksum(**self.__dict__)
     super(Message, self).save(*args, **kwargs)
Beispiel #20
0
 def save(self, *args, **kwargs):
     if not self.checksum:
         self.checksum = construct_checksum(**self.__dict__)
     super(Message, self).save(*args, **kwargs)
Beispiel #21
0
    def from_kwargs(self, **kwargs):
        from sentry.models import Message, GroupedMessage, FilterValue

        URL_MAX_LENGTH = Message._meta.get_field_by_name('url')[0].max_length
        now = datetime.datetime.now()

        view = kwargs.pop('view', None)
        logger_name = kwargs.pop('logger', 'root')
        url = kwargs.pop('url', None)
        server_name = kwargs.pop('server_name', conf.CLIENT)
        site = kwargs.pop('site', None)
        request_user = kwargs.pop('request_user', None)
        data = kwargs.pop('data', {}) or {}
        message_id = kwargs.pop('message_id', None)

        if url:
            data['url'] = url
            url = url[:URL_MAX_LENGTH]

        checksum = kwargs.pop('checksum', None)
        if not checksum:
            checksum = construct_checksum(**kwargs)

        mail = False
        try:
            kwargs['data'] = {}

            if 'url' in data:
                kwargs['data']['url'] = data['url']
            if 'version' in data.get('__sentry__', {}):
                kwargs['data']['version'] = data['__sentry__']['version']
            if 'module' in data.get('__sentry__', {}):
                kwargs['data']['module'] = data['__sentry__']['module']

            group, created = GroupedMessage.objects.get_or_create(
                view=view,
                logger=logger_name,
                checksum=checksum,
                # we store some sample data for rendering
                defaults=kwargs)
            kwargs.pop('data', None)
            if not created:
                # HACK: maintain appeared state
                if group.status == 1:
                    mail = True
                group.status = 0
                group.last_seen = now
                group.times_seen += 1
                GroupedMessage.objects.filter(pk=group.pk).update(
                    times_seen=models.F('times_seen') + 1,
                    status=0,
                    last_seen=now,
                    score=ScoreClause(group),
                )
                signals.post_save.send(sender=GroupedMessage,
                                       instance=group,
                                       created=False)
            else:
                GroupedMessage.objects.filter(pk=group.pk).update(
                    score=ScoreClause(group), )
                mail = True
            instance = Message.objects.create(message_id=message_id,
                                              view=view,
                                              logger=logger_name,
                                              data=data,
                                              url=url,
                                              server_name=server_name,
                                              site=site,
                                              request_user=request_user,
                                              checksum=checksum,
                                              group=group,
                                              **kwargs)
            if server_name:
                FilterValue.objects.get_or_create(key='server_name',
                                                  value=server_name)
            if site:
                FilterValue.objects.get_or_create(key='site', value=site)
            if request_user:
                FilterValue.objects.get_or_create(key='request_user',
                                                  value=request_user)
            if logger_name:
                FilterValue.objects.get_or_create(key='logger',
                                                  value=logger_name)
        except Exception, exc:
            # TODO: should we mail admins when there are failures?
            try:
                logger.exception(u'Unable to process log entry: %s' % (exc, ))
            except Exception, exc:
                warnings.warn(u'Unable to process log entry: %s' % (exc, ))
Beispiel #22
0
    def from_kwargs(self, **kwargs):
        from sentry.models import Message, GroupedMessage, FilterValue

        URL_MAX_LENGTH = Message._meta.get_field_by_name('url')[0].max_length
        now = datetime.datetime.now()

        view = kwargs.pop('view', None)
        logger_name = kwargs.pop('logger', 'root')
        url = kwargs.pop('url', None)
        server_name = kwargs.pop('server_name', settings.CLIENT)
        site = kwargs.pop('site', None)
        data = kwargs.pop('data', {}) or {}

        if url:
            data['url'] = url
            url = url[:URL_MAX_LENGTH]

        checksum = construct_checksum(**kwargs)

        mail = False
        try:
            group, created = GroupedMessage.objects.get_or_create(
                view=view,
                logger=logger_name,
                checksum=checksum,
                defaults=kwargs)
            if not created:
                GroupedMessage.objects.filter(pk=group.pk).update(
                    times_seen=models.F('times_seen') + 1,
                    status=0,
                    last_seen=now,
                )
                # HACK: maintain appeared state
                if group.status == 1:
                    mail = True
                group.status = 0
                group.last_seen = now
                group.times_seen += 1
                signals.post_save.send(sender=GroupedMessage,
                                       instance=group,
                                       created=False)
            else:
                mail = True
            instance = Message.objects.create(view=view,
                                              logger=logger_name,
                                              data=data,
                                              url=url,
                                              server_name=server_name,
                                              site=site,
                                              checksum=checksum,
                                              group=group,
                                              **kwargs)
            if server_name:
                FilterValue.objects.get_or_create(key='server_name',
                                                  value=server_name)
            if site:
                FilterValue.objects.get_or_create(key='site', value=site)
            if logger_name:
                FilterValue.objects.get_or_create(key='logger',
                                                  value=logger_name)
        except Exception, exc:
            # TODO: should we mail admins when there are failures?
            try:
                logger.exception(u'Unable to process log entry: %s' % (exc, ))
            except Exception, exc:
                warnings.warn(u'Unable to process log entry: %s' % (exc, ))
Beispiel #23
0
    def process(self, **kwargs):
        "Processes the message before passing it on to the server"
        from sentry.helpers import get_filters

        if kwargs.get('data'):
            # Ensure we're not changing the original data which was passed
            # to Sentry
            kwargs['data'] = kwargs['data'].copy()

        request = kwargs.pop('request', None)
        if request:
            if not kwargs.get('data'):
                kwargs['data'] = {}
            kwargs['data'].update(
                dict(
                    META=request.META,
                    POST=request.POST,
                    GET=request.GET,
                    COOKIES=request.COOKIES,
                ))

            if not kwargs.get('url'):
                kwargs['url'] = request.build_absolute_uri()

        kwargs.setdefault('level', logging.ERROR)
        kwargs.setdefault('server_name', conf.NAME)

        # save versions of all installed apps
        if 'data' not in kwargs or '__sentry__' not in (kwargs['data'] or {}):
            if kwargs.get('data') is None:
                kwargs['data'] = {}
            kwargs['data']['__sentry__'] = {}

        versions = get_versions()
        kwargs['data']['__sentry__']['versions'] = versions

        if kwargs.get('view'):
            # get list of modules from right to left
            parts = kwargs['view'].split('.')
            module_list = [
                '.'.join(parts[:idx]) for idx in xrange(1,
                                                        len(parts) + 1)
            ][::-1]
            version = None
            module = None
            for m in module_list:
                if m in versions:
                    module = m
                    version = versions[m]

            # store our "best guess" for application version
            if version:
                kwargs['data']['__sentry__'].update({
                    'version': version,
                    'module': module,
                })

        if 'checksum' not in kwargs:
            checksum = construct_checksum(**kwargs)
        else:
            checksum = kwargs['checksum']

        if conf.THRASHING_TIMEOUT and conf.THRASHING_LIMIT:
            cache_key = 'sentry:%s:%s' % (kwargs.get('class_name')
                                          or '', checksum)
            added = cache.add(cache_key, 1, conf.THRASHING_TIMEOUT)
            if not added:
                try:
                    thrash_count = cache.incr(cache_key)
                except (KeyError, ValueError):
                    # cache.incr can fail. Assume we aren't thrashing yet, and
                    # if we are, hope that the next error has a successful
                    # cache.incr call.
                    thrash_count = 0
                if thrash_count > conf.THRASHING_LIMIT:
                    message_id = cache.get('%s:last_message_id' % cache_key)
                    if request:
                        # attach the sentry object to the request
                        request.sentry = {
                            'id': message_id,
                            'thrashed': True,
                        }
                    return message_id

        for filter_ in get_filters():
            kwargs = filter_(None).process(kwargs) or kwargs

        # create ID client-side so that it can be passed to application
        message_id = uuid.uuid4().hex
        kwargs['message_id'] = message_id

        # Make sure all data is coerced
        kwargs = transform(kwargs)

        self.send(**kwargs)

        if request:
            # attach the sentry object to the request
            request.sentry = {
                'id': message_id,
            }

        if conf.THRASHING_TIMEOUT and conf.THRASHING_LIMIT:
            # store the last message_id incase we hit thrashing limits
            cache.set('%s:last_message_id' % cache_key, message_id,
                      conf.THRASHING_LIMIT + 5)

        return message_id
Beispiel #24
0
    def from_kwargs(self, **kwargs):
        from sentry.models import Message, GroupedMessage, FilterValue
        URL_MAX_LENGTH = Message._meta.get_field_by_name('url')[0].max_length

        name = kwargs.pop('name', None)
        message_type = kwargs.pop("message_type", conf.LOG)
        project = kwargs.pop("project")

        site = kwargs.pop('site', None)
        test_result = kwargs.pop('test_result', None)

        # Log specific
        logger_name = kwargs.pop('logger', '')
        url = kwargs.pop('url', None)
        data = kwargs.pop('data', {}) or {}
        if url:
            data['url'] = url
            url = url[:URL_MAX_LENGTH]

        checksum = construct_checksum(**kwargs)
        mail = False
        try:
            if 'url' in data:
                kwargs['data'] = {'url': data['url']}
            group, created = self._get_or_create_group_message(message_type, name,
                                                               checksum, project,
                                                               test_result, kwargs)
            kwargs.pop('data', None)
            if not created:
                self._update_counters(group)
            else: 
                mail = True

            instance = Message.objects.create(
                name=name,
                message_type=message_type,
                project=project,
                checksum=checksum,
                group=group,

                logger=logger_name,
                data=data,
                url=url,
                site=site,
                test_result=test_result,
                **kwargs
            )
            if site:
                print FilterValue.objects.all()
                
                FilterValue.objects.get_or_create(key='site__servername', value=site.servername, label=site.servername)
#            if site:
#                FilterValue.objects.get_or_create(key='site', value=site.id, label=site.name)
            if site:
                FilterValue.objects.get_or_create(key='project', value=project.id, label=project.name)
            if logger_name:
                FilterValue.objects.get_or_create(key='logger', value=logger_name, label=logger_name)
            if test_result:
                FilterValue.objects.get_or_create(key='test_result', value=test_result, label=test_result)
        except Exception, exc:
            # TODO: should we mail admins when there are failures?
            try:
                logger.exception(u'Unable to process log entry: %s' % (exc,))
            except Exception, exc:
                warnings.warn(u'Unable to process log entry: %s' % (exc,))