From c88ac3f6bb3a6074b87e5d06f7a1e2d130693a82 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Tue, 26 Nov 2024 08:07:35 +0100 Subject: [PATCH] Add changelog --- CHANGELOG.md | 2 ++ .../instrumentation/starlette/__init__.py | 32 +++++++------------ 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 173abf88f4..04f802043c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- `opentelemetry-instrumentation-starlette` Add type hints to the instrumentation + ([#3045](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3045)) - `opentelemetry-instrumentation-sqlalchemy` Update unit tests to run with SQLALchemy 2 ([#2976](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2976)) - Add `opentelemetry-instrumentation-openai-v2` to `opentelemetry-bootstrap` diff --git a/instrumentation/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py b/instrumentation/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py index 677d4d2144..ca940f748c 100644 --- a/instrumentation/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py @@ -193,14 +193,14 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A from opentelemetry.util.http import get_excluded_urls if TYPE_CHECKING: - from typing import NotRequired, TypedDict, Unpack + from typing import TypedDict, Unpack - class InstrumentKwargs(TypedDict): - tracer_provider: NotRequired[TracerProvider] - meter_provider: NotRequired[MeterProvider] - server_request_hook: NotRequired[ServerRequestHook] - client_request_hook: NotRequired[ClientRequestHook] - client_response_hook: NotRequired[ClientResponseHook] + class InstrumentKwargs(TypedDict, total=False): + tracer_provider: TracerProvider + meter_provider: MeterProvider + server_request_hook: ServerRequestHook + client_request_hook: ClientRequestHook + client_response_hook: ClientResponseHook _excluded_urls = get_excluded_urls("STARLETTE") @@ -256,11 +256,7 @@ def instrument_app( @staticmethod def uninstrument_app(app: applications.Starlette): - app.user_middleware = [ - x - for x in app.user_middleware - if x.cls is not OpenTelemetryMiddleware - ] + app.user_middleware = [x for x in app.user_middleware if x.cls is not OpenTelemetryMiddleware] app.middleware_stack = app.build_middleware_stack() app._is_instrumented_by_opentelemetry = False @@ -270,15 +266,9 @@ def instrumentation_dependencies(self) -> Collection[str]: def _instrument(self, **kwargs: Unpack[InstrumentKwargs]): self._original_starlette = applications.Starlette _InstrumentedStarlette._tracer_provider = kwargs.get("tracer_provider") - _InstrumentedStarlette._server_request_hook = kwargs.get( - "server_request_hook" - ) - _InstrumentedStarlette._client_request_hook = kwargs.get( - "client_request_hook" - ) - _InstrumentedStarlette._client_response_hook = kwargs.get( - "client_response_hook" - ) + _InstrumentedStarlette._server_request_hook = kwargs.get("server_request_hook") + _InstrumentedStarlette._client_request_hook = kwargs.get("client_request_hook") + _InstrumentedStarlette._client_response_hook = kwargs.get("client_response_hook") _InstrumentedStarlette._meter_provider = kwargs.get("_meter_provider") applications.Starlette = _InstrumentedStarlette