Skip to content

Commit b8befc5

Browse files
committed
fix(flask): stop reading deprecated flask.__version__
Flask 3.1 deprecated the module-level __version__ attribute and emits a DeprecationWarning on access; 3.2 will remove it. The Flask instrumentor was hitting that warning at import time via _IS_FLASK_31_PLUS = hasattr(flask, "__version__") and package_version.parse(flask.__version__) >= package_version.parse("3.1.0") even though it already computes the installed Flask version via importlib.metadata.version("flask") a few lines below. Use that flask_version for the 3.1+ check and drop the deprecated attribute access entirely. Fixes #4402 Signed-off-by: Ali <alliasgher123@gmail.com>
1 parent a912524 commit b8befc5

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
1212
## Unreleased
1313

14+
### Fixed
15+
16+
- `opentelemetry-instrumentation-flask`: Stop reading the deprecated `flask.__version__` attribute; resolve the Flask version via `importlib.metadata` only so import no longer emits a `DeprecationWarning` under Flask 3.1+
17+
([#4402](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/4402))
18+
1419
### Breaking changes
1520

1621
- Drop Python 3.9 support

instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,6 @@ def response_hook(span: Span, status: str, response_headers: List):
301301
)
302302

303303
_logger = getLogger(__name__)
304-
# Global constants for Flask 3.1+ streaming context cleanup
305-
_IS_FLASK_31_PLUS = hasattr(flask, "__version__") and package_version.parse(
306-
flask.__version__
307-
) >= package_version.parse("3.1.0")
308304

309305
_ENVIRON_STARTTIME_KEY = "opentelemetry-flask.starttime_key"
310306
_ENVIRON_SPAN_KEY = "opentelemetry-flask.span_key"
@@ -314,8 +310,16 @@ def response_hook(span: Span, status: str, response_headers: List):
314310

315311
_excluded_urls_from_env = get_excluded_urls("FLASK")
316312

313+
# Use importlib.metadata rather than flask.__version__: the latter has been
314+
# deprecated in Flask 3.1 and emits a DeprecationWarning on import; it is
315+
# scheduled for removal in Flask 3.2.
317316
flask_version = version("flask")
318317

318+
# Global constant for Flask 3.1+ streaming context cleanup.
319+
_IS_FLASK_31_PLUS = package_version.parse(flask_version) >= package_version.parse(
320+
"3.1.0"
321+
)
322+
319323
if package_version.parse(flask_version) >= package_version.parse("2.2.0"):
320324

321325
def _request_ctx_ref() -> weakref.ReferenceType:

0 commit comments

Comments
 (0)