Skip to content

Commit 04b02db

Browse files
committed
Don't pass bounded server_request_hook when using FastAPIInstrumentor.instrument(server_request_hook=...)
1 parent 8c3bf3e commit 04b02db

File tree

1 file changed

+9
-49
lines changed
  • instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi

1 file changed

+9
-49
lines changed

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

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
185185
import functools
186186
import logging
187187
import types
188-
from typing import Collection, Literal
188+
from typing import Any, Collection, Literal
189189

190190
import fastapi
191191
from starlette.applications import Starlette
@@ -361,68 +361,28 @@ def uninstrument_app(app: fastapi.FastAPI):
361361
def instrumentation_dependencies(self) -> Collection[str]:
362362
return _instruments
363363

364-
def _instrument(self, **kwargs):
364+
def _instrument(self, **kwargs: Any):
365365
self._original_fastapi = fastapi.FastAPI
366-
_InstrumentedFastAPI._tracer_provider = kwargs.get("tracer_provider")
367-
_InstrumentedFastAPI._server_request_hook = kwargs.get(
368-
"server_request_hook"
369-
)
370-
_InstrumentedFastAPI._client_request_hook = kwargs.get(
371-
"client_request_hook"
372-
)
373-
_InstrumentedFastAPI._client_response_hook = kwargs.get(
374-
"client_response_hook"
375-
)
376-
_InstrumentedFastAPI._http_capture_headers_server_request = kwargs.get(
377-
"http_capture_headers_server_request"
378-
)
379-
_InstrumentedFastAPI._http_capture_headers_server_response = (
380-
kwargs.get("http_capture_headers_server_response")
381-
)
382-
_InstrumentedFastAPI._http_capture_headers_sanitize_fields = (
383-
kwargs.get("http_capture_headers_sanitize_fields")
384-
)
385-
_InstrumentedFastAPI._excluded_urls = kwargs.get("excluded_urls")
386-
_InstrumentedFastAPI._meter_provider = kwargs.get("meter_provider")
387-
_InstrumentedFastAPI._exclude_spans = kwargs.get("exclude_spans")
366+
_InstrumentedFastAPI._instrument_kwargs = kwargs
388367
fastapi.FastAPI = _InstrumentedFastAPI
389368

390-
def _uninstrument(self, **kwargs):
369+
def _uninstrument(self, **kwargs: Any):
391370
for instance in _InstrumentedFastAPI._instrumented_fastapi_apps:
392371
self.uninstrument_app(instance)
393372
_InstrumentedFastAPI._instrumented_fastapi_apps.clear()
394373
fastapi.FastAPI = self._original_fastapi
395374

396375

397376
class _InstrumentedFastAPI(fastapi.FastAPI):
398-
_tracer_provider = None
399-
_meter_provider = None
400-
_excluded_urls = None
401-
_server_request_hook: ServerRequestHook = None
402-
_client_request_hook: ClientRequestHook = None
403-
_client_response_hook: ClientResponseHook = None
404-
_http_capture_headers_server_request: list[str] | None = None
405-
_http_capture_headers_server_response: list[str] | None = None
406-
_http_capture_headers_sanitize_fields: list[str] | None = None
407-
_exclude_spans: list[Literal["receive", "send"]] | None = None
408-
409-
_instrumented_fastapi_apps = set()
377+
_instrument_kwargs: dict[str, Any] = {}
378+
379+
_instrumented_fastapi_apps: set[fastapi.FastAPI] = set()
410380
_sem_conv_opt_in_mode = _StabilityMode.DEFAULT
411381

412-
def __init__(self, *args, **kwargs):
382+
def __init__(self, *args: Any, **kwargs: Any):
413383
super().__init__(*args, **kwargs)
414384
FastAPIInstrumentor.instrument_app(
415-
self,
416-
server_request_hook=self._server_request_hook,
417-
client_request_hook=self._client_request_hook,
418-
client_response_hook=self._client_response_hook,
419-
tracer_provider=self._tracer_provider,
420-
meter_provider=self._meter_provider,
421-
excluded_urls=self._excluded_urls,
422-
http_capture_headers_server_request=self._http_capture_headers_server_request,
423-
http_capture_headers_server_response=self._http_capture_headers_server_response,
424-
http_capture_headers_sanitize_fields=self._http_capture_headers_sanitize_fields,
425-
exclude_spans=self._exclude_spans,
385+
self, **_InstrumentedFastAPI._instrument_kwargs
426386
)
427387
_InstrumentedFastAPI._instrumented_fastapi_apps.add(self)
428388

0 commit comments

Comments
 (0)