Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@
Warning:
Capture of sqlcomment in ``db.statement``/``db.query.text`` may have high cardinality without platform normalization. See `Semantic Conventions for database spans <https://opentelemetry.io/docs/specs/semconv/database/database-spans/#generating-a-summary-of-the-query-text>`_ for more information.

Capture parameters
******************
By default, only statements are captured, without the associated query parameters.
To capture query parameters, enable ``capture_parameters``.

.. code:: python

from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor

PyMySQLInstrumentor().instrument(
capture_parameters=True,
)

API
---
"""
Expand Down Expand Up @@ -178,6 +191,7 @@ def _instrument(self, **kwargs): # pylint: disable=no-self-use
enable_attribute_commenter = kwargs.get(
"enable_attribute_commenter", False
)
capture_parameters = kwargs.get("capture_parameters", False)

dbapi.wrap_connect(
__name__,
Expand All @@ -190,6 +204,7 @@ def _instrument(self, **kwargs): # pylint: disable=no-self-use
enable_commenter=enable_sqlcommenter,
commenter_options=commenter_options,
enable_attribute_commenter=enable_attribute_commenter,
capture_parameters=capture_parameters,
)

def _uninstrument(self, **kwargs): # pylint: disable=no-self-use
Expand All @@ -203,6 +218,7 @@ def instrument_connection(
enable_commenter=None,
commenter_options=None,
enable_attribute_commenter=None,
capture_parameters=False,
):
"""Enable instrumentation in a PyMySQL connection.

Expand All @@ -221,6 +237,8 @@ def instrument_connection(
You can specify various options, such as enabling driver information, database version logging,
traceparent propagation, and other customizable metadata enhancements.
See *SQLCommenter Configurations* above for more information.
capture_parameters:
A flag to enable query parameter capture.
Returns:
An instrumented connection.
"""
Expand All @@ -236,6 +254,7 @@ def instrument_connection(
commenter_options=commenter_options,
connect_module=pymysql,
enable_attribute_commenter=enable_attribute_commenter,
capture_parameters=capture_parameters,
)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,29 @@ def test_instrument_connection_enable_commenter_dbapi_kwargs(
enable_commenter=True,
commenter_options={"foo": True},
enable_attribute_commenter=True,
capture_parameters=True,
)
cursor = cnx.cursor()
cursor.execute("SELECT * FROM test")
kwargs = mock_instrument_connection.call_args[1]
self.assertEqual(kwargs["enable_commenter"], True)
self.assertEqual(kwargs["commenter_options"], {"foo": True})
self.assertEqual(kwargs["enable_attribute_commenter"], True)
self.assertEqual(kwargs["capture_parameters"], True)

@mock.patch("opentelemetry.instrumentation.dbapi.instrument_connection")
@mock.patch("pymysql.connect")
# pylint: disable=unused-argument
def test_instrument_connection_dbapi_kwargs_defaults(
self,
mock_connect,
mock_instrument_connection,
):
cnx = pymysql.connect(database="test")
PyMySQLInstrumentor().instrument_connection(cnx)

kwargs = mock_instrument_connection.call_args[1]
self.assertEqual(kwargs["capture_parameters"], False)

def test_instrument_connection_with_dbapi_sqlcomment_enabled(self):
mock_connect_module = mock.MagicMock(
Expand Down Expand Up @@ -298,6 +314,20 @@ def test_instrument_enable_commenter_dbapi_kwargs(
self.assertEqual(kwargs["enable_commenter"], True)
self.assertEqual(kwargs["commenter_options"], {"foo": True})
self.assertEqual(kwargs["enable_attribute_commenter"], True)
self.assertEqual(kwargs["capture_parameters"], False)

@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
@mock.patch("pymysql.connect")
# pylint: disable=unused-argument
def test_instrument_capture_parameters_dbapi_kwargs(
self,
mock_connect,
mock_wrap_connect,
):
PyMySQLInstrumentor()._instrument(capture_parameters=True)

kwargs = mock_wrap_connect.call_args[1]
self.assertEqual(kwargs["capture_parameters"], True)

def test_instrument_with_dbapi_sqlcomment_enabled(
self,
Expand Down
Loading