fix(dbapi): avoid IndexError in get_operation_name for comment/whites… - #4934
fix(dbapi): avoid IndexError in get_operation_name for comment/whites…#4934Atishyy27 wants to merge 3 commits into
Conversation
ae76d02 to
d95c7b3
Compare
henry3260
left a comment
There was a problem hiding this comment.
Thanks for the fix! Just some nits.
I think we should add a changelog fragment under .changelog/ instead of editing CHANGELOG.md directly.
|
also moved the changelog to a .changelog/4934.fixed fragment per your note. |
|
Ran both this and #4936 against Reproduced. Calling
There is already a precedent in-tree, and it is the same fix. try:
name = self._leading_comment_remover.sub("", name).split()[0]
except IndexError:
name = ""So "no tokens left after stripping ⇒ empty operation name" is already this repo's answer to this exact input; dbapi and psycopg are the packages that never got it. Two sites are still uncovered after both PRs. Same probe, same clean checkout:
One thing neither PR changes, mentioned so it is not mistaken for a gap. The regex is AI-assisted; every result above is output from running the code on |
Pull request dashboard statusWaiting on maintainers · refreshed 2026-08-21 18:39 UTC Merge when ready. Status above doesn't look right?
|
Per xrmx's review on open-telemetry#4934: the test executed both regression cases in one loop, then asserted span count/names in a second pass after the loop ended, so a subTest failure wouldn't isolate which case actually failed. Moves the assertion inside each subTest and clears the exporter per iteration, so each case checks exactly its own single span.
…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>
… empty-token test Signed-off-by: Atishyy27 <142108881+Atishyy27@users.noreply.github.com>
Per xrmx's review on open-telemetry#4934: the test executed both regression cases in one loop, then asserted span count/names in a second pass after the loop ended, so a subTest failure wouldn't isolate which case actually failed. Moves the assertion inside each subTest and clears the exporter per iteration, so each case checks exactly its own single span.
90e00cb to
b3d2c41
Compare
…espace-only Composed statements A Composed statement that is truthy but has no tokens after leading-comment or whitespace stripping made `.split()[0]` raise IndexError in the psycopg instrumentation's get_operation_name override. Guard it the same way the dbapi base does, returning an empty operation name instead. Adds a regression test. Follow-up to open-telemetry#4934. Signed-off-by: Atishyy27 <142108881+Atishyy27@users.noreply.github.com>
…tespace-only statements A statement that is truthy but has no tokens after leading-comment or whitespace stripping made .split()[0] raise IndexError in the psycopg2 instrumentation's get_operation_name override. Guard it like the dbapi base does. Adds a regression test. Follow-up to open-telemetry#4934. Signed-off-by: Atishyy27 <142108881+Atishyy27@users.noreply.github.com>
…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>
This comment has been minimized.
This comment has been minimized.
|
Addressed the review feedback and added the related fixes for psycopg, psycopg2, and SQLAlchemy. The PR is ready for maintainer review/merge. |
Description
get_operation_namein the DB-API instrumentation strips a leading SQL comment andtakes 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]raisesIndexError, which propagates out of the instrumentedexecute()call and breaks it.This guards the result the same way the adjacent
_Templatebranch already does(
tokens[0] if tokens else "") and returns an empty operation name instead. It extends theempty-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
How Has This Been Tested?
Added
test_operation_name_no_tokens_after_comment_stripintest_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_succeededstill passes.Does This PR Require a Core Repo Change?
Checklist: