Skip to content

Commit

Permalink
refactor: combine exclusions into one optional list
Browse files Browse the repository at this point in the history
  • Loading branch information
omgitsaheadcrab committed Aug 21, 2024
1 parent 9c32d80 commit f312d4a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f312d4a

Please sign in to comment.