Browse Source

[FIX] sentry: fixes missing `raven` library preventing loading of modules

Related: #761 #879 #881
pull/1082/head
Naglis Jonaitis 8 years ago
committed by Nicolas JEUDY
parent
commit
6b83a8b548
  1. 2
      sentry/README.rst
  2. 17
      sentry/__init__.py
  3. 27
      sentry/const.py
  4. 2
      sentry/logutils.py
  5. 2
      sentry/tests/test_client.py

2
sentry/README.rst

@ -37,7 +37,7 @@ configuration file:
*https://<public_key>:<secret_key>@sentry.example.com/<project id>* *https://<public_key>:<secret_key>@sentry.example.com/<project id>*
This is the only required option in order to use the module. This is the only required option in order to use the module.
``sentry_enabled`` Whether or not Sentry logging is enabled. ``True``
``sentry_enabled`` Whether or not Sentry logging is enabled. ``False``
``sentry_logging_level`` The minimal logging level for which to send reports to Sentry. ``warn`` ``sentry_logging_level`` The minimal logging level for which to send reports to Sentry. ``warn``
Possible values: *notset*, *debug*, *info*, *warn*, *error*, Possible values: *notset*, *debug*, *info*, *warn*, *error*,

17
sentry/__init__.py

@ -11,10 +11,12 @@ from . import const
from .logutils import LoggerNameFilter, OdooSentryHandler from .logutils import LoggerNameFilter, OdooSentryHandler
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
HAS_RAVEN = True
try: try:
import raven import raven
from raven.middleware import Sentry from raven.middleware import Sentry
except ImportError: except ImportError:
HAS_RAVEN = False
_logger.debug('Cannot import "raven". Please make sure it is installed.') _logger.debug('Cannot import "raven". Please make sure it is installed.')
@ -29,25 +31,25 @@ def get_odoo_commit(odoo_dir):
u'Odoo directory: "%s" not a valid git repository', odoo_dir) u'Odoo directory: "%s" not a valid git repository', odoo_dir)
def initialize_raven(config, client_cls=raven.Client):
def initialize_raven(config, client_cls=None):
''' '''
Setup an instance of :class:`raven.Client`. Setup an instance of :class:`raven.Client`.
:param config: Sentry configuration :param config: Sentry configuration
:param client: class used to instantiate the raven client. :param client: class used to instantiate the raven client.
''' '''
enabled = config.get('sentry_enabled', False)
if not (HAS_RAVEN and enabled):
return
options = { options = {
'release': get_odoo_commit(config.get('sentry_odoo_dir')), 'release': get_odoo_commit(config.get('sentry_odoo_dir')),
} }
for option in const.SENTRY_OPTIONS:
for option in const.get_sentry_options():
value = config.get('sentry_%s' % option.key, option.default) value = config.get('sentry_%s' % option.key, option.default)
if callable(option.converter): if callable(option.converter):
value = option.converter(value) value = option.converter(value)
options[option.key] = value options[option.key] = value
client = client_cls(**options)
enabled = config.get('sentry_enabled', True)
level = config.get('sentry_logging_level', const.DEFAULT_LOG_LEVEL) level = config.get('sentry_logging_level', const.DEFAULT_LOG_LEVEL)
exclude_loggers = const.split_multiple( exclude_loggers = const.split_multiple(
config.get('sentry_exclude_loggers', const.DEFAULT_EXCLUDE_LOGGERS) config.get('sentry_exclude_loggers', const.DEFAULT_EXCLUDE_LOGGERS)
@ -55,7 +57,8 @@ def initialize_raven(config, client_cls=raven.Client):
if level not in const.LOG_LEVEL_MAP: if level not in const.LOG_LEVEL_MAP:
level = const.DEFAULT_LOG_LEVEL level = const.DEFAULT_LOG_LEVEL
if enabled:
client_cls = client_cls or raven.Client
client = client_cls(**options)
handler = OdooSentryHandler( handler = OdooSentryHandler(
config.get('sentry_include_context', True), config.get('sentry_include_context', True),
client=client, client=client,
@ -68,8 +71,8 @@ def initialize_raven(config, client_cls=raven.Client):
wsgi_server.application = Sentry( wsgi_server.application = Sentry(
wsgi_server.application, client=client) wsgi_server.application, client=client)
client.captureMessage('Starting Odoo Server')
return client return client
sentry_client = initialize_raven(odoo_config) sentry_client = initialize_raven(odoo_config)
sentry_client.captureMessage('Starting Odoo Server')

27
sentry/const.py

@ -32,14 +32,6 @@ LOG_LEVEL_MAP = dict([
]) ])
DEFAULT_LOG_LEVEL = 'warn' DEFAULT_LOG_LEVEL = 'warn'
DEFAULT_TRANSPORT = 'threaded'
TRANSPORT_CLASS_MAP = {
'requests_synchronous': raven.transport.RequestsHTTPTransport,
'requests_threaded': raven.transport.ThreadedRequestsHTTPTransport,
'synchronous': raven.transport.HTTPTransport,
'threaded': raven.transport.ThreadedHTTPTransport,
}
ODOO_USER_EXCEPTIONS = [ ODOO_USER_EXCEPTIONS = [
'odoo.exceptions.AccessDenied', 'odoo.exceptions.AccessDenied',
'odoo.exceptions.AccessError', 'odoo.exceptions.AccessError',
@ -64,10 +56,23 @@ EXCLUDE_LOGGERS = (
) )
DEFAULT_EXCLUDE_LOGGERS = ','.join(EXCLUDE_LOGGERS) DEFAULT_EXCLUDE_LOGGERS = ','.join(EXCLUDE_LOGGERS)
SENTRY_OPTIONS = [
DEFAULT_TRANSPORT = 'threaded'
def select_transport(name=DEFAULT_TRANSPORT):
return {
'requests_synchronous': raven.transport.RequestsHTTPTransport,
'requests_threaded': raven.transport.ThreadedRequestsHTTPTransport,
'synchronous': raven.transport.HTTPTransport,
'threaded': raven.transport.ThreadedHTTPTransport,
}.get(name, DEFAULT_TRANSPORT)
def get_sentry_options():
return [
SentryOption('dsn', '', str.strip), SentryOption('dsn', '', str.strip),
SentryOption('install_sys_hook', False, None), SentryOption('install_sys_hook', False, None),
SentryOption('transport', DEFAULT_TRANSPORT, TRANSPORT_CLASS_MAP.get),
SentryOption('transport', DEFAULT_TRANSPORT, select_transport),
SentryOption('include_paths', '', split_multiple), SentryOption('include_paths', '', split_multiple),
SentryOption('exclude_paths', '', split_multiple), SentryOption('exclude_paths', '', split_multiple),
SentryOption('machine', defaults.NAME, None), SentryOption('machine', defaults.NAME, None),
@ -81,4 +86,4 @@ SENTRY_OPTIONS = [
'ignore_exceptions', DEFAULT_IGNORED_EXCEPTIONS, split_multiple), 'ignore_exceptions', DEFAULT_IGNORED_EXCEPTIONS, split_multiple),
SentryOption('processors', DEFAULT_PROCESSORS, split_multiple), SentryOption('processors', DEFAULT_PROCESSORS, split_multiple),
SentryOption('environment', None, None), SentryOption('environment', None, None),
]
]

2
sentry/logutils.py

@ -14,6 +14,8 @@ try:
from raven.utils.wsgi import get_environ, get_headers from raven.utils.wsgi import get_environ, get_headers
except ImportError: except ImportError:
_logger.debug('Cannot import "raven". Please make sure it is installed.') _logger.debug('Cannot import "raven". Please make sure it is installed.')
SentryHandler = object
SanitizePasswordsProcessor = object
def get_request_info(request): def get_request_info(request):

2
sentry/tests/test_client.py

@ -85,7 +85,7 @@ class TestClientSetup(unittest.TestCase):
def test_initialize_raven_sets_dsn(self): def test_initialize_raven_sets_dsn(self):
config = { config = {
'sentry_enabled': False,
'sentry_enabled': True,
'sentry_dsn': 'http://public:secret@example.com/1', 'sentry_dsn': 'http://public:secret@example.com/1',
} }
client = initialize_raven(config, client_cls=InMemoryClient) client = initialize_raven(config, client_cls=InMemoryClient)

Loading…
Cancel
Save