diff --git a/monitoring_log_requests/README.rst b/monitoring_log_requests/README.rst new file mode 100644 index 0000000..9b4af8d --- /dev/null +++ b/monitoring_log_requests/README.rst @@ -0,0 +1,17 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :alt: License + +Monitoring: Requests Logging +============================ + +This addon is used to output in the logs informations about the user's +requests. Data such as *what* is the request, *who* requested it and *how +much* time did it took to complete could then be extracted from the logs and +loaded in an analysis tool such as ElasticSearch/Kibana. + +Each log line is a JSON with the monitored fields, so it is easier to parse. + +The logs are prefixed with ``monitoring.http.requests`` so be sure to enable +this path in the log handler:: + + - LOG_HANDLER=":WARNING,monitoring.http.requests:INFO" diff --git a/monitoring_log_requests/__init__.py b/monitoring_log_requests/__init__.py new file mode 100644 index 0000000..cde864b --- /dev/null +++ b/monitoring_log_requests/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import models diff --git a/monitoring_log_requests/__openerp__.py b/monitoring_log_requests/__openerp__.py new file mode 100644 index 0000000..99fc1af --- /dev/null +++ b/monitoring_log_requests/__openerp__.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + + +{'name': 'Monitoring: Requests Logging', + 'version': '9.0.1.0.0', + 'author': 'Camptocamp', + 'license': 'AGPL-3', + 'category': 'category', + 'depends': ['base'], + 'website': 'http://www.camptocamp.com', + 'data': [], + 'installable': True, + } diff --git a/monitoring_log_requests/models/__init__.py b/monitoring_log_requests/models/__init__.py new file mode 100644 index 0000000..d9b809c --- /dev/null +++ b/monitoring_log_requests/models/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import ir_http diff --git a/monitoring_log_requests/models/ir_http.py b/monitoring_log_requests/models/ir_http.py new file mode 100644 index 0000000..1275714 --- /dev/null +++ b/monitoring_log_requests/models/ir_http.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +import json +import logging +import time + +from openerp import models +from openerp.http import request +from openerp.tools.config import config + + +_logger = logging.getLogger('monitoring.http.requests') + + +class IrHttp(models.AbstractModel): + _inherit = 'ir.http' + + def _dispatch(self): + begin = time.time() + response = super(IrHttp, self)._dispatch() + end = time.time() + info = self._monitoring_info(response, begin, end) + self._monitoring_log(info) + return response + + def _monitoring_info(self, response, begin, end): + info = { + # timing + 'start_time': time.strftime("%Y-%m-%d %H:%M:%S", + time.gmtime(begin)), + 'duration': end - begin, + # HTTP things + 'method': request.httprequest.method, + 'url': request.httprequest.url, + 'path': request.httprequest.environ.get('PATH_INFO'), + 'content_type': request.httprequest.environ.get('CONTENT_TYPE'), + 'user_agent': request.httprequest.environ.get('HTTP_USER_AGENT'), + # Odoo things + 'db': None, + 'uid': request.uid, + 'login': None, + 'json_method': None, + 'server_environment': config.get('running_env'), + 'model': None, + 'model_method': None, + # response things + 'response_status_code': response.status_code, + } + if hasattr(request, 'session'): + info.update({ + 'login': request.session.get('login'), + 'db': request.session.get('db'), + }) + if hasattr(request, 'jsonrequest'): + info['json_method'] = request.jsonrequest.get('method') + if hasattr(request, 'params'): + info.update({ + 'model': request.params.get('model'), + 'model_method': request.params.get('method'), + }) + return info + + def _monitoring_log(self, info): + _logger.info(json.dumps(info))