PATH:
opt
/
hc_python
/
lib
/
python3.12
/
site-packages
/
sentry_sdk
/
integrations
from typing import TYPE_CHECKING import sentry_sdk from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration from sentry_sdk.tracing import BAGGAGE_HEADER_NAME from sentry_sdk.tracing_utils import ( add_http_request_source, add_sentry_baggage_to_headers, has_span_streaming_enabled, should_propagate_trace, ) from sentry_sdk.utils import ( SENSITIVE_DATA_SUBSTITUTE, capture_internal_exceptions, ensure_integration_enabled, logger, parse_url, ) if TYPE_CHECKING: from typing import Any from sentry_sdk._types import Attributes try: from httpx2 import AsyncClient, Client, Request, Response except ImportError: raise DidNotEnable("httpx2 is not installed") __all__ = ["Httpx2Integration"] class Httpx2Integration(Integration): identifier = "httpx2" origin = f"auto.http.{identifier}" @staticmethod def setup_once() -> None: """ httpx2 has its own transport layer and can be customized when needed, so patch Client.send and AsyncClient.send to support both synchronous and async interfaces. """ _install_httpx2_client() _install_httpx2_async_client() def _install_httpx2_client() -> None: real_send = Client.send @ensure_integration_enabled(Httpx2Integration, real_send) def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response": client = sentry_sdk.get_client() is_span_streaming_enabled = has_span_streaming_enabled(client.options) parsed_url = None with capture_internal_exceptions(): parsed_url = parse_url(str(request.url), sanitize=False) if is_span_streaming_enabled: with sentry_sdk.traces.start_span( name="%s %s" % ( request.method, parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, ), attributes={ "sentry.op": OP.HTTP_CLIENT, "sentry.origin": Httpx2Integration.origin, "http.request.method": request.method, }, ) as streamed_span: attributes: "Attributes" = {} if parsed_url is not None: attributes["url.full"] = parsed_url.url if parsed_url.query: attributes["url.query"] = parsed_url.query if parsed_url.fragment: attributes["url.fragment"] = parsed_url.fragment if should_propagate_trace(client, str(request.url)): for ( key, value, ) in ( sentry_sdk.get_current_scope().iter_trace_propagation_headers() ): logger.debug( f"[Tracing] Adding `{key}` header {value} to outgoing request to {request.url}." ) if key == BAGGAGE_HEADER_NAME: add_sentry_baggage_to_headers(request.headers, value) else: request.headers[key] = value try: rv = real_send(self, request, **kwargs) streamed_span.status = "error" if rv.status_code >= 400 else "ok" attributes["http.response.status_code"] = rv.status_code finally: streamed_span.set_attributes(attributes) # Needs to happen within the context manager as we want to attach the # final data before the span finishes and is sent for ingesting. with capture_internal_exceptions(): add_http_request_source(streamed_span) else: with sentry_sdk.start_span( op=OP.HTTP_CLIENT, name="%s %s" % ( request.method, parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, ), origin=Httpx2Integration.origin, ) as span: span.set_data(SPANDATA.HTTP_METHOD, request.method) if parsed_url is not None: span.set_data("url", parsed_url.url) span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) if should_propagate_trace(client, str(request.url)): for ( key, value, ) in ( sentry_sdk.get_current_scope().iter_trace_propagation_headers() ): logger.debug( f"[Tracing] Adding `{key}` header {value} to outgoing request to {request.url}." ) if key == BAGGAGE_HEADER_NAME: add_sentry_baggage_to_headers(request.headers, value) else: request.headers[key] = value rv = real_send(self, request, **kwargs) span.set_http_status(rv.status_code) span.set_data("reason", rv.reason_phrase) with capture_internal_exceptions(): add_http_request_source(span) return rv Client.send = send # type: ignore def _install_httpx2_async_client() -> None: real_send = AsyncClient.send async def send( self: "AsyncClient", request: "Request", **kwargs: "Any" ) -> "Response": client = sentry_sdk.get_client() if client.get_integration(Httpx2Integration) is None: return await real_send(self, request, **kwargs) is_span_streaming_enabled = has_span_streaming_enabled(client.options) parsed_url = None with capture_internal_exceptions(): parsed_url = parse_url(str(request.url), sanitize=False) if is_span_streaming_enabled: with sentry_sdk.traces.start_span( name="%s %s" % ( request.method, parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, ), attributes={ "sentry.op": OP.HTTP_CLIENT, "sentry.origin": Httpx2Integration.origin, "http.request.method": request.method, }, ) as streamed_span: attributes: "Attributes" = {} if parsed_url is not None: attributes["url.full"] = parsed_url.url if parsed_url.query: attributes["url.query"] = parsed_url.query if parsed_url.fragment: attributes["url.fragment"] = parsed_url.fragment if should_propagate_trace(client, str(request.url)): for ( key, value, ) in ( sentry_sdk.get_current_scope().iter_trace_propagation_headers() ): logger.debug( f"[Tracing] Adding `{key}` header {value} to outgoing request to {request.url}." ) if key == BAGGAGE_HEADER_NAME: add_sentry_baggage_to_headers(request.headers, value) else: request.headers[key] = value try: rv = await real_send(self, request, **kwargs) streamed_span.status = "error" if rv.status_code >= 400 else "ok" attributes["http.response.status_code"] = rv.status_code finally: streamed_span.set_attributes(attributes) # Needs to happen within the context manager as we want to attach the # final data before the span finishes and is sent for ingesting. with capture_internal_exceptions(): add_http_request_source(streamed_span) else: with sentry_sdk.start_span( op=OP.HTTP_CLIENT, name="%s %s" % ( request.method, parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, ), origin=Httpx2Integration.origin, ) as span: span.set_data(SPANDATA.HTTP_METHOD, request.method) if parsed_url is not None: span.set_data("url", parsed_url.url) span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) if should_propagate_trace(client, str(request.url)): for ( key, value, ) in ( sentry_sdk.get_current_scope().iter_trace_propagation_headers() ): logger.debug( f"[Tracing] Adding `{key}` header {value} to outgoing request to {request.url}." ) if key == BAGGAGE_HEADER_NAME: add_sentry_baggage_to_headers(request.headers, value) else: request.headers[key] = value rv = await real_send(self, request, **kwargs) span.set_http_status(rv.status_code) span.set_data("reason", rv.reason_phrase) with capture_internal_exceptions(): add_http_request_source(span) return rv AsyncClient.send = send # type: ignore
[-] tornado.py
[edit]
[-] httpx2.py
[edit]
[-] launchdarkly.py
[edit]
[+]
pydantic_ai
[-] strawberry.py
[edit]
[-] falcon.py
[edit]
[+]
grpc
[-] litestar.py
[edit]
[-] logging.py
[edit]
[-] pymongo.py
[edit]
[-] wsgi.py
[edit]
[-] asyncio.py
[edit]
[-] excepthook.py
[edit]
[-] _wsgi_common.py
[edit]
[-] trytond.py
[edit]
[-] pure_eval.py
[edit]
[-] starlette.py
[edit]
[-] rq.py
[edit]
[-] modules.py
[edit]
[-] rust_tracing.py
[edit]
[-] stdlib.py
[edit]
[-] dramatiq.py
[edit]
[+]
openai_agents
[-] threading.py
[edit]
[-] aiohttp.py
[edit]
[-] arq.py
[edit]
[+]
celery
[-] starlite.py
[edit]
[-] sqlalchemy.py
[edit]
[+]
__pycache__
[-] graphene.py
[edit]
[-] sanic.py
[edit]
[-] anthropic.py
[edit]
[-] langgraph.py
[edit]
[+]
opentelemetry
[-] statsig.py
[edit]
[-] openfeature.py
[edit]
[-] ariadne.py
[edit]
[-] asyncpg.py
[edit]
[-] ray.py
[edit]
[-] openai.py
[edit]
[-] typer.py
[edit]
[-] cohere.py
[edit]
[-] mcp.py
[edit]
[+]
..
[-] huggingface_hub.py
[edit]
[-] chalice.py
[edit]
[-] dedupe.py
[edit]
[-] aws_lambda.py
[edit]
[-] boto3.py
[edit]
[-] loguru.py
[edit]
[-] argv.py
[edit]
[-] gql.py
[edit]
[-] pyramid.py
[edit]
[-] asgi.py
[edit]
[-] serverless.py
[edit]
[-] atexit.py
[edit]
[-] gnu_backtrace.py
[edit]
[-] socket.py
[edit]
[-] langchain.py
[edit]
[-] httpx.py
[edit]
[-] gcp.py
[edit]
[-] otlp.py
[edit]
[+]
django
[-] fastapi.py
[edit]
[-] flask.py
[edit]
[+]
google_genai
[-] _asgi_common.py
[edit]
[-] clickhouse_driver.py
[edit]
[+]
redis
[-] aiomysql.py
[edit]
[-] executing.py
[edit]
[-] __init__.py
[edit]
[-] quart.py
[edit]
[-] beam.py
[edit]
[+]
spark
[-] unraisablehook.py
[edit]
[-] bottle.py
[edit]
[-] litellm.py
[edit]
[-] pyreqwest.py
[edit]
[-] sys_exit.py
[edit]
[-] huey.py
[edit]
[-] unleash.py
[edit]
[-] cloud_resource_context.py
[edit]