Skip to content

missing capture_parameters in instrumentation psycopg #3676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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 @@ -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 @@ -258,6 +258,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 @@ -473,6 +492,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