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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@
Warning:
Capture of sqlcomment in ``db.statement`` 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 in the span attribute `db.statement.parameters`, enable `capture_parameters`.

.. code:: python

from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor

Psycopg2Instrumentor().instrument(
capture_parameters=True,
)

API
---
"""
Expand Down Expand Up @@ -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,
Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"]
)
Loading