diff --git a/CHANGELOG.md b/CHANGELOG.md index e4dcc10c7b..19002c1b75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -388,6 +388,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1780](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1780)) - Add metric instrumentation for celery ([#1679](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1679)) +- Added Enable_commenter for mysql + ([#1834](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1834)) - `opentelemetry-instrumentation-asgi` Add `http.server.response.size` metric ([#1789](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1789)) - `opentelemetry-instrumentation-grpc` Allow gRPC connections via Unix socket diff --git a/instrumentation/opentelemetry-instrumentation-mysql/src/opentelemetry/instrumentation/mysql/__init__.py b/instrumentation/opentelemetry-instrumentation-mysql/src/opentelemetry/instrumentation/mysql/__init__.py index d88dffde2b..8af9e6ed11 100644 --- a/instrumentation/opentelemetry-instrumentation-mysql/src/opentelemetry/instrumentation/mysql/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-mysql/src/opentelemetry/instrumentation/mysql/__init__.py @@ -18,6 +18,53 @@ .. _mysql-connector: https://pypi.org/project/mysql-connector/ +SQLCOMMENTER +***************************************** +You can optionally configure MySQL instrumentation to enable sqlcommenter which enriches +the query with contextual information. +Usage +----- + +.. code:: python + + from opentelemetry.instrumentation.mysql import MySQLInstrumentor() + MySQLInstrumentor().instrument(enable_commenter=True, commenter_options={}) + +For example, + +:: + + Invoking cursor.execute("select * from auth_users") will lead to sql query "select * from auth_users" but when SQLCommenter is enabled + the query will get appended with some configurable tags like "select * from auth_users /*tag=value*/;" + +SQLCOMMENTER Configurations +*************************** +We can configure the tags to be appended to the sqlquery log by adding configuration inside commenter_options(default:{}) keyword +db_driver = True(Default) or False +For example, +:: +Enabling this flag will add mysql and its version. +dbapi_threadsafety = True(Default) or False +For example, +:: +Enabling this flag will add threadsafety /*dbapi_threadsafety=2*/ +dbapi_level = True(Default) or False +For example, +:: +Enabling this flag will add dbapi_level /*dbapi_level='2.0'*/ +libpq_version = True(Default) or False +For example, +:: +Enabling this flag will add libpq_version /*libpq_version=140001*/ +driver_paramstyle = True(Default) or False +For example, +:: +Enabling this flag will add driver_paramstyle /*driver_paramstyle='pyformat'*/ +opentelemetry_values = True(Default) or False +For example, +:: +Enabling this flag will add traceparent values /*traceparent='00-03afa25236b8cd948fa853d67038ac79-405ff022e8247c46-01'*/ + Usage ----- @@ -66,6 +113,8 @@ def _instrument(self, **kwargs): https://dev.mysql.com/doc/connector-python/en/ """ tracer_provider = kwargs.get("tracer_provider") + enable_sqlcommenter = kwargs.get("enable_commenter", False) + commenter_options = kwargs.get("commenter_options", {}) dbapi.wrap_connect( __name__, @@ -75,6 +124,8 @@ def _instrument(self, **kwargs): self._CONNECTION_ATTRIBUTES, version=__version__, tracer_provider=tracer_provider, + enable_commenter=enable_sqlcommenter, + commenter_options=commenter_options, ) def _uninstrument(self, **kwargs): diff --git a/instrumentation/opentelemetry-instrumentation-mysql/tests/test_mysql_integration.py b/instrumentation/opentelemetry-instrumentation-mysql/tests/test_mysql_integration.py index 3614febffd..e3f47b8c9a 100644 --- a/instrumentation/opentelemetry-instrumentation-mysql/tests/test_mysql_integration.py +++ b/instrumentation/opentelemetry-instrumentation-mysql/tests/test_mysql_integration.py @@ -117,3 +117,25 @@ def test_uninstrument_connection(self, mock_connect): spans_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans_list), 1) + + @mock.patch("mysql.connector.connect") + # pylint: disable=unused-argument + def test_sqlcommenter_enabled(self, event_mocked): + MySQLInstrumentor().instrument(enable_commenter=True) + cnx = mysql.connector.connect(database="test") + query = "SELECT * FROM test" + cursor = cnx.cursor() + cursor.execute(query) + kwargs = event_mocked.call_args[1] + self.assertEqual(kwargs["enable_commenter"], True) + + @mock.patch("mysql.connector.connect") + # pylint: disable=unused-argument + def test_sqlcommenter_disabled(self, event_mocked): + MySQLInstrumentor().instrument(enable_commenter=False) + cnx = mysql.connector.connect(database="test") + query = "SELECT * FROM test" + cursor = cnx.cursor() + cursor.execute(query) + kwargs = event_mocked.call_args[1] + self.assertEqual(kwargs["enable_commenter"], False)