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

[14.0][MIG] sentry #1929

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 8 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repos:
language: fail
files: "\\.rej$"
- repo: https://github.com/oca/maintainer-tools
rev: 1b5c7ad
rev: 0290f3971ed057ff868731ddac36a28cf4b1bc97
hooks:
# update the NOT INSTALLABLE ADDONS section above
- id: oca-update-pre-commit-excluded-addons
Expand All @@ -41,7 +41,7 @@ repos:
rev: 20.8b1
hooks:
- id: black
- repo: https://github.com/prettier/pre-commit
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.1.2
hooks:
- id: prettier
Expand All @@ -52,15 +52,15 @@ repos:
args:
- --plugin=@prettier/plugin-xml
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v7.8.1
rev: v7.13.0-1
hooks:
- id: eslint
verbose: true
args:
- --color
- --fix
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
rev: v3.3.0
hooks:
- id: trailing-whitespace
# exclude autogenerated files
Expand All @@ -82,11 +82,11 @@ repos:
- id: mixed-line-ending
args: ["--fix=lf"]
- repo: https://github.com/asottile/pyupgrade
rev: v2.7.2
rev: v2.7.4
hooks:
- id: pyupgrade
- repo: https://github.com/PyCQA/isort
rev: 5.5.1
rev: 5.6.4
hooks:
- id: isort
name: isort except __init__.py
Expand All @@ -104,7 +104,7 @@ repos:
- --header
- "# generated from manifests external_dependencies"
- repo: https://gitlab.com/PyCQA/flake8
rev: 3.8.3
rev: 3.8.4
hooks:
- id: flake8
name: flake8 except __init__.py
Expand All @@ -116,7 +116,7 @@ repos:
files: /__init__\.py$
additional_dependencies: ["flake8-bugbear==20.1.4"]
- repo: https://github.com/PyCQA/pylint
rev: pylint-2.5.3
rev: pylint-2.6.0
hooks:
- id: pylint
name: pylint with optional checks
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# generated from manifests external_dependencies
raven
190 changes: 190 additions & 0 deletions sentry/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
======
Sentry
======

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/13.0/sentry
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-13-0/server-tools-13-0-sentry
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/149/13.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

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

**Table of contents**

.. contents::
:local:

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. ``False``

``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.DeferredException,
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_release`` Explicitly define a version to be sent as the release version to
Sentry. Useful in conjuntion with Sentry's "Resolve in the next
release"-functionality. Also useful if your production deployment
does not include any Git context from which a commit might be read.
Overrides *sentry_odoo_dir*.

``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.
Overridden by *sentry_release*
============================= ==================================================================== ==========================================================

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/
sentry_release = 1.3.2

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/13.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/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20sentry%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Mohammed Barsi
* Versada
* Nicolas JEUDY

Contributors
~~~~~~~~~~~~

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

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

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

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.

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/13.0/sentry>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
85 changes: 85 additions & 0 deletions sentry/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# 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

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.')


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('Odoo directory: "%s" not a valid git repository', odoo_dir)


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

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"))
)
}
for option in const.get_sentry_options():
value = config.get("sentry_%s" % option.key, option.default)
if isinstance(option.converter, collections.Callable):
value = option.converter(value)
options[option.key] = value

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

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)
18 changes: 18 additions & 0 deletions sentry/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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": "14.0.1.0.0",
"category": "Extra Tools",
"website": "https://github.com/OCA/server-tools",
"author": "Mohammed Barsi,"
"Versada,"
"Nicolas JEUDY,"
"Odoo Community Association (OCA)",
"license": "AGPL-3",
"application": False,
"installable": True,
"external_dependencies": {"python": ["raven"]},
"depends": ["base"],
}
Loading