From e2222cb2ff6103e4603e93e58f9834a2dc64a9cc Mon Sep 17 00:00:00 2001 From: Andrew Carter Date: Wed, 15 Oct 2025 15:20:38 -0500 Subject: [PATCH] opentelemetry-instrumentation-sqlalchemy: Fix exception on empty query --- CHANGELOG.md | 2 ++ .../opentelemetry/instrumentation/sqlalchemy/engine.py | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9814367242..3fbe3cc3a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- `opentelemetry-instrumentation-sqlalchemy`: Fix exception on empty query + ([#3860](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3860)) - `opentelemetry-instrumentation-botocore`: migrate off the deprecated events API to use the logs API ([#3624](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3624)) - `opentelemetry-instrumentation-dbapi`: fix crash retrieving libpq version when enabling commenter with psycopg diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py index 973a3d7c9c..265847c00c 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py @@ -239,9 +239,11 @@ def _operation_name(self, db_name, statement): # For some very special cases it might not record the correct statement if the SQL # dialect is too weird but in any case it shouldn't break anything. # Strip leading comments so we get the operation name. - parts.append( - self._leading_comment_remover.sub("", statement).split()[0] - ) + split_query = self._leading_comment_remover.sub( + "", statement + ).split() + if split_query: + parts.append(split_query[0]) if db_name: parts.append(db_name) if not parts: