Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] sentry module #761

Merged
merged 2 commits into from
Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ install:
- export PATH=${HOME}/maintainer-quality-tools/travis:${PATH}
- travis_install_nightly
# Install libraries that require specific development headers, but not during lint test
- if ! [ "$LINT_CHECK" = "1" ]; then pip install pymssql MySQL-python pyodbc; fi
- if ! [ "$LINT_CHECK" = "1" ]; then pip install pymssql MySQL-python pyodbc raven; fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move these to requirements.txt

- printf '[options]\n\nrunning_env = dev\n' > ${HOME}/.openerp_serverrc
- ln -s ${TRAVIS_BUILD_DIR}/server_environment_files_sample ${TRAVIS_BUILD_DIR}/server_environment_files
script:
Expand Down
167 changes: 167 additions & 0 deletions sentry/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

======
Sentry
======

This module allows painless `Sentry <https://sentry.io/>`__ integration with
Odoo.

Installation
============

The module can be installed just like any other Odoo module, by adding the
module's directory to Odoo *addons_path*. In order for the module to correctly
wrap the Odoo WSGI application, it also needs to be loaded as a server-wide
module. This can be done with the ``server_wide_modules`` parameter in your
Odoo config file or with the ``--load`` command-line parameter.

This module additionally requires the raven_ Python package to be available on
the system. It can be installed using pip::

pip install raven

Configuration
=============

The following additional configuration options can be added to your Odoo
configuration file:

============================= ==================================================================== ==========================================================
Option Description Default
============================= ==================================================================== ==========================================================
``sentry_dsn`` Sentry *Data Source Name*. You can find this value in your Sentry ``''``
project configuration. Typically it looks something like this:
*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_logging_level`` The minimal logging level for which to send reports to Sentry. ``warn``
Possible values: *notset*, *debug*, *info*, *warn*, *error*,
*critical*. It is recommended to have this set to at least *warn*,
to avoid spamming yourself with Sentry events.

``sentry_exclude_loggers`` A string of comma-separated logger names which should be excluded ``werkzeug``
from Sentry.

``sentry_ignored_exceptions`` A string of comma-separated exceptions which should be ignored. ``odoo.exceptions.AccessDenied,
You can use a star symbol (*) at the end, to ignore all exceptions odoo.exceptions.AccessError,
from a module, eg.: *odoo.exceptions.**. odoo.exceptions.MissingError,
odoo.exceptions.RedirectWarning,
odoo.exceptions.UserError,
odoo.exceptions.ValidationError,
odoo.exceptions.Warning,
odoo.exceptions.except_orm``

``sentry_processors`` A string of comma-separated processor classes which will be applied ``raven.processors.SanitizePasswordsProcessor,
on an event before sending it to Sentry. odoo.addons.sentry.logutils.SanitizeOdooCookiesProcessor``

``sentry_transport`` Transport class which will be used to send events to Sentry. ``threaded``
Possible values: *threaded*: spawns an async worker for processing
messages, *synchronous*: a synchronous blocking transport;
*requests_threaded*: an asynchronous transport using the *requests*
library; *requests_synchronous* - blocking transport using the
*requests* library.

``sentry_include_context`` If enabled, additional context data will be extracted from current ``True``
HTTP request and user session (if available). This has no effect
for Cron jobs, as no request/session is available inside a Cron job.

``sentry_odoo_dir`` Absolute path to your Odoo installation directory. This is optional
and will only be used to extract the Odoo Git commit, which will be
sent to Sentry, to allow to distinguish between Odoo updates.
============================= ==================================================================== ==========================================================

Other `client arguments
<https://docs.sentry.io/clients/python/advanced/#client-arguments>`_ can be
configured by prepending the argument name with *sentry_* in your Odoo config
file. Currently supported additional client arguments are: ``install_sys_hook,
include_paths, exclude_paths, machine, auto_log_stacks, capture_locals,
string_max_length, list_max_length, site, include_versions, environment``.

Example Odoo configuration
--------------------------

Below is an example of Odoo configuration file with *Odoo Sentry* options::

[options]
sentry_dsn = https://<public_key>:<secret_key>@sentry.example.com/<project id>
sentry_enabled = true
sentry_logging_level = warn
sentry_exclude_loggers = werkzeug
sentry_ignore_exceptions = odoo.exceptions.AccessDenied,odoo.exceptions.AccessError,odoo.exceptions.MissingError,odoo.exceptions.RedirectWarning,odoo.exceptions.UserError,odoo.exceptions.ValidationError,odoo.exceptions.Warning,odoo.exceptions.except_orm
sentry_processors = raven.processors.SanitizePasswordsProcessor,odoo.addons.sentry.logutils.SanitizeOdooCookiesProcessor
sentry_transport = threaded
sentry_include_context = true
sentry_environment = production
sentry_auto_log_stacks = false
sentry_odoo_dir = /home/odoo/odoo/

Usage
=====

Once configured and installed, the module will report any logging event at and
above the configured Sentry logging level, no additional actions are necessary.

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/149/10.0

Known issues / Roadmap
======================

* **No database separation** -- This module functions by intercepting all Odoo
logging records in a running Odoo process. This means that once installed in
one database, it will intercept and report errors for all Odoo databases,
which are used on that Odoo server.

* **Frontend integration** -- In the future, it would be nice to add
Odoo client-side error reporting to this module as well, by integrating
`raven-js <https://github.com/getsentry/raven-js>`_. Additionally, `Sentry user
feedback form <https://docs.sentry.io/learn/user-feedback/>`_ could be
integrated into the Odoo client error dialog window to allow users shortly
describe what they were doing when things went wrong.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/maintainer-tools/issues>`_. In case of trouble, please
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/maintainer-tools/server-tools

check there if your issue has already been reported. If you spotted it first,
help us smash it by providing detailed and welcomed feedback.

Credits
=======

Images
------

* `Module Icon <https://sentry.io/branding/>`_

Contributors
------------

* Mohammed Barsi <[email protected]>
* Andrius Preimantas <[email protected]>
* Naglis Jonaitis <[email protected]>

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.


.. _raven: https://github.com/getsentry/raven-python
75 changes: 75 additions & 0 deletions sentry/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
# Copyright 2016-2017 Versada <https://versada.eu/>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging

from odoo.service import wsgi_server
from odoo.tools import config as odoo_config

from . import const
from .logutils import LoggerNameFilter, OdooSentryHandler

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


def get_odoo_commit(odoo_dir):
'''Attempts to get Odoo git commit from :param:`odoo_dir`.'''
if not odoo_dir:
return
try:
return raven.fetch_git_sha(odoo_dir)
except raven.exceptions.InvalidGitRepository:
_logger.debug(
u'Odoo directory: "%s" not a valid git repository', odoo_dir)


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.
'''
options = {
'release': get_odoo_commit(config.get('sentry_odoo_dir')),
}
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

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)

return client


sentry_client = initialize_raven(odoo_config)
sentry_client.captureMessage('Starting Odoo Server')
24 changes: 24 additions & 0 deletions sentry/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Copyright 2016-2017 Versada <https://versada.eu/>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': 'Sentry',
'summary': 'Report Odoo errors to Sentry',
'version': '10.0.0.1.0',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10.0.1.0.0

'category': 'Extra Tools',
'website': 'https://odoo-community.org/',
'author': 'Mohammed Barsi,'
'Versada,'
'Odoo Community Association (OCA)',
'license': 'AGPL-3',
'application': False,
'installable': True,
'external_dependencies': {
'python': [
'raven',
]
},
'depends': [
'base',
],
}
83 changes: 83 additions & 0 deletions sentry/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-
# Copyright 2016-2017 Versada <https://versada.eu/>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import collections
import logging

import odoo.loglevels

_logger = logging.getLogger(__name__)
try:
import raven
from raven.conf import defaults
except ImportError:
_logger.debug('Cannot import "raven". Please make sure it is installed.')


def split_multiple(string, delimiter=',', strip_chars=None):
'''Splits :param:`string` and strips :param:`strip_chars` from values.'''
if not string:
return []
return [v.strip(strip_chars) for v in string.split(delimiter)]


SentryOption = collections.namedtuple(
'SentryOption', ['key', 'default', 'converter'])

# Mapping of Odoo logging level -> Python stdlib logging library log level.
LOG_LEVEL_MAP = dict([
(getattr(odoo.loglevels, 'LOG_%s' % x), getattr(logging, x))
for x in ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET')
])
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',
'odoo.exceptions.MissingError',
'odoo.exceptions.RedirectWarning',
'odoo.exceptions.UserError',
'odoo.exceptions.ValidationError',
'odoo.exceptions.Warning',
'odoo.exceptions.except_orm',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and odoo.exceptions.QWebException

Take a look at the below though - maybe we could leverage something like this to make the module a bit more future-ready?

>>> from odoo import exceptions
>>> dir(exceptions)
['AccessDenied', 'AccessError', 'DeferredException', 'MissingError', 'QWebException', 'RedirectWarning', 'UserError', 'ValidationError', 'Warning', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_logger', 'currentframe', 'except_orm', 'frame_codeinfo', 'logging']
>>> [e for e in dir(exceptions) if not e.startswith('_')]
['AccessDenied', 'AccessError', 'DeferredException', 'MissingError', 'QWebException', 'RedirectWarning', 'UserError', 'ValidationError', 'Warning', 'currentframe', 'except_orm', 'frame_codeinfo', 'logging']

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and odoo.exceptions.QWebException

Hm, QWebException does not look like a user error so maybe it would make sense to receive such errors?. Also, it's weird that QWebException is also defined in addons/base/ir/ir_qweb/qweb.py so I guess we should ignore both (if we decide to ignore).

Take a look at the below though - maybe we could leverage something like this to make the module a bit more future-ready?

Do you mean that we should analize the names in odoo.exceptions and look for Exception subclasses? IDK, the list of ignored exceptions is configurable, so one could add any new exceptions to the list in the config file. Also, analizing the exceptions at load time would feel a bit magic, i.e. you wouldn't really know which exceptions are ignored. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm I didn't realize there were ignored exceptions here. Is there a reason we don't want to catch all exceptions raised by Odoo?

Copy link
Contributor

@lasley lasley Mar 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking for a bit (and drinking a bit of coffee), I think actually the better question is this:

What is the definition of a "User Error" in the context of this app , and how can they be changed ?

Edit: they can be changed with sentry_ignored_exceptions in Odoo conf. My bad on that miss.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odoo uses these exceptions as part of flow control and these exceptions are integrated more nicely with the client, eg. in Odoo 8, if you try to add a line to a SO before setting the partner in the SO form, you will get an except_orm informing about the partner not being set. I think getting such exceptions in Sentry would cause more noise than it would do good, so these are ignored by default (but, as you've noticed, it's configurable with sentry_ignored_exceptions).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation @naglis - works for me!

]
DEFAULT_IGNORED_EXCEPTIONS = ','.join(ODOO_USER_EXCEPTIONS)

PROCESSORS = (
'raven.processors.SanitizePasswordsProcessor',
'odoo.addons.sentry.logutils.SanitizeOdooCookiesProcessor',
)
DEFAULT_PROCESSORS = ','.join(PROCESSORS)

EXCLUDE_LOGGERS = (
'werkzeug',
)
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),
]
Loading