Skip to content

Commit

Permalink
[ADD] sentry module (OCA#761)
Browse files Browse the repository at this point in the history
* [ADD] sentry module

* [FIX] updated sentry module according to PR comments
  • Loading branch information
naglis authored and dnplkndll committed Dec 29, 2020
1 parent 5c0a774 commit 4ea8144
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 39 deletions.
63 changes: 26 additions & 37 deletions sentry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
from . import const
from .logutils import LoggerNameFilter, OdooSentryHandler

import collections

_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 @@ -28,58 +24,51 @@ def get_odoo_commit(odoo_dir):
try:
return raven.fetch_git_sha(odoo_dir)
except raven.exceptions.InvalidGitRepository:
_logger.debug('Odoo directory: "%s" not a valid git repository', odoo_dir)
_logger.debug(
u'Odoo directory: "%s" not a valid git repository', odoo_dir)


def initialize_raven(config, client_cls=None):
"""
def initialize_raven(config, client_cls=raven.Client):
'''
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

if config.get("sentry_odoo_dir") and config.get("sentry_release"):
_logger.debug(
"Both sentry_odoo_dir and sentry_release defined, choosing sentry_release"
)
'''
options = {
"release": config.get(
"sentry_release", get_odoo_commit(config.get("sentry_odoo_dir"))
)
'release': get_odoo_commit(config.get('sentry_odoo_dir')),
}
for option in const.get_sentry_options():
value = config.get("sentry_%s" % option.key, option.default)
if isinstance(option.converter, collections.Callable):
for option in const.SENTRY_OPTIONS:
value = config.get('sentry_%s' % option.key, option.default)
if callable(option.converter):
value = option.converter(value)
options[option.key] = value

level = config.get("sentry_logging_level", const.DEFAULT_LOG_LEVEL)
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)
config.get('sentry_exclude_loggers', const.DEFAULT_EXCLUDE_LOGGERS)
)
if level not in const.LOG_LEVEL_MAP:
level = const.DEFAULT_LOG_LEVEL

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")
if enabled:
handler = OdooSentryHandler(
config.get('sentry_include_context', True),
client=client,
level=const.LOG_LEVEL_MAP[level],
)
raven.conf.setup_logging(handler)
wsgi_server.application = Sentry(wsgi_server.application, client=client)
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')
4 changes: 2 additions & 2 deletions sentry/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ def setUp(self):
def assertEventCaptured(self, client, event_level, event_msg):
self.assertTrue(
client.has_event(event_level, event_msg),
msg='Event: "%s" was not captured' % event_msg,
msg=u'Event: "%s" was not captured' % event_msg
)

def assertEventNotCaptured(self, client, event_level, event_msg):
self.assertFalse(
client.has_event(event_level, event_msg),
msg='Event: "%s" was captured' % event_msg,
msg=u'Event: "%s" was captured' % event_msg
)

def test_initialize_raven_sets_dsn(self):
Expand Down

0 comments on commit 4ea8144

Please sign in to comment.