From 541082686c471fc1280dcb9c639a9da5520cc856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul=20=28ACSONE=29?= Date: Sat, 23 Nov 2019 12:53:33 +0100 Subject: [PATCH] [ADD] slow_statement_logger --- .../odoo/addons/slow_statement_logger | 1 + setup/slow_statement_logger/setup.py | 6 + slow_statement_logger/README.rst | 94 ++++ slow_statement_logger/__init__.py | 41 ++ slow_statement_logger/__manifest__.py | 9 + slow_statement_logger/readme/CONTRIBUTORS.rst | 1 + slow_statement_logger/readme/DESCRIPTION.rst | 8 + slow_statement_logger/readme/USAGE.rst | 10 + .../static/description/index.html | 439 ++++++++++++++++++ 9 files changed, 609 insertions(+) create mode 120000 setup/slow_statement_logger/odoo/addons/slow_statement_logger create mode 100644 setup/slow_statement_logger/setup.py create mode 100644 slow_statement_logger/README.rst create mode 100644 slow_statement_logger/__init__.py create mode 100644 slow_statement_logger/__manifest__.py create mode 100644 slow_statement_logger/readme/CONTRIBUTORS.rst create mode 100644 slow_statement_logger/readme/DESCRIPTION.rst create mode 100644 slow_statement_logger/readme/USAGE.rst create mode 100644 slow_statement_logger/static/description/index.html diff --git a/setup/slow_statement_logger/odoo/addons/slow_statement_logger b/setup/slow_statement_logger/odoo/addons/slow_statement_logger new file mode 120000 index 000000000..d08fd41d6 --- /dev/null +++ b/setup/slow_statement_logger/odoo/addons/slow_statement_logger @@ -0,0 +1 @@ +../../../../slow_statement_logger \ No newline at end of file diff --git a/setup/slow_statement_logger/setup.py b/setup/slow_statement_logger/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/slow_statement_logger/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/slow_statement_logger/README.rst b/slow_statement_logger/README.rst new file mode 100644 index 000000000..765b21218 --- /dev/null +++ b/slow_statement_logger/README.rst @@ -0,0 +1,94 @@ +========================= +Slow SQL Statement Logger +========================= + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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/12.0/slow_statement_logger + :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-12-0/server-tools-12-0-slow_statement_logger + :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/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module lets you log slow SQL statements. It is useful when you don't have +access to the PostgreSQL log or when it is difficult to correlate the +PostgreSQL log with the Odoo log. + +.. warning:: + + This module may leak confidential data in the log, in a similar way + to Odoo's ``--log-level=debug_sql`` option. Use with care. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Add ``slow_statement_logger`` to Odoo's ``--load`` command line option, or to the +``server_wide_modules`` configuration file entry. + +Add a ``log_min_duration_statement`` entry in the ``options`` section of your +Odoo configuration file. Statements running at least this number of +milliseconds will be logged with a *warning* level in the +``odoo.addons.slow_statement_logger`` handler. ``0`` means all statements will be +logged. ``-1`` disables this logging. You can also set an environment variable +``ODOO_LOG_MIN_DURATION_STATEMENT`` which will have priority over the +configuration file entry. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub 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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* ACSONE SA/NV + +Contributors +~~~~~~~~~~~~ + +* stephane.bidoul@acsone.eu + +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 `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/slow_statement_logger/__init__.py b/slow_statement_logger/__init__.py new file mode 100644 index 000000000..b06af6ecd --- /dev/null +++ b/slow_statement_logger/__init__.py @@ -0,0 +1,41 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +# Copyright (c) ACSONE SA/NV 2019 + +import logging +import os +import time + +import psycopg2 + +from odoo import sql_db +from odoo.tools import config + +_logger = logging.getLogger(__name__) + +# TODO Odoo option? server_environment? +LOG_MIN_DURATION_STATEMENT = int(config.get("log_min_duration_statement", "-1")) +ENV_VAR = "ODOO_LOG_MIN_DURATION_STATEMENT" +if ENV_VAR in os.environ: + LOG_MIN_DURATION_STATEMENT = int(os.environ.get(ENV_VAR, "-1")) + + +class SlowStatementLoggingCursor(sql_db.Cursor): + def execute(self, query, params=None, log_exceptions=None): + if LOG_MIN_DURATION_STATEMENT >= 0: + start = time.perf_counter() + res = super().execute(query, params, log_exceptions) + duration = (time.perf_counter() - start) * 1000.0 + if duration >= LOG_MIN_DURATION_STATEMENT: + # same logging technique as Odoo in sql_log mode + encoding = psycopg2.extensions.encodings[self.connection.encoding] + _logger.warning( + "duration: %.3f ms statement: %s", + duration, + self._obj.mogrify(query, params).decode(encoding, "replace"), + ) + return res + else: + return super().execute(query, params, log_exceptions) + + +sql_db.Cursor = SlowStatementLoggingCursor diff --git a/slow_statement_logger/__manifest__.py b/slow_statement_logger/__manifest__.py new file mode 100644 index 000000000..88988c7ac --- /dev/null +++ b/slow_statement_logger/__manifest__.py @@ -0,0 +1,9 @@ +{ + "name": "Slow SQL Statement Logger", + "summary": "Log slow SQL statements", + "version": "12.0.1.0.0", + "author": "ACSONE SA/NV, Odoo Community Association (OCA)", + "license": "AGPL-3", + "website": "https://github.com/OCA/server-tools/", + "category": "Tools", +} diff --git a/slow_statement_logger/readme/CONTRIBUTORS.rst b/slow_statement_logger/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..f78ab73d7 --- /dev/null +++ b/slow_statement_logger/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* stephane.bidoul@acsone.eu diff --git a/slow_statement_logger/readme/DESCRIPTION.rst b/slow_statement_logger/readme/DESCRIPTION.rst new file mode 100644 index 000000000..8fd9289f3 --- /dev/null +++ b/slow_statement_logger/readme/DESCRIPTION.rst @@ -0,0 +1,8 @@ +This module lets you log slow SQL statements. It is useful when you don't have +access to the PostgreSQL log or when it is difficult to correlate the +PostgreSQL log with the Odoo log. + +.. warning:: + + This module may leak confidential data in the log, in a similar way + to Odoo's ``--log-level=debug_sql`` option. Use with care. diff --git a/slow_statement_logger/readme/USAGE.rst b/slow_statement_logger/readme/USAGE.rst new file mode 100644 index 000000000..44bc92200 --- /dev/null +++ b/slow_statement_logger/readme/USAGE.rst @@ -0,0 +1,10 @@ +Add ``slow_statement_logger`` to Odoo's ``--load`` command line option, or to the +``server_wide_modules`` configuration file entry. + +Add a ``log_min_duration_statement`` entry in the ``options`` section of your +Odoo configuration file. Statements running at least this number of +milliseconds will be logged with a *warning* level in the +``odoo.addons.slow_statement_logger`` handler. ``0`` means all statements will be +logged. ``-1`` disables this logging. You can also set an environment variable +``ODOO_LOG_MIN_DURATION_STATEMENT`` which will have priority over the +configuration file entry. diff --git a/slow_statement_logger/static/description/index.html b/slow_statement_logger/static/description/index.html new file mode 100644 index 000000000..f30759627 --- /dev/null +++ b/slow_statement_logger/static/description/index.html @@ -0,0 +1,439 @@ + + + + + + +Slow SQL Statement Logger + + + +
+

Slow SQL Statement Logger

+ + +

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runbot

+

This module lets you log slow SQL statements. It is useful when you don’t have +access to the PostgreSQL log or when it is difficult to correlate the +PostgreSQL log with the Odoo log.

+
+

Warning

+

This module may leak confidential data in the log, in a similar way +to Odoo’s --log-level=debug_sql option. Use with care.

+
+

Table of contents

+ +
+

Usage

+

Add slow_statement_logger to Odoo’s --load command line option, or to the +server_wide_modules configuration file entry.

+

Add a log_min_duration_statement entry in the options section of your +Odoo configuration file. Statements running at least this number of +milliseconds will be logged with a warning level in the +odoo.addons.slow_statement_logger handler. 0 means all statements will be +logged. -1 disables this logging. You can also set an environment variable +ODOO_LOG_MIN_DURATION_STATEMENT which will have priority over the +configuration file entry.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub 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.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • ACSONE SA/NV
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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 project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ +