diff --git a/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py b/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py index bbf90499b7..a47b739a6c 100644 --- a/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py @@ -527,8 +527,7 @@ class OpenTelemetryMiddleware: the current globally configured one is used. meter_provider: The optional meter provider to use. If omitted the current globally configured one is used. - exclude_receive_span: Optional flag to exclude the http receive span from the trace. - exclude_send_span: Optional flag to exclude the http send span from the trace. + exclude_spans: Optionally exclude http `send` and/or `receive` span from the trace. """ # pylint: disable=too-many-branches @@ -547,8 +546,7 @@ def __init__( http_capture_headers_server_request: list[str] | None = None, http_capture_headers_server_response: list[str] | None = None, http_capture_headers_sanitize_fields: list[str] | None = None, - exclude_receive_span: bool = False, - exclude_send_span: bool = False, + exclude_spans: list[typing.Literal["receive", "send"]] | None = None, ): # initialize semantic conventions opt-in if needed _OpenTelemetrySemanticConventionStability._initialize() @@ -655,8 +653,10 @@ def __init__( ) or [] ) - self.exclude_receive_span = exclude_receive_span - self.exclude_send_span = exclude_send_span + self.exclude_receive_span = ( + "receive" in exclude_spans if exclude_spans else False + ) + self.exclude_send_span = "send" in exclude_spans if exclude_spans else False # pylint: disable=too-many-statements async def __call__( 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 a4668a9f73..1319615fbd 100644 --- a/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py @@ -179,7 +179,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A from __future__ import annotations import logging -from typing import Collection +from typing import Collection, Literal import fastapi from starlette.routing import Match @@ -232,8 +232,7 @@ def instrument_app( http_capture_headers_server_request: list[str] | None = None, http_capture_headers_server_response: list[str] | None = None, http_capture_headers_sanitize_fields: list[str] | None = None, - exclude_receive_span: bool = False, - exclude_send_span: bool = False, + exclude_spans: list[Literal["receive", "send"]] | None = None, ): """Instrument an uninstrumented FastAPI application. @@ -253,8 +252,7 @@ def instrument_app( http_capture_headers_server_request: Optional list of HTTP headers to capture from the request. http_capture_headers_server_response: Optional list of HTTP headers to capture from the response. http_capture_headers_sanitize_fields: Optional list of HTTP headers to sanitize. - exclude_receive_span: Optional flag to exclude the http receive span from the trace. - exclude_send_span: Optional flag to exclude the http send span from the trace. + exclude_spans: Optionally exclude http `send` and/or `receive` span from the trace. """ if not hasattr(app, "_is_instrumented_by_opentelemetry"): app._is_instrumented_by_opentelemetry = False @@ -295,8 +293,7 @@ def instrument_app( http_capture_headers_server_request=http_capture_headers_server_request, http_capture_headers_server_response=http_capture_headers_server_response, http_capture_headers_sanitize_fields=http_capture_headers_sanitize_fields, - exclude_receive_span=exclude_receive_span, - exclude_send_span=exclude_send_span, + exclude_spans=exclude_spans, ) app._is_instrumented_by_opentelemetry = True if app not in _InstrumentedFastAPI._instrumented_fastapi_apps: @@ -347,11 +344,8 @@ def _instrument(self, **kwargs): else parse_excluded_urls(_excluded_urls) ) _InstrumentedFastAPI._meter_provider = kwargs.get("meter_provider") - _InstrumentedFastAPI._exclude_receive_span = kwargs.get( - "exclude_receive_span" - ) - _InstrumentedFastAPI._exclude_send_span = kwargs.get( - "exclude_send_span" + _InstrumentedFastAPI._exclude_spans = kwargs.get( + "exclude_spans" ) fastapi.FastAPI = _InstrumentedFastAPI @@ -403,8 +397,7 @@ def __init__(self, *args, **kwargs): http_capture_headers_server_request=_InstrumentedFastAPI._http_capture_headers_server_request, http_capture_headers_server_response=_InstrumentedFastAPI._http_capture_headers_server_response, http_capture_headers_sanitize_fields=_InstrumentedFastAPI._http_capture_headers_sanitize_fields, - exclude_receive_span=_InstrumentedFastAPI._exclude_receive_span, - exclude_send_span=_InstrumentedFastAPI._exclude_send_span, + exclude_spans=_InstrumentedFastAPI._exclude_spans, ) self._is_instrumented_by_opentelemetry = True _InstrumentedFastAPI._instrumented_fastapi_apps.add(self)