Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teach fastapi instrumentation about fastapi-slim #2702

Merged
merged 22 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `opentelemetry-instrumentation-fastapi` Add dependency support for fastapi-slim
lzchen marked this conversation as resolved.
Show resolved Hide resolved
([#2702](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2702))
- `opentelemetry-instrumentation-httpx` Ensure httpx.get or httpx.request like methods are instrumented
([#2538](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2538))
- Add Python 3.12 support
Expand Down
2 changes: 1 addition & 1 deletion instrumentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
| [opentelemetry-instrumentation-django](./opentelemetry-instrumentation-django) | django >= 1.10 | Yes | experimental
| [opentelemetry-instrumentation-elasticsearch](./opentelemetry-instrumentation-elasticsearch) | elasticsearch >= 6.0 | No | experimental
| [opentelemetry-instrumentation-falcon](./opentelemetry-instrumentation-falcon) | falcon >= 1.4.1, < 4.0.0 | Yes | experimental
| [opentelemetry-instrumentation-fastapi](./opentelemetry-instrumentation-fastapi) | fastapi ~= 0.58 | Yes | migration
| [opentelemetry-instrumentation-fastapi](./opentelemetry-instrumentation-fastapi) | fastapi >= 0.58.0,fastapi-slim ~= 0.111.0 | Yes | experimental
zhihali marked this conversation as resolved.
Show resolved Hide resolved
| [opentelemetry-instrumentation-flask](./opentelemetry-instrumentation-flask) | flask >= 1.0 | Yes | migration
| [opentelemetry-instrumentation-grpc](./opentelemetry-instrumentation-grpc) | grpcio ~= 1.27 | No | experimental
| [opentelemetry-instrumentation-httpx](./opentelemetry-instrumentation-httpx) | httpx >= 0.18.0 | No | migration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ dependencies = [

[project.optional-dependencies]
instruments = [
"fastapi ~= 0.58",
"fastapi >= 0.58.0",
zhihali marked this conversation as resolved.
Show resolved Hide resolved
"fastapi-slim ~= 0.111.0",
]

[project.entry-points.opentelemetry_instrumentor]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
---
"""
import logging
from importlib.util import find_spec
from typing import Collection

import fastapi
Expand All @@ -189,7 +190,11 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
ClientResponseHook,
ServerRequestHook,
)
from opentelemetry.instrumentation.fastapi.package import _instruments
from opentelemetry.instrumentation.fastapi.package import (
_fastapi,
_fastapi_slim,
_instruments,
)
from opentelemetry.instrumentation.fastapi.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.metrics import get_meter
Expand Down Expand Up @@ -280,6 +285,11 @@ def uninstrument_app(app: fastapi.FastAPI):
app._is_instrumented_by_opentelemetry = False

def instrumentation_dependencies(self) -> Collection[str]:
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
zhihali marked this conversation as resolved.
Show resolved Hide resolved
return _instruments

def _instrument(self, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
# limitations under the License.


_instruments = ("fastapi ~= 0.58",)
_fastapi = "fastapi >= 0.58.0"
_fastapi_slim = "fastapi-slim ~= 0.111.0"

_instruments = (_fastapi, _fastapi_slim)

_supports_metrics = True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@
"instrumentation": "opentelemetry-instrumentation-falcon==0.47b0.dev",
},
{
"library": "fastapi ~= 0.58",
"library": "fastapi >= 0.58.0",
"instrumentation": "opentelemetry-instrumentation-fastapi==0.47b0.dev",
},
{
"library": "fastapi-slim ~= 0.111.0",
"instrumentation": "opentelemetry-instrumentation-fastapi==0.47b0.dev",
},
{
Expand Down
Loading