mirror of
https://github.com/camptocamp/odoo-cloud-platform.git
synced 2026-06-23 18:04:34 +00:00
Add monitoring_log_requests
Addons that logs requests for usage analysis
This commit is contained in:
@@ -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"
|
||||
@@ -0,0 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from . import models
|
||||
@@ -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,
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from . import ir_http
|
||||
@@ -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))
|
||||
Reference in New Issue
Block a user