Skip to content

Commit c9d709a

Browse files
allen-k1mocelotl
andauthored
Feature/support for flask 3.0.0 (#2013)
* support flask 3.0.0 * support flask 3.0.0 * support flask 3.0.0 * modify tox.ini * modify Werkzeug version * modify Werkzeug version * modify flask test version * modify flask version * modify flask version * modify CHANGELOG.md * include feedback * include feedback * Fix lint and generate --------- Co-authored-by: Diego Hurtado <[email protected]>
1 parent 42faa1a commit c9d709a

File tree

7 files changed

+30
-13
lines changed

7 files changed

+30
-13
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7272

7373
- `opentelemetry-resource-detector-azure` Add resource detectors for Azure App Service and VM
7474
([#1901](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1901))
75+
- `opentelemetry-instrumentation-flask` Add support for Flask 3.0.0
76+
([#152](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2013))
7577

7678
## Version 1.19.0/0.40b0 (2023-07-13)
7779
- `opentelemetry-instrumentation-asgi` Add `http.server.request.size` metric

instrumentation/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
| [opentelemetry-instrumentation-elasticsearch](./opentelemetry-instrumentation-elasticsearch) | elasticsearch >= 2.0 | No
2020
| [opentelemetry-instrumentation-falcon](./opentelemetry-instrumentation-falcon) | falcon >= 1.4.1, < 4.0.0 | Yes
2121
| [opentelemetry-instrumentation-fastapi](./opentelemetry-instrumentation-fastapi) | fastapi ~= 0.58 | Yes
22-
| [opentelemetry-instrumentation-flask](./opentelemetry-instrumentation-flask) | flask >= 1.0, < 3.0 | Yes
22+
| [opentelemetry-instrumentation-flask](./opentelemetry-instrumentation-flask) | flask >= 1.0 | Yes
2323
| [opentelemetry-instrumentation-grpc](./opentelemetry-instrumentation-grpc) | grpcio ~= 1.27 | No
2424
| [opentelemetry-instrumentation-httpx](./opentelemetry-instrumentation-httpx) | httpx >= 0.18.0 | No
2525
| [opentelemetry-instrumentation-jinja2](./opentelemetry-instrumentation-jinja2) | jinja2 >= 2.7, < 4.0 | No

instrumentation/opentelemetry-instrumentation-flask/pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ dependencies = [
3535

3636
[project.optional-dependencies]
3737
instruments = [
38-
"flask >= 1.0, < 3.0",
39-
"werkzeug < 3.0.0"
38+
"werkzeug < 3.0.0",
39+
"flask >= 1.0",
4040
]
4141
test = [
4242
"opentelemetry-instrumentation-flask[instruments]",

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,16 @@ def response_hook(span: Span, status: str, response_headers: List):
251251
from opentelemetry import context, trace
252252
from opentelemetry.instrumentation.flask.package import _instruments
253253
from opentelemetry.instrumentation.flask.version import __version__
254+
255+
try:
256+
flask_version = flask.__version__
257+
except AttributeError:
258+
try:
259+
from importlib import metadata
260+
except ImportError:
261+
import importlib_metadata as metadata
262+
flask_version = metadata.version("flask")
263+
254264
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
255265
from opentelemetry.instrumentation.propagators import (
256266
get_global_response_propagator,
@@ -271,7 +281,7 @@ def response_hook(span: Span, status: str, response_headers: List):
271281

272282
_excluded_urls_from_env = get_excluded_urls("FLASK")
273283

274-
if package_version.parse(flask.__version__) >= package_version.parse("2.2.0"):
284+
if package_version.parse(flask_version) >= package_version.parse("2.2.0"):
275285

276286
def _request_ctx_ref() -> weakref.ReferenceType:
277287
return weakref.ref(flask.globals.request_ctx._get_current_object())
@@ -420,7 +430,7 @@ def _before_request():
420430
# https://flask.palletsprojects.com/en/1.1.x/api/#flask.has_request_context
421431
if flask and flask.request:
422432
if commenter_options.get("framework", True):
423-
flask_info["framework"] = f"flask:{flask.__version__}"
433+
flask_info["framework"] = f"flask:{flask_version}"
424434
if (
425435
commenter_options.get("controller", True)
426436
and flask.request.endpoint

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
# limitations under the License.
1414

1515

16-
_instruments = ("flask >= 1.0, < 3.0",)
16+
_instruments = ("flask >= 1.0",)
1717

1818
_supports_metrics = True

opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@
8585
"instrumentation": "opentelemetry-instrumentation-fastapi==0.44b0.dev",
8686
},
8787
{
88-
"library": "flask >= 1.0, < 3.0",
88+
"library": "werkzeug < 3.0.0",
8989
"instrumentation": "opentelemetry-instrumentation-flask==0.44b0.dev",
9090
},
9191
{
92-
"library": "werkzeug < 3.0.0",
92+
"library": "flask >= 1.0",
9393
"instrumentation": "opentelemetry-instrumentation-flask==0.44b0.dev",
9494
},
9595
{

tox.ini

+10-5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ envlist =
8989

9090
; opentelemetry-instrumentation-flask
9191
py3{7,8,9,10,11}-test-instrumentation-flask{213,220}
92+
py3{8,9,10,11}-test-instrumentation-flask{300}
9293
pypy3-test-instrumentation-flask{213,220}
9394

9495
; opentelemetry-instrumentation-urllib
@@ -273,7 +274,11 @@ deps =
273274
falcon2: falcon >=2.0.0,<3.0.0
274275
falcon3: falcon >=3.0.0,<4.0.0
275276
flask213: Flask ==2.1.3
276-
flask220: Flask >=2.2.0
277+
flask213: Werkzeug <3.0.0
278+
flask220: Flask ==2.2.0
279+
flask220: Werkzeug <3.0.0
280+
flask300: Flask >=3.0.0
281+
flask300: Werkzeug >=3.0.0
277282
grpc: pytest-asyncio
278283
sqlalchemy11: sqlalchemy>=1.1,<1.2
279284
sqlalchemy14: aiosqlite
@@ -324,7 +329,7 @@ changedir =
324329
test-instrumentation-elasticsearch{2,5,6}: instrumentation/opentelemetry-instrumentation-elasticsearch/tests
325330
test-instrumentation-falcon{1,2,3}: instrumentation/opentelemetry-instrumentation-falcon/tests
326331
test-instrumentation-fastapi: instrumentation/opentelemetry-instrumentation-fastapi/tests
327-
test-instrumentation-flask{213,220}: instrumentation/opentelemetry-instrumentation-flask/tests
332+
test-instrumentation-flask{213,220,300}: instrumentation/opentelemetry-instrumentation-flask/tests
328333
test-instrumentation-urllib: instrumentation/opentelemetry-instrumentation-urllib/tests
329334
test-instrumentation-urllib3v{1,2}: instrumentation/opentelemetry-instrumentation-urllib3/tests
330335
test-instrumentation-grpc: instrumentation/opentelemetry-instrumentation-grpc/tests
@@ -386,8 +391,8 @@ commands_pre =
386391

387392
grpc: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-grpc[test]
388393

389-
falcon{1,2,3},flask{213,220},django{1,2,3,4},pyramid,tornado,starlette,fastapi,aiohttp,asgi,httpx{18,21},requests,urllib,urllib3v{1,2},wsgi: pip install {toxinidir}/util/opentelemetry-util-http[test]
390-
wsgi,falcon{1,2,3},flask{213,220},django{1,2,3,4},pyramid: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-wsgi[test]
394+
falcon{1,2,3},flask{213,220,300},django{1,2,3,4},pyramid,tornado,starlette,fastapi,aiohttp,asgi,httpx{18,21},requests,urllib,urllib3v{1,2},wsgi: pip install {toxinidir}/util/opentelemetry-util-http[test]
395+
wsgi,falcon{1,2,3},flask{213,220,300},django{1,2,3,4},pyramid: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-wsgi[test]
391396
asgi,django{3,4},starlette,fastapi: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-asgi[test]
392397

393398
asyncpg: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-asyncpg[test]
@@ -401,7 +406,7 @@ commands_pre =
401406

402407
falcon{1,2,3}: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-falcon[test]
403408

404-
flask{213,220}: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-flask[test]
409+
flask{213,220,300}: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-flask[test]
405410

406411
urllib: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-urllib[test]
407412

0 commit comments

Comments
 (0)