Skip to content

Commit

Permalink
instrumentation/fastapi: fix fastapi-slim support
Browse files Browse the repository at this point in the history
In instrumentation_dependencies we need to use pkg_resources.get_distribution
because find_spec("fastapi") will return something even with just fastapi-slim
installed.

Then need to fix the tox environment name otherwise all the fastapi
dependencies will also be installed for fastapi-slim and tests would
pass because they find fastapi installed.
  • Loading branch information
xrmx committed Jul 30, 2024
1 parent c87ffd4 commit 96e6cc9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/instrumentations_1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- "resource-detector-azure"
- "resource-detector-container"
- "util-http"
- "fastapi-slim"
- "fastapislim"
- "processor-baggage"
os: [ubuntu-20.04]
exclude:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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:
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

Expand Down
14 changes: 7 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 96e6cc9

Please sign in to comment.