Handle empty statement in psycopg instrumentation (#2644)

This commit is contained in:
Hollis Wu
2024-07-02 12:05:41 -04:00
committed by GitHub
parent df3415b0dd
commit 90211489a3
3 changed files with 9 additions and 2 deletions

View File

@ -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))

View File

@ -269,7 +269,8 @@ class CursorTracer(dbapi.CursorTracer):
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]

View File

@ -245,14 +245,18 @@ class TestPostgresqlIntegration(PostgresqlIntegrationTestMixin, TestBase):
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):