예제 #1
0
    def test_should_filter_message(self, mock_is_valid_error_message):
        TestItem = namedtuple('TestItem', 'value formatted result')

        items = [
            TestItem(
                {'type': 'UnfilteredException'},
                'UnfilteredException',
                True,
            ),
            TestItem(
                {'value': 'This is an unfiltered exception.'},
                'This is an unfiltered exception.',
                True,
            ),
            TestItem(
                {
                    'type': 'UnfilteredException',
                    'value': 'This is an unfiltered exception.'
                },
                'UnfilteredException: This is an unfiltered exception.',
                True,
            ),
            TestItem(
                {
                    'type': 'FilteredException',
                    'value': 'This is a filtered exception.'
                },
                'FilteredException: This is a filtered exception.',
                False,
            ),
        ]

        data = {
            'exception': {
                'values': [item.value for item in items]
            },
        }

        relay_config = get_full_relay_config(self.project.id)
        manager = EventManager(data,
                               project=self.project,
                               relay_config=relay_config)

        mock_is_valid_error_message.side_effect = [
            item.result for item in items
        ]

        assert manager.should_filter() == (True, FilterStatKeys.ERROR_MESSAGE)

        assert mock_is_valid_error_message.call_args_list == [
            mock.call(relay_config, item.formatted) for item in items
        ]
예제 #2
0
파일: api.py 프로젝트: wangzhichuang/sentry
    def dispatch(self, request, project_id=None, *args, **kwargs):
        helper = None
        try:
            helper = ClientApiHelper(
                agent=request.META.get('HTTP_USER_AGENT'),
                project_id=project_id,
                ip_address=request.META['REMOTE_ADDR'],
            )

            # if the project id is not directly specified get it from the authentication information
            project_id = _get_project_id_from_request(project_id, request,
                                                      self.auth_helper_cls,
                                                      helper)

            relay_config = get_full_relay_config(project_id)

            helper.context.bind_project(relay_config.project)

            if kafka_publisher is not None:
                self._publish_to_kafka(request, relay_config)

            origin = self.auth_helper_cls.origin_from_request(request)

            response = self._dispatch(request,
                                      helper,
                                      relay_config,
                                      origin=origin,
                                      *args,
                                      **kwargs)
        except APIError as e:
            context = {
                'error': force_bytes(e.msg, errors='replace'),
            }
            if e.name:
                context['error_name'] = e.name

            response = HttpResponse(json.dumps(context),
                                    content_type='application/json',
                                    status=e.http_status)
            # Set X-Sentry-Error as in many cases it is easier to inspect the headers
            response['X-Sentry-Error'] = context['error']

            if isinstance(e, APIRateLimited) and e.retry_after is not None:
                response['Retry-After'] = six.text_type(
                    int(math.ceil(e.retry_after)))

        except Exception as e:
            # TODO(dcramer): test failures are not outputting the log message
            # here
            if settings.DEBUG:
                content = traceback.format_exc()
            else:
                content = ''
            logger.exception(e)
            response = HttpResponse(content,
                                    content_type='text/plain',
                                    status=500)

        # TODO(dcramer): it'd be nice if we had an incr_multi method so
        # tsdb could optimize this
        metrics.incr('client-api.all-versions.requests', skip_internal=False)
        metrics.incr('client-api.all-versions.responses.%s' %
                     (response.status_code, ),
                     skip_internal=False)
        metrics.incr(
            'client-api.all-versions.responses.%sxx' %
            (six.text_type(response.status_code)[0], ),
            skip_internal=False,
        )

        if helper is not None and helper.context is not None and helper.context.version:
            metrics.incr(
                'client-api.v%s.requests' % (helper.context.version, ),
                skip_internal=False,
            )
            metrics.incr(
                'client-api.v%s.responses.%s' %
                (helper.context.version, response.status_code),
                skip_internal=False,
            )
            metrics.incr(
                'client-api.v%s.responses.%sxx' %
                (helper.context.version, six.text_type(
                    response.status_code)[0]),
                skip_internal=False,
            )

        return response
예제 #3
0
 def is_valid_error_message(self, value, inputs):
     self.project.update_option(
         u'sentry:{}'.format(FilterTypes.ERROR_MESSAGES), inputs)
     relay_config = get_full_relay_config(self.project.id)
     return is_valid_error_message(relay_config, value)
예제 #4
0
 def is_valid_release(self, value, inputs):
     self.project.update_option(u'sentry:{}'.format(FilterTypes.RELEASES),
                                inputs)
     relay_config = get_full_relay_config(self.project.id)
     return is_valid_release(relay_config, value)
예제 #5
0
 def is_valid_ip(self, ip, inputs):
     self.project.update_option('sentry:blacklisted_ips', inputs)
     relay_config = get_full_relay_config(self.project.id)
     return is_valid_ip(relay_config, ip)