Skip to content

fix(dbapi): avoid IndexError in get_operation_name for comment/whites… - #4934

Open
Atishyy27 wants to merge 2 commits into
open-telemetry:mainfrom
Atishyy27:fix/dbapi-operation-name-empty-tokens
Open

fix(dbapi): avoid IndexError in get_operation_name for comment/whites…#4934
Atishyy27 wants to merge 2 commits into
open-telemetry:mainfrom
Atishyy27:fix/dbapi-operation-name-empty-tokens

Conversation

@Atishyy27

Copy link
Copy Markdown

Description

get_operation_name in the DB-API instrumentation strips a leading SQL comment and
takes the first token as the operation name. If a statement is truthy but has no tokens
left after stripping (a comment-only or whitespace-only statement), .split()[0] raises
IndexError, which propagates out of the instrumented execute() call and breaks it.

This guards the result the same way the adjacent _Template branch already does
(tokens[0] if tokens else "") and returns an empty operation name instead. It extends the
empty-string handling added in #2643 to the comment-only / whitespace-only case.

No open issue for this one — found while reading the operation-name parsing. Related: #2643.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Added test_operation_name_no_tokens_after_comment_strip in test_dbapi_integration.py,
which executes a comment-only (/* comment only */) and a whitespace-only (" ")
statement and asserts instrumentation does not raise. Verified locally that it passes and
that the existing test_span_succeeded still passes.

Does This PR Require a Core Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@Atishyy27
Atishyy27 requested a review from a team as a code owner August 6, 2026 23:59
…pace-only statements

A statement that is truthy but has no tokens left after leading-comment or
whitespace stripping (e.g. a comment-only or whitespace-only query) made
`.split()[0]` raise IndexError, which propagates out of the instrumented
execute call. Guard the result the same way the `_Template` branch already
does and return an empty operation name instead. Adds a regression test.

Signed-off-by: Atishyy27 <142108881+Atishyy27@users.noreply.github.com>
@Atishyy27
Atishyy27 force-pushed the fix/dbapi-operation-name-empty-tokens branch from ae76d02 to d95c7b3 Compare August 7, 2026 00:00

@henry3260 henry3260 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! Just some nits.
I think we should add a changelog fragment under .changelog/ instead of editing CHANGELOG.md directly.

cursor.execute(query)
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 2)
for span in spans_list:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we wrap the loop body in self.subTest(query=query)? Then the failure output names which query broke, and one failing input doesn't stop the other from running.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in bddd827
wrapped the loop body in self.subTest(query=query), so a failing input names its query and one bad case doesn't stop the others. thanks!

if query and isinstance(query, str):
# Strip leading comments so we get the operation name.
return self._leading_comment_remover.sub("", query).split()[0]
tokens = self._leading_comment_remover.sub("", query).split()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same shape exists in psycopg2's get_operation_name override — it has no guard at all, so even conn.execute("") from #2643 still raises.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. yeah, psycopg2's get_operation_name override has the same unguarded .split()[0], so conn.execute("") from #2643 still raises there. i'll fix psycopg2 (and check psycopg + sqlalchemy for the same shape) in a separate PR to keep this one a single logical change, rather than bundling it here.

… empty-token test

Signed-off-by: Atishyy27 <142108881+Atishyy27@users.noreply.github.com>
@Atishyy27

Copy link
Copy Markdown
Author

also moved the changelog to a .changelog/4934.fixed fragment per your note.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants