Skip to content

Commit

Permalink
[FIX] sentry: fixes missing raven library preventing loading of mod…
Browse files Browse the repository at this point in the history
…ules

Related: OCA#761 OCA#879 OCA#881
  • Loading branch information
Naglis Jonaitis authored and Nicolas JEUDY committed Jan 11, 2018
1 parent ce7b6a1 commit 6b83a8b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 46 deletions.
2 changes: 1 addition & 1 deletion sentry/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ configuration file:
*https://<public_key>:<secret_key>@sentry.example.com/<project id>*
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``
Possible values: *notset*, *debug*, *info*, *warn*, *error*,
Expand Down
39 changes: 21 additions & 18 deletions sentry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
from .logutils import LoggerNameFilter, OdooSentryHandler

_logger = logging.getLogger(__name__)
HAS_RAVEN = True
try:
import raven
from raven.middleware import Sentry
except ImportError:
HAS_RAVEN = False
_logger.debug('Cannot import "raven". Please make sure it is installed.')


Expand All @@ -29,47 +31,48 @@ def get_odoo_commit(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`.
:param config: Sentry configuration
:param client: class used to instantiate the raven client.
'''
enabled = config.get('sentry_enabled', False)
if not (HAS_RAVEN and enabled):
return
options = {
'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)
if callable(option.converter):
value = option.converter(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)
exclude_loggers = const.split_multiple(
config.get('sentry_exclude_loggers', const.DEFAULT_EXCLUDE_LOGGERS)
)
if level not in const.LOG_LEVEL_MAP:
level = const.DEFAULT_LOG_LEVEL

if enabled:
handler = OdooSentryHandler(
config.get('sentry_include_context', True),
client=client,
level=const.LOG_LEVEL_MAP[level],
)
if exclude_loggers:
handler.addFilter(LoggerNameFilter(
exclude_loggers, name='sentry.logger.filter'))
raven.conf.setup_logging(handler)
wsgi_server.application = Sentry(
wsgi_server.application, client=client)
client_cls = client_cls or raven.Client
client = client_cls(**options)
handler = OdooSentryHandler(
config.get('sentry_include_context', True),
client=client,
level=const.LOG_LEVEL_MAP[level],
)
if exclude_loggers:
handler.addFilter(LoggerNameFilter(
exclude_loggers, name='sentry.logger.filter'))
raven.conf.setup_logging(handler)
wsgi_server.application = Sentry(
wsgi_server.application, client=client)

client.captureMessage('Starting Odoo Server')
return client


sentry_client = initialize_raven(odoo_config)
sentry_client.captureMessage('Starting Odoo Server')
57 changes: 31 additions & 26 deletions sentry/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ def split_multiple(string, delimiter=',', strip_chars=None):
])
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.exceptions.AccessDenied',
'odoo.exceptions.AccessError',
Expand All @@ -64,21 +56,34 @@ def split_multiple(string, delimiter=',', strip_chars=None):
)
DEFAULT_EXCLUDE_LOGGERS = ','.join(EXCLUDE_LOGGERS)

SENTRY_OPTIONS = [
SentryOption('dsn', '', str.strip),
SentryOption('install_sys_hook', False, None),
SentryOption('transport', DEFAULT_TRANSPORT, TRANSPORT_CLASS_MAP.get),
SentryOption('include_paths', '', split_multiple),
SentryOption('exclude_paths', '', split_multiple),
SentryOption('machine', defaults.NAME, None),
SentryOption('auto_log_stacks', defaults.AUTO_LOG_STACKS, None),
SentryOption('capture_locals', defaults.CAPTURE_LOCALS, None),
SentryOption('string_max_length', defaults.MAX_LENGTH_STRING, None),
SentryOption('list_max_length', defaults.MAX_LENGTH_LIST, None),
SentryOption('site', None, None),
SentryOption('include_versions', True, None),
SentryOption(
'ignore_exceptions', DEFAULT_IGNORED_EXCEPTIONS, split_multiple),
SentryOption('processors', DEFAULT_PROCESSORS, split_multiple),
SentryOption('environment', None, None),
]
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('install_sys_hook', False, None),
SentryOption('transport', DEFAULT_TRANSPORT, select_transport),
SentryOption('include_paths', '', split_multiple),
SentryOption('exclude_paths', '', split_multiple),
SentryOption('machine', defaults.NAME, None),
SentryOption('auto_log_stacks', defaults.AUTO_LOG_STACKS, None),
SentryOption('capture_locals', defaults.CAPTURE_LOCALS, None),
SentryOption('string_max_length', defaults.MAX_LENGTH_STRING, None),
SentryOption('list_max_length', defaults.MAX_LENGTH_LIST, None),
SentryOption('site', None, None),
SentryOption('include_versions', True, None),
SentryOption(
'ignore_exceptions', DEFAULT_IGNORED_EXCEPTIONS, split_multiple),
SentryOption('processors', DEFAULT_PROCESSORS, split_multiple),
SentryOption('environment', None, None),
]
2 changes: 2 additions & 0 deletions sentry/logutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from raven.utils.wsgi import get_environ, get_headers
except ImportError:
_logger.debug('Cannot import "raven". Please make sure it is installed.')
SentryHandler = object
SanitizePasswordsProcessor = object


def get_request_info(request):
Expand Down
2 changes: 1 addition & 1 deletion sentry/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def assertEventNotCaptured(self, client, event_level, event_msg):

def test_initialize_raven_sets_dsn(self):
config = {
'sentry_enabled': False,
'sentry_enabled': True,
'sentry_dsn': 'http://public:[email protected]/1',
}
client = initialize_raven(config, client_cls=InMemoryClient)
Expand Down

0 comments on commit 6b83a8b

Please sign in to comment.