diff --git a/monitoring_statsd/README.rst b/monitoring_statsd/README.rst new file mode 100644 index 0000000..2ef9b1e --- /dev/null +++ b/monitoring_statsd/README.rst @@ -0,0 +1,31 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :alt: License + +============================ +Monitoring: Statsd metrics +============================ + +Send metrics to a Statsd (or Statsd compatible) server. + +Currently, it sends: + * time taken to process a click on a button + * time taken to process a workflow signal + * time taken by other requests + +Configuration +============= + +Activate with the environment variable: + +* ``ODOO_STATSD=1`` + +Configure Statsd: + +* ``STATSD_HOST=host`` +* ``STATSD_PORT=port`` + +Configure differentiator: + +* ``STATSD_CUSTOMER`` must contain the name of the client +* ``STATSD_ENVIRONMENT`` might contain the name of the environment, by + default, it will use the ``server_environment``'s one diff --git a/monitoring_statsd/__init__.py b/monitoring_statsd/__init__.py new file mode 100644 index 0000000..cde864b --- /dev/null +++ b/monitoring_statsd/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import models diff --git a/monitoring_statsd/__openerp__.py b/monitoring_statsd/__openerp__.py new file mode 100644 index 0000000..2989cd1 --- /dev/null +++ b/monitoring_statsd/__openerp__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + + +{'name': 'Monitoring: Statsd Metrics', + 'version': '9.0.1.0.0', + 'author': 'Camptocamp', + 'license': 'AGPL-3', + 'category': 'category', + 'depends': ['base', + 'web', + 'server_environment', + ], + 'website': 'http://www.camptocamp.com', + 'data': [], + 'external_dependencies': { + 'python': ['statsd'], + }, + 'installable': True, + } diff --git a/monitoring_statsd/models/__init__.py b/monitoring_statsd/models/__init__.py new file mode 100644 index 0000000..d9b809c --- /dev/null +++ b/monitoring_statsd/models/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import ir_http diff --git a/monitoring_statsd/models/ir_http.py b/monitoring_statsd/models/ir_http.py new file mode 100644 index 0000000..c2cc0de --- /dev/null +++ b/monitoring_statsd/models/ir_http.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +from openerp import models +from openerp.http import request + +from ..statsd_client import statsd, customer, environment + + +class IrHttp(models.AbstractModel): + _inherit = 'ir.http' + + def _dispatch(self): + if not statsd: + return super(IrHttp, self)._dispatch() + + path_info = request.httprequest.environ.get('PATH_INFO') + if path_info.startswith('/longpolling/'): + return super(IrHttp, self)._dispatch() + + parts = ['http', ] + if path_info.startswith('/web/dataset/call_button'): + parts += ['button', + customer, environment, + request.params['model'].replace('.', '_'), + request.params['method'], + ] + elif path_info.startswith('/web/dataset/exec_workflow'): + parts += ['workflow', + customer, environment, + request.params['model'].replace('.', '_'), + request.params['signal'], + ] + else: + parts += ['request', + customer, environment, + ] + + with statsd.timer('.'.join(parts)): + return super(IrHttp, self)._dispatch() diff --git a/monitoring_statsd/statsd_client.py b/monitoring_statsd/statsd_client.py new file mode 100644 index 0000000..4503915 --- /dev/null +++ b/monitoring_statsd/statsd_client.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- + +from __future__ import absolute_import + +import logging +import os + +from distutils.util import strtobool + +from openerp.tools.config import config + +_logger = logging.getLogger(__name__) + +try: + from statsd import defaults + from statsd.client import StatsClient +except ImportError: + _logger.warning('statds must be installed') + defaults = None # noqa + StatsClient = None # noqa + + +def is_true(strval): + return bool(strtobool(strval or '0'.lower())) + + +statsd_active = is_true(os.environ.get('ODOO_STATSD')) + +statsd = None +customer = None +environment = None +if statsd_active and statsd is None and StatsClient is not None: + if not os.environ.get('STATSD_CUSTOMER'): + raise Exception( + 'STATSD_CUSTOMER must contain the name of the customer' + ) + if os.environ.get('STATSD_ENVIRONMENT'): + environment = os.environ['STATSD_ENVIRONMENT'] + elif config.get('running_env'): + environment = config['running_env'] + else: + raise Exception( + 'Either STATSD_ENVIRONMENT or configuration option running_env ' + 'must contain the environment (prod, integration, ...)' + ) + + host = os.getenv('STATSD_HOST', defaults.HOST) + port = int(os.getenv('STATSD_PORT', defaults.PORT)) + prefix = os.getenv('STATSD_PREFIX', defaults.PREFIX) + maxudpsize = int(os.getenv('STATSD_MAXUDPSIZE', defaults.MAXUDPSIZE)) + ipv6 = bool(int(os.getenv('STATSD_IPV6', defaults.IPV6))) + statsd = StatsClient(host=host, port=port, prefix='odoo', + maxudpsize=maxudpsize, ipv6=ipv6)