diff --git a/.github/workflows/instrumentations_1.yml b/.github/workflows/instrumentations_1.yml index 40689bdc13..2658f4d4b2 100644 --- a/.github/workflows/instrumentations_1.yml +++ b/.github/workflows/instrumentations_1.yml @@ -38,7 +38,7 @@ jobs: - "resource-detector-azure" - "resource-detector-container" - "util-http" - - "fastapi-slim" + - "fastapislim" - "processor-baggage" os: [ubuntu-20.04] exclude: diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b09c75064..6d8d037d69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#2746](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2746)) - `opentelemetry-instrumentation-grpc` Fixes the issue with the gRPC instrumentation not working with the 1.63.0 and higher version of gRPC ([#2483](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2484)) +- `opentelemetry-instrumentation-fastapi` Fix fastapi-slim support + ([#2756](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2756)) ## Version 1.26.0/0.47b0 (2024-07-23) diff --git a/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py b/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py index 9356093a45..5d61918277 100644 --- a/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py @@ -176,6 +176,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A from typing import Collection import fastapi +from pkg_resources import get_distribution from starlette.routing import Match from opentelemetry.instrumentation._semconv import ( @@ -285,10 +286,16 @@ def uninstrument_app(app: fastapi.FastAPI): app._is_instrumented_by_opentelemetry = False def instrumentation_dependencies(self) -> Collection[str]: + # need to use get_distribution because find_spec("fastapi") will return + # something even with just fastapi-slim installed + try: + get_distribution("fastapi-slim") + return (_fastapi_slim,) + except Exception: # pylint: disable=broad-exception-caught + pass + if find_spec("fastapi") is not None: return (_fastapi,) - if find_spec("fastapi_slim") is not None: - return (_fastapi_slim,) # If neither is installed, return both as potential dependencies return _instruments diff --git a/tox.ini b/tox.ini index 028db9c19f..e28f912c33 100644 --- a/tox.ini +++ b/tox.ini @@ -116,9 +116,9 @@ envlist = ; opentelemetry-instrumentation-fastapi py3{8,9,10,11,12}-test-instrumentation-fastapi - py3{8,9,10,11,12}-test-instrumentation-fastapi-slim + py3{8,9,10,11,12}-test-instrumentation-fastapislim pypy3-test-instrumentation-fastapi - pypy3-test-instrumentation-fastapi-slim + pypy3-test-instrumentation-fastapislim lint-instrumentation-fastapi ; opentelemetry-instrumentation-flask @@ -552,11 +552,11 @@ commands_pre = fastapi: pip install opentelemetry-sdk@{env:CORE_REPO}\#egg=opentelemetry-sdk&subdirectory=opentelemetry-sdk fastapi: pip install opentelemetry-test-utils@{env:CORE_REPO}\#egg=opentelemetry-test-utils&subdirectory=tests/opentelemetry-test-utils fastapi: pip install -r {toxinidir}/instrumentation/opentelemetry-instrumentation-fastapi/test-requirements.txt - fastapi-slim: pip install opentelemetry-api@{env:CORE_REPO}\#egg=opentelemetry-api&subdirectory=opentelemetry-api - fastapi-slim: pip install opentelemetry-semantic-conventions@{env:CORE_REPO}\#egg=opentelemetry-semantic-conventions&subdirectory=opentelemetry-semantic-conventions - fastapi-slim: pip install opentelemetry-sdk@{env:CORE_REPO}\#egg=opentelemetry-sdk&subdirectory=opentelemetry-sdk - fastapi-slim: pip install opentelemetry-test-utils@{env:CORE_REPO}\#egg=opentelemetry-test-utils&subdirectory=tests/opentelemetry-test-utils - fastapi-slim: pip install -r {toxinidir}/instrumentation/opentelemetry-instrumentation-fastapi/test-requirements-slim.txt + fastapislim: pip install opentelemetry-api@{env:CORE_REPO}\#egg=opentelemetry-api&subdirectory=opentelemetry-api + fastapislim: pip install opentelemetry-semantic-conventions@{env:CORE_REPO}\#egg=opentelemetry-semantic-conventions&subdirectory=opentelemetry-semantic-conventions + fastapislim: pip install opentelemetry-sdk@{env:CORE_REPO}\#egg=opentelemetry-sdk&subdirectory=opentelemetry-sdk + fastapislim: pip install opentelemetry-test-utils@{env:CORE_REPO}\#egg=opentelemetry-test-utils&subdirectory=tests/opentelemetry-test-utils + fastapislim: pip install -r {toxinidir}/instrumentation/opentelemetry-instrumentation-fastapi/test-requirements-slim.txt mysql: pip install opentelemetry-api@{env:CORE_REPO}\#egg=opentelemetry-api&subdirectory=opentelemetry-api mysql: pip install opentelemetry-semantic-conventions@{env:CORE_REPO}\#egg=opentelemetry-semantic-conventions&subdirectory=opentelemetry-semantic-conventions