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 @@ -28,6 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3666](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3666))
- `opentelemetry-sdk-extension-aws` Add AWS X-Ray Remote Sampler with initial Rules Poller implementation
([#3366](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3366))
- `opentelemetry-instrumentation-psycopg` Add missing parameter `capture_parameters` to instrumentor.
([#3676](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3676))

## Version 1.36.0/0.57b0 (2025-07-29)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def _instrument(self, **kwargs: Any):
enable_attribute_commenter = kwargs.get(
"enable_attribute_commenter", False
)
capture_parameters = kwargs.get("capture_parameters", False)
dbapi.wrap_connect(
__name__,
psycopg,
Expand All @@ -195,6 +196,7 @@ def _instrument(self, **kwargs: Any):
enable_commenter=enable_sqlcommenter,
commenter_options=commenter_options,
enable_attribute_commenter=enable_attribute_commenter,
capture_parameters=capture_parameters,
)

dbapi.wrap_connect(
Expand All @@ -209,6 +211,7 @@ def _instrument(self, **kwargs: Any):
enable_commenter=enable_sqlcommenter,
commenter_options=commenter_options,
enable_attribute_commenter=enable_attribute_commenter,
capture_parameters=capture_parameters,
)
dbapi.wrap_connect(
__name__,
Expand All @@ -222,6 +225,7 @@ def _instrument(self, **kwargs: Any):
enable_commenter=enable_sqlcommenter,
commenter_options=commenter_options,
enable_attribute_commenter=enable_attribute_commenter,
capture_parameters=capture_parameters,
)

def _uninstrument(self, **kwargs: Any):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,25 @@ def test_span_name(self):
self.assertEqual(spans_list[6].name, "postgresql")
self.assertEqual(spans_list[7].name, "--")

def test_span_params_attribute(self):
PsycopgInstrumentor().instrument(capture_parameters=True)
cnx = psycopg.connect(database="test")
query = "SELECT * FROM mytable WHERE myparam1 = %s AND myparam2 = %s"
params = ("test", 42)

cursor = cnx.cursor()

cursor.execute(query, params)
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
self.assertEqual(spans_list[0].name, "SELECT")
assert spans_list[0].attributes is not None
self.assertEqual(spans_list[0].attributes["db.statement"], query)
self.assertEqual(
spans_list[0].attributes["db.statement.parameters"],
str(params)
)

# pylint: disable=unused-argument
def test_not_recording(self):
mock_tracer = mock.Mock()
Expand Down Expand Up @@ -479,6 +498,25 @@ async def test_span_name_async(self):
self.assertEqual(spans_list[4].name, "query")
self.assertEqual(spans_list[5].name, "query")

async def test_span_params_attribute(self):
PsycopgInstrumentor().instrument(capture_parameters=True)
cnx = await psycopg.AsyncConnection.connect("test")
query = "SELECT * FROM mytable WHERE myparam1 = %s AND myparam2 = %s"
params = ("test", 42)
async with cnx.cursor() as cursor:

await cursor.execute(query, params)

spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
self.assertEqual(spans_list[0].name, "SELECT")
assert spans_list[0].attributes is not None
self.assertEqual(spans_list[0].attributes["db.statement"], query)
self.assertEqual(
spans_list[0].attributes["db.statement.parameters"],
str(params)
)

# pylint: disable=unused-argument
async def test_not_recording_async(self):
mock_tracer = mock.Mock()
Expand Down
Loading