Skip to content

fix(sqlalchemy): avoid IndexError in _operation_name for comment/whitespace-only statements - #4943

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

fix(sqlalchemy): avoid IndexError in _operation_name for comment/whitespace-only statements#4943
Atishyy27 wants to merge 4 commits into
open-telemetry:mainfrom
Atishyy27:fix/sqlalchemy-operation-name-empty-tokens

Conversation

@Atishyy27

Copy link
Copy Markdown

Same class as #4934, in the SQLAlchemy instrumentation's _operation_name: a comment-only / whitespace-only statement is truthy but has no tokens after leading-comment stripping, so .split()[0] raises IndexError. Guards it so such a statement falls back to the db name / vendor instead of crashing.

Type of change: Bug fix (non-breaking)

How Has This Been Tested? Added test_operation_name_comment_or_whitespace_only (unit test on _operation_name); existing tests still pass. Verified locally.

Does This PR Require a Core Repo Change? No.

Atishyy27 added a commit to Atishyy27/opentelemetry-python-contrib that referenced this pull request Aug 10, 2026
Signed-off-by: Atishyy27 <142108881+Atishyy27@users.noreply.github.com>
@Atishyy27
Atishyy27 requested a review from a team as a code owner August 10, 2026 09:22

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

Looks very nice! just leave some nits

Comment on lines +321 to +323
tokens = self._leading_comment_remover.sub("", statement).split()
if tokens:
parts.append(tokens[0])

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.

Falling back to the db name for the span name makes sense, but the same value is also used as db.operation.name, so that field ends up holding the database name (or the vendor, e.g. sqlite, when there is no db name). The conflation isn't introduced by this PR _operation_name has fed db.operation.name since #4110 , just noting it here. Might be worth a follow-up.

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.

Yeah, care to file a bug so we can track it?

Atishyy27 added a commit to Atishyy27/opentelemetry-python-contrib that referenced this pull request Aug 13, 2026
…n the regression test

Per henry3260's review on open-telemetry#4943: constructing EngineTracer directly
registers SQLAlchemy event listeners on the engine that the test never
removed. Clean them up via EngineTracer.remove_all_event_listeners()
in a finally block so they don't leak past the test.

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

opentelemetry-pr-dashboard Bot commented Aug 14, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-08-21 18:24 UTC

Respond to 1 review item (e.g. link a commit, explain why not, ask a follow-up):

  • Inline threads: 1
Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

Comment thread instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py Outdated
@xrmx xrmx moved this to Reviewed PRs that need fixes in Python PR digest Aug 17, 2026
Atishyy27 added a commit to Atishyy27/opentelemetry-python-contrib that referenced this pull request Aug 17, 2026
…xecute()

Per xrmx's review on open-telemetry#4943: the regression test called the internal
_operation_name method directly instead of going through actual
instrumentation. Rewritten to instrument a real engine and execute
comment-only/whitespace-only statements through it, asserting the resulting
span names (falls back to just the db name, ':memory:', with no operation
token) instead of calling the internal method.
…espace-only statements

A statement that is truthy but has no tokens after leading-comment or whitespace
stripping made .split()[0] raise IndexError in _operation_name. Guard it so such a
statement falls back to the db name / vendor instead of crashing. Adds a regression
test. Follow-up to open-telemetry#4934.

Signed-off-by: Atishyy27 <142108881+Atishyy27@users.noreply.github.com>
Signed-off-by: Atishyy27 <142108881+Atishyy27@users.noreply.github.com>
…n the regression test

Per henry3260's review on open-telemetry#4943: constructing EngineTracer directly
registers SQLAlchemy event listeners on the engine that the test never
removed. Clean them up via EngineTracer.remove_all_event_listeners()
in a finally block so they don't leak past the test.

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

Per xrmx's review on open-telemetry#4943: the regression test called the internal
_operation_name method directly instead of going through actual
instrumentation. Rewritten to instrument a real engine and execute
comment-only/whitespace-only statements through it, asserting the resulting
span names (falls back to just the db name, ':memory:', with no operation
token) instead of calling the internal method.
@Atishyy27
Atishyy27 force-pushed the fix/sqlalchemy-operation-name-empty-tokens branch from 626b010 to f477eaf Compare August 20, 2026 23:57
@xrmx xrmx moved this from Reviewed PRs that need fixes to Approved PRs in Python PR digest Aug 21, 2026
@opentelemetry-pr-dashboard

Copy link
Copy Markdown

Hi @Atishyy27 — just a friendly reminder that this pull request is waiting on you. The dashboard status comment has the open items and is kept current.

  • Replying is enough to hand it off — answer, explain why no change is needed, or ask a follow-up. The dashboard routes it onward once nothing on the list is waiting on you.
  • To hand it back for any other reason, including the dashboard getting this wrong, comment /dashboard route:reviewers.

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

Labels

None yet

Projects

Status: Approved PRs

Development

Successfully merging this pull request may close these issues.

3 participants