Exemplo n.º 1
0
    def _check_request(self, application_name, intent_map):
        if application_name in settings.LEGACY_APPLICATION_LIST:
            return

        im = InstanceManagement(
            self.huskar_client, application_name, SERVICE_SUBDOMAIN)
        im.set_context(self.from_application_name, self.from_cluster_name)
        for intent, icluster_names in intent_map.iteritems():
            if not icluster_names:   # pragma: no cover  # TODO: fix
                continue
            dest_cluster_blacklist = settings.ROUTE_DEST_CLUSTER_BLACKLIST.get(
                application_name, [])
            if self.hijack_mode is self.Mode.checking:
                if icluster_names & set(dest_cluster_blacklist):
                    continue

            if len(icluster_names) > 1:
                logger.info(
                    '[%s]Unstable: %s %s -> %s %s', self.hijack_mode.value,
                    self.from_application_name, self.from_cluster_name,
                    application_name, intent_map)
                capture_message(
                    '[%s]RouteHijack unstable' % self.hijack_mode.value,
                    extra={
                        'from_application_name': (
                            self.from_application_name),
                        'from_cluster_name': self.from_cluster_name,
                        'application_name': application_name,
                        'intent_map': repr(intent_map),
                        'intent': intent,
                    })
                continue

            resolved_name = im.resolve_cluster_name(intent)
            cluster_name = list(icluster_names)[0]
            cluster_name = (
                im.resolve_cluster_name(cluster_name) or cluster_name)
            if resolved_name != cluster_name:
                logger.info(
                    '[%s]Mismatch: %s %s -> %s %s %s %s',
                    self.hijack_mode.value, self.from_application_name,
                    self.from_cluster_name, application_name, intent_map,
                    resolved_name, cluster_name)
                if self.hijack_mode is self.Mode.checking:
                    capture_message(
                        '[%s]RouteHijack mismatch' % self.hijack_mode.value,
                        extra={
                            'from_application_name': (
                                self.from_application_name),
                            'from_cluster_name': self.from_cluster_name,
                            'application_name': application_name,
                            'cluster_name': cluster_name,
                            'intent_map': repr(intent_map),
                            'intent': intent,
                            'resolved_name': resolved_name,
                        })
                self.analyse_mismatch(application_name, cluster_name,
                                      resolved_name, intent, intent_map)
Exemplo n.º 2
0
def trace_remote_http_call(url):
    start_at = int(time.time() * 1000)
    domain = urlparse.urlparse(url).hostname
    try:
        yield
    except HTTPError as e:
        response = e.response
        status_code = response.status_code
        if response.status_code >= 500:
            monitor_client.increment('remote_http_call.error',
                                     tags={
                                         'type': 'internal_error',
                                         'domain': domain,
                                         'status_code': str(status_code),
                                     })
            message = 'Remote HTTP API Internal Server Error'
            capture_message(message,
                            level=logging.WARNING,
                            extra={
                                'url': url,
                                'status_code': status_code,
                                'body': repr(response.content),
                            })
        raise
    except (Timeout, ConnectionError) as e:
        if isinstance(e, Timeout):
            _type = 'timeout'
        else:
            _type = 'connection_error'
        monitor_client.increment('remote_http_call.error',
                                 tags={
                                     'type': _type,
                                     'domain': domain,
                                     'status_code': 'unknown',
                                 })
        capture_exception(level=logging.WARNING, extra={'url': url})
        raise
    finally:
        monitor_client.timing('remote_http_call.timer',
                              int(time.time() * 1000) - start_at,
                              tags={'domain': domain})
Exemplo n.º 3
0
 def analyse_mismatch(self, dest_application_name, orig_dest_cluster_name,
                      resolved_dest_cluster_name, intent, intent_map):
     logger.info(
         '[%s]Unexpected mismatch: %s %s -> %s %s %s %s',
         self.hijack_mode.value, self.from_application_name,
         self.from_cluster_name, dest_application_name,
         intent_map, resolved_dest_cluster_name,
         orig_dest_cluster_name)
     capture_message(
         '[%s]Unexpected RouteHijack mismatch' % self.hijack_mode.value,
         extra={
             'from_application_name': self.from_application_name,
             'from_cluster_name': self.from_cluster_name,
             'application_name': dest_application_name,
             'cluster_name': orig_dest_cluster_name,
             'intent_map': repr(intent_map),
             'intent': intent,
             'resolved_name': resolved_dest_cluster_name},
         tags={
             'component': __name__,
             'from_application_name': self.from_application_name,
             'application_name': dest_application_name})
Exemplo n.º 4
0
def test_ignore_send_error(mocker):
    def is_switched_on(switch_name, default=True):
        return default

    mocker.patch.object(switch, 'is_switched_on', is_switched_on)

    mocker.patch(
        'huskar_api.extras.raven.raven_client',
        mocker.MagicMock(
            captureMessage=mocker.MagicMock(side_effect=Exception),
            captureException=mocker.MagicMock(side_effect=Exception)))

    assert capture_message('test') is None
    assert capture_exception('error') is None
Exemplo n.º 5
0
def test_message_off(sentry_client, mocker, turn_off):
    func = mocker.patch.object(sentry_client, 'captureMessage', autospec=True)
    turn_off(SWITCH_ENABLE_SENTRY_MESSAGE)
    capture_message('foobar')
    assert not func.called
Exemplo n.º 6
0
def test_message_on(sentry_client, mocker):
    func = mocker.patch.object(sentry_client, 'captureMessage', autospec=True)
    capture_message('foobar')
    func.assert_called_once_with('foobar')