From 90211489a3e7c094d4084166b28f504c82a1315c Mon Sep 17 00:00:00 2001 From: Hollis Wu Date: Tue, 2 Jul 2024 12:05:41 -0400 Subject: [PATCH] Handle empty statement in psycopg instrumentation (#2644) --- CHANGELOG.md | 2 ++ .../src/opentelemetry/instrumentation/psycopg/__init__.py | 3 ++- .../tests/test_psycopg_integration.py | 6 +++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4a42d1c36..f597d64da9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#2590](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2590)) - Reference symbols from generated semantic conventions ([#2611](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2611)) +- `opentelemetry-instrumentation-psycopg` Bugfix: Handle empty statement. + ([#2644](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2644)) - `opentelemetry-instrumentation-confluent-kafka` Confluent Kafka: Ensure consume span is ended when consumer is closed ([#2640](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2640)) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py index 5d7054151a..4f61713b29 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg/__init__.py @@ -269,7 +269,8 @@ def get_operation_name(self, cursor, args): if isinstance(statement, Composed): statement = statement.as_string(cursor) - if isinstance(statement, str): + # `statement` can be empty string. See #2643 + if statement and isinstance(statement, str): # Strip leading comments so we get the operation name. return self._leading_comment_remover.sub("", statement).split()[0] diff --git a/instrumentation/opentelemetry-instrumentation-psycopg/tests/test_psycopg_integration.py b/instrumentation/opentelemetry-instrumentation-psycopg/tests/test_psycopg_integration.py index 5a5b39d80b..dc9969ba8c 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg/tests/test_psycopg_integration.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg/tests/test_psycopg_integration.py @@ -245,14 +245,18 @@ def test_span_name(self): cursor.execute("/* leading comment */ query") cursor.execute("/* leading comment */ query /* trailing comment */") cursor.execute("query /* trailing comment */") + cursor.execute("") + cursor.execute("--") spans_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(spans_list), 6) + self.assertEqual(len(spans_list), 8) self.assertEqual(spans_list[0].name, "Test") self.assertEqual(spans_list[1].name, "multi") self.assertEqual(spans_list[2].name, "tab") self.assertEqual(spans_list[3].name, "query") self.assertEqual(spans_list[4].name, "query") self.assertEqual(spans_list[5].name, "query") + self.assertEqual(spans_list[6].name, "postgresql") + self.assertEqual(spans_list[7].name, "--") # pylint: disable=unused-argument def test_not_recording(self):