diff --git a/CHANGELOG.md b/CHANGELOG.md index 52bc006893..a6f5600bb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#4141](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4141)) - `opentelemetry-instrumentation-pyramid`: pass request attributes at span creation ([#4139](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4139)) +- `opentelemetry-instrumentation-psycopg2`: Add parameter `capture_parameters` to instrumentor. + ([#4212](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4212)) ### Fixed diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py index f5e113a47e..f04047e925 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py @@ -136,6 +136,19 @@ Warning: Capture of sqlcomment in ``db.statement`` may have high cardinality without platform normalization. See `Semantic Conventions for database spans `_ for more information. +Capture parameters +****************** +By default, only statements are captured, without the associated query parameters. +To capture query parameters in the span attribute `db.statement.parameters`, enable `capture_parameters`. + +.. code:: python + + from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor + + Psycopg2Instrumentor().instrument( + capture_parameters=True, + ) + API --- """ @@ -202,6 +215,7 @@ def _instrument(self, **kwargs): enable_attribute_commenter = kwargs.get( "enable_attribute_commenter", False ) + capture_parameters = kwargs.get("capture_parameters", False) dbapi.wrap_connect( __name__, psycopg2, @@ -214,6 +228,7 @@ def _instrument(self, **kwargs): enable_commenter=enable_sqlcommenter, commenter_options=commenter_options, enable_attribute_commenter=enable_attribute_commenter, + capture_parameters=capture_parameters, ) def _uninstrument(self, **kwargs): diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_instrumentation.py b/instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_instrumentation.py index 93a17d9ed1..1847bb5106 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_instrumentation.py @@ -15,14 +15,21 @@ from unittest import TestCase from unittest.mock import Mock, call, patch +import psycopg2 + from opentelemetry.instrumentation.auto_instrumentation._load import ( _load_instrumentors, ) -from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor +from opentelemetry.instrumentation.psycopg2 import ( + DatabaseApiIntegration, + Psycopg2Instrumentor, +) from opentelemetry.instrumentation.psycopg2.package import ( _instruments_psycopg2, _instruments_psycopg2_binary, ) +from opentelemetry.instrumentation.psycopg2.version import __version__ +from opentelemetry.test.test_base import TestBase class TestPsycopg2InstrumentationDependencies(TestCase): @@ -160,3 +167,41 @@ def _instrumentation_loaded_successfully_call(): mock_logger.debug.assert_has_calls( [_instrumentation_loaded_successfully_call()] ) + + +@patch("opentelemetry.instrumentation.psycopg2.dbapi") +class TestPsycopg2InstrumentorParameters(TestBase): + def tearDown(self): + super().tearDown() + with self.disable_logging(): + Psycopg2Instrumentor().uninstrument() + + def test_instrument_defaults(self, mock_dbapi): + Psycopg2Instrumentor().instrument() + + mock_dbapi.wrap_connect.assert_called_once_with( + "opentelemetry.instrumentation.psycopg2", + psycopg2, + "connect", + "postgresql", + { + "database": "info.dbname", + "port": "info.port", + "host": "info.host", + "user": "info.user", + }, + version=__version__, + tracer_provider=None, + db_api_integration_factory=DatabaseApiIntegration, + enable_commenter=False, + commenter_options={}, + enable_attribute_commenter=False, + capture_parameters=False, + ) + + def test_instrument_capture_parameters(self, mock_dbapi): + Psycopg2Instrumentor().instrument(capture_parameters=True) + + self.assertTrue( + mock_dbapi.wrap_connect.call_args.kwargs["capture_parameters"] + )