Exemple #1
0
    def make_errormator_middleware(app, global_config=None, **kw):
        if not global_config:
            global_config = {}
        config = global_config.copy()
        config.update(kw)
        # this shuts down all errormator functionalities
        if not asbool(config.get('errormator', True)):
            return app
        logging.critical('ERRORMATOR MIDDLEWARE')
        ini_path = os.environ.get(
            'ERRORMATOR_INI', config.get('errormator.config_path', ini_file))

        config = get_config(config=config, path_to_config=ini_path)
        client = Client(config)
        from errormator_client.wsgi import ErrormatorWSGIWrapper
        app = ErrormatorWSGIWrapper(app, client)
        return app
    def make_errormator_middleware(app, global_config=None, **kw):
        if not global_config:
            global_config = {}
        config = global_config.copy()
        config.update(kw)
        # this shuts down all errormator functionalities
        if not asbool(config.get('errormator', True)):
            return app
        log.critical('ERRORMATOR MIDDLEWARE')
        ini_path = os.environ.get('ERRORMATOR_INI',
                                  config.get('errormator.config_path',
                                             ini_file))
        config = get_config(config=config, path_to_config=ini_path)
        client = Client(config)
        from errormator_client.wsgi import ErrormatorWSGIWrapper

        app = ErrormatorWSGIWrapper(app, client)
        return app
def make_errormator_middleware(app, global_config=None, **kw):
    if global_config:
        config = global_config.copy()
    else:
        config = {}
    config.update(kw)
    ini_path = os.environ.get('ERRORMATOR_INI',
                              config.get('errormator.config_path'))
    config = get_config(config=config, path_to_config=ini_path)
    # this shuts down all errormator functionalities
    if not asbool(config.get('errormator', True)):
        log.warning("ERRORMATOR_INI config variable is missing from"
                    "environment or errormator.config_path "
                    "not passed in app global config, "
                    "or errormator disabled in config")
        return app
    client = Client(config)
    from errormator_client.wsgi import ErrormatorWSGIWrapper

    app = ErrormatorWSGIWrapper(app, client)
    return app
    def __init__(self, config=None, register_timing=True):
        """
        at minimum client expects following keys to be present::

            errormator = true
            errormator.server_url = https://api.errormator.com
            errormator.api_key = YOUR_API_KEY

        """
        self.config = {}
        # general options
        self.config['enabled'] = asbool(config.get('errormator', True))
        self.config['server_name'] = config.get('errormator.server_name') \
            or socket.getfqdn()
        if PY3:
            default_client = 'python3'
        else:
            default_client = 'python'
        self.config['client'] = config.get('errormator.client', default_client)
        self.config['api_key'] = config.get('errormator.api_key')
        self.config['server_url'] = config.get('errormator.server_url',
                                               'https://api.errormator.com')
        self.config['timeout'] = int(config.get('errormator.timeout', 10))
        self.config['reraise_exceptions'] = asbool(
            config.get('errormator.reraise_exceptions', True))
        self.config['slow_requests'] = asbool(
            config.get('errormator.slow_requests', True))
        self.config['slow_request_time'] = float(
            config.get('errormator.slow_request_time', 1))
        if self.config['slow_request_time'] < 0.01:
            self.config['slow_request_time'] = 0.01
        self.config['slow_request_time'] = datetime.timedelta(
            seconds=self.config['slow_request_time'])
        self.config['logging'] = asbool(config.get('errormator.logging', True))
        self.config['logging_on_error'] = asbool(
            config.get('errormator.logging_on_error', False))
        self.config['report_404'] = asbool(config.get('errormator.report_404',
                                                      False))
        self.config['report_local_vars'] = asbool(
            config.get('errormator.report_local_vars', False))
        self.config['report_errors'] = asbool(
            config.get('errormator.report_errors', True))
        self.config['buffer_flush_interval'] = int(
            config.get('errormator.buffer_flush_interval', 5))
        self.config['force_send'] = asbool(config.get('errormator.force_send',
                                                      False))
        self.config['request_keys_blacklist'] = ['password', 'passwd', 'pwd',
                                                 'auth_tkt', 'secret', 'csrf',
                                                 'session', 'pass', 'config',
                                                 'settings', 'environ']
        req_blacklist = aslist(config.get('errormator.request_keys_blacklist',
                                           config.get(
                                               'errormator.bad_request_keys')),
                                ',')
        self.config['request_keys_blacklist'].extend(
            filter(lambda x: x, req_blacklist)
        )
        if config.get('errormator.bad_request_keys'):
            log.warning('errormator.bad_request_keys is deprecated use '
                        'request_keys_blacklist')  # pragma: nocover

        self.config['environ_keys_whitelist'] = [
            'REMOTE_USER', 'REMOTE_ADDR', 'SERVER_NAME', 'CONTENT_TYPE',
            'HTTP_REFERER']
        environ_whitelist = aslist(
            config.get('errormator.environ_keys_whitelist'), ',')
        self.config['environ_keys_whitelist'].extend(
            filter(lambda x: x, environ_whitelist))
        self.config['log_namespace_blacklist'] = ['errormator_client.client']

        log_blacklist = aslist(
            config.get('errormator.log_namespace_blacklist'), ',')
        self.config['log_namespace_blacklist'].extend(filter(
            lambda x: x, log_blacklist))

        self.filter_callable = config.get('errormator.filter_callable')
        if self.filter_callable:
            try:
                parts = self.filter_callable.split(':')
                _tmp = __import__(parts[0], globals(), locals(),
                                  [parts[1], ], 0)
                self.filter_callable = getattr(_tmp, parts[1])
            except ImportError as e:
                self.filter_callable = self.data_filter
                msg = 'Could not import filter callable, using default, %s' % e
                log.error(msg)
        else:
            self.filter_callable = self.data_filter

        if self.config['buffer_flush_interval'] < 1:
            self.config['buffer_flush_interval'] = 1
        self.config['buffer_flush_interval'] = datetime.timedelta(
            seconds=self.config['buffer_flush_interval'])
        # register logging
        import errormator_client.logger

        if self.config['logging'] and self.config['enabled']:
            self.log_handler = errormator_client.logger.register_logging()
            level = LEVELS.get(config.get('errormator.logging.level',
                                          'WARNING').lower(), logging.WARNING)
            self.log_handler.setLevel(level)

        # register slow call metrics
        if self.config['slow_requests'] and self.config['enabled']:
            self.config['timing'] = config.get('errormator.timing', {})
            for k, v in config.items():
                if k.startswith('errormator.timing'):
                    try:
                        self.config['timing'][k[18:]] = float(v)
                    except (TypeError, ValueError) as e:
                        self.config['timing'][k[18:]] = False
            import errormator_client.timing

            errormator_client.timing.register_timing(self.config)

        self.endpoints = {
            "reports": '/api/reports',
            "slow_reports": '/api/slow_reports',
            "logs": '/api/logs',
            "request_stats": '/api/request_stats'
        }

        self.report_queue = []
        self.report_queue_lock = threading.RLock()
        self.slow_report_queue = []
        self.slow_report_queue_lock = threading.RLock()
        self.log_queue = []
        self.log_queue_lock = threading.RLock()
        self.request_stats = {}
        self.request_stats_lock = threading.RLock()
        self.uuid = uuid.uuid4()
        self.last_submit = datetime.datetime.utcnow()
        self.last_request_stats_submit = datetime.datetime.utcnow()
Exemple #5
0
    def __init__(self, config=None, register_timing=True):
        """
        at minimum client expects following keys to be present::

            errormator = true
            errormator.server_url = https://api.errormator.com
            errormator.api_key = YOUR_API_KEY

        """
        self.config = {}
        # general options
        self.config['enabled'] = asbool(config.get('errormator', True))
        self.config['server_name'] = config.get('errormator.server_name') \
            or socket.getfqdn()
        if PY3:
            default_client = 'python3'
        else:
            default_client = 'python'
        self.config['client'] = config.get('errormator.client', default_client)
        self.config['api_key'] = config.get('errormator.api_key')
        if not self.config['api_key']:
            self.config['enabled'] = False
            logging.warning("Disabling errormator client, no api key")

        self.config['server_url'] = config.get('errormator.server_url',
                                               'https://api.errormator.com')
        self.config['timeout'] = int(config.get('errormator.timeout', 10))
        self.config['reraise_exceptions'] = asbool(
            config.get('errormator.reraise_exceptions', True))
        self.config['slow_requests'] = asbool(
            config.get('errormator.slow_requests', True))
        self.config['slow_request_time'] = float(
            config.get('errormator.slow_request_time', 1))
        if self.config['slow_request_time'] < 0.01:
            self.config['slow_request_time'] = 0.01
        self.config['slow_request_time'] = datetime.timedelta(
            seconds=self.config['slow_request_time'])
        self.config['logging'] = asbool(config.get('errormator.logging', True))
        self.config['logging_on_error'] = asbool(
            config.get('errormator.logging_on_error', False))
        self.config['report_404'] = asbool(
            config.get('errormator.report_404', False))
        self.config['report_local_vars'] = asbool(
            config.get('errormator.report_local_vars', False))
        self.config['report_errors'] = asbool(
            config.get('errormator.report_errors', True))
        self.config['buffer_flush_interval'] = int(
            config.get('errormator.buffer_flush_interval', 5))
        self.config['force_send'] = asbool(
            config.get('errormator.force_send', False))
        self.config['request_keys_blacklist'] = [
            'password', 'passwd', 'pwd', 'auth_tkt', 'secret', 'csrf',
            'session', 'pass', 'config', 'settings', 'environ', 'xsrf', 'auth'
        ]
        req_blacklist = aslist(
            config.get('errormator.request_keys_blacklist',
                       config.get('errormator.bad_request_keys')), ',')
        self.config['request_keys_blacklist'].extend(
            filter(lambda x: x, req_blacklist))
        if config.get('errormator.bad_request_keys'):
            log.warning('errormator.bad_request_keys is deprecated use '
                        'request_keys_blacklist')  # pragma: nocover

        self.config['environ_keys_whitelist'] = [
            'REMOTE_USER', 'REMOTE_ADDR', 'SERVER_NAME', 'CONTENT_TYPE',
            'HTTP_REFERER'
        ]
        environ_whitelist = aslist(
            config.get('errormator.environ_keys_whitelist'), ',')
        self.config['environ_keys_whitelist'].extend(
            filter(lambda x: x, environ_whitelist))
        self.config['log_namespace_blacklist'] = ['errormator_client.client']

        log_blacklist = aslist(
            config.get('errormator.log_namespace_blacklist'), ',')
        self.config['log_namespace_blacklist'].extend(
            filter(lambda x: x, log_blacklist))

        self.filter_callable = config.get('errormator.filter_callable')
        if self.filter_callable:
            try:
                parts = self.filter_callable.split(':')
                _tmp = __import__(parts[0], globals(), locals(), [
                    parts[1],
                ], 0)
                self.filter_callable = getattr(_tmp, parts[1])
            except ImportError as e:
                self.filter_callable = self.data_filter
                msg = 'Could not import filter callable, using default, %s' % e
                log.error(msg)
        else:
            self.filter_callable = self.data_filter

        if self.config['buffer_flush_interval'] < 1:
            self.config['buffer_flush_interval'] = 1
        self.config['buffer_flush_interval'] = datetime.timedelta(
            seconds=self.config['buffer_flush_interval'])
        # register logging
        import errormator_client.logger
        if self.config['logging'] and self.config['enabled']:
            self.log_handler = errormator_client.logger.register_logging()
            level = LEVELS.get(
                config.get('errormator.logging.level', 'WARNING').lower(),
                logging.WARNING)
            self.log_handler.setLevel(level)

        # register slow call metrics
        if self.config['slow_requests'] and self.config['enabled']:
            self.config['timing'] = config.get('errormator.timing', {})
            for k, v in config.items():
                if k.startswith('errormator.timing'):
                    try:
                        self.config['timing'][k[18:]] = float(v)
                    except (TypeError, ValueError) as e:
                        self.config['timing'][k[18:]] = False
            import errormator_client.timing

            errormator_client.timing.register_timing(self.config)

        self.endpoints = {
            "reports": '/api/reports',
            "slow_reports": '/api/slow_reports',
            "logs": '/api/logs',
            "request_stats": '/api/request_stats'
        }

        self.report_queue = []
        self.report_queue_lock = threading.RLock()
        self.slow_report_queue = []
        self.slow_report_queue_lock = threading.RLock()
        self.log_queue = []
        self.log_queue_lock = threading.RLock()
        self.request_stats = {}
        self.request_stats_lock = threading.RLock()
        self.uuid = uuid.uuid4()
        self.last_submit = datetime.datetime.utcnow() - datetime.timedelta(
            minutes=1)
        self.last_request_stats_submit = datetime.datetime.utcnow(
        ) - datetime.timedelta(minutes=1)