You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

  1. import logging
  2. from odoo.http import WebRequest
  3. from odoo import http
  4. from odoo.sql_db import Cursor
  5. from .models.profiler_profile import ProfilerProfile
  6. _logger = logging.getLogger(__name__)
  7. def patch_web_request_call_function():
  8. """Modify Odoo entry points so that profile can record.
  9. Odoo is a multi-threaded program. Therefore, the :data:`profile` object
  10. needs to be enabled/disabled each in each thread to capture all the
  11. execution.
  12. For instance, Odoo spawns a new thread for each request.
  13. """
  14. _logger.info('Patching http.WebRequest._call_function')
  15. webreq_f_origin = WebRequest._call_function
  16. def webreq_f(*args, **kwargs):
  17. with ProfilerProfile.profiling():
  18. return webreq_f_origin(*args, **kwargs)
  19. WebRequest._call_function = webreq_f
  20. def patch_cursor_init():
  21. _logger.info('Patching sql_dp.Cursor.__init__')
  22. cursor_f_origin = Cursor.__init__
  23. def init_f(self, *args, **kwargs):
  24. cursor_f_origin(self, *args, **kwargs)
  25. enable = ProfilerProfile.activate_deactivate_pglogs
  26. if enable is not None:
  27. self._obj.execute('SET log_min_duration_statement TO "%s"' %
  28. ((not enable) * -1,))
  29. Cursor.__init__ = init_f
  30. def patch_dispatch_rpc():
  31. _logger.info('Patching Dispatch RPC http.dispatch_rpc')
  32. dispatch_rpc = http.dispatch_rpc
  33. def dispatch_rpc_f(service_name, method, params):
  34. with ProfilerProfile.profiling():
  35. return dispatch_rpc(service_name, method, params)
  36. http.dispatch_rpc = dispatch_rpc_f
  37. def post_load():
  38. patch_web_request_call_function()
  39. patch_cursor_init()
  40. patch_dispatch_rpc()