Skip to content

Commit

Permalink
Handle empty statement in psycopg instrumentation (open-telemetry#2644)
Browse files Browse the repository at this point in the history
  • Loading branch information
Holi0317 authored Jul 2, 2024
1 parent df3415b commit 9021148
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 9021148

Please sign in to comment.