PHP 8.2.30
Preview: httpx.py Size: 5.94 KB
/opt/hc_python/lib64/python3.12/site-packages/sentry_sdk/integrations/httpx.py

import sentry_sdk
from sentry_sdk import start_span
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.integrations import Integration, DidNotEnable
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME
from sentry_sdk.tracing_utils import (
    Baggage,
    should_propagate_trace,
    add_http_request_source,
)
from sentry_sdk.utils import (
    SENSITIVE_DATA_SUBSTITUTE,
    capture_internal_exceptions,
    ensure_integration_enabled,
    logger,
    parse_url,
)

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from collections.abc import MutableMapping
    from typing import Any


try:
    from httpx import AsyncClient, Client, Request, Response  # type: ignore
except ImportError:
    raise DidNotEnable("httpx is not installed")

__all__ = ["HttpxIntegration"]


class HttpxIntegration(Integration):
    identifier = "httpx"
    origin = f"auto.http.{identifier}"

    @staticmethod
    def setup_once():
        # type: () -> None
        """
        httpx 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_httpx_client()
        _install_httpx_async_client()


def _install_httpx_client():
    # type: () -> None
    real_send = Client.send

    @ensure_integration_enabled(HttpxIntegration, real_send)
    def send(self, request, **kwargs):
        # type: (Client, Request, **Any) -> Response
        parsed_url = None
        with capture_internal_exceptions():
            parsed_url = parse_url(str(request.url), sanitize=False)

        with start_span(
            op=OP.HTTP_CLIENT,
            name="%s %s"
            % (
                request.method,
                parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE,
            ),
            origin=HttpxIntegration.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(sentry_sdk.get_client(), str(request.url)):
                for (
                    key,
                    value,
                ) in sentry_sdk.get_current_scope().iter_trace_propagation_headers():
                    logger.debug(
                        "[Tracing] Adding `{key}` header {value} to outgoing request to {url}.".format(
                            key=key, value=value, url=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


def _install_httpx_async_client():
    # type: () -> None
    real_send = AsyncClient.send

    async def send(self, request, **kwargs):
        # type: (AsyncClient, Request, **Any) -> Response
        if sentry_sdk.get_client().get_integration(HttpxIntegration) is None:
            return await real_send(self, request, **kwargs)

        parsed_url = None
        with capture_internal_exceptions():
            parsed_url = parse_url(str(request.url), sanitize=False)

        with start_span(
            op=OP.HTTP_CLIENT,
            name="%s %s"
            % (
                request.method,
                parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE,
            ),
            origin=HttpxIntegration.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(sentry_sdk.get_client(), str(request.url)):
                for (
                    key,
                    value,
                ) in sentry_sdk.get_current_scope().iter_trace_propagation_headers():
                    logger.debug(
                        "[Tracing] Adding `{key}` header {value} to outgoing request to {url}.".format(
                            key=key, value=value, url=request.url
                        )
                    )
                    if key == BAGGAGE_HEADER_NAME and request.headers.get(
                        BAGGAGE_HEADER_NAME
                    ):
                        # do not overwrite any existing baggage, just append to it
                        request.headers[key] += "," + 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


def _add_sentry_baggage_to_headers(headers, sentry_baggage):
    # type: (MutableMapping[str, str], str) -> None
    """Add the Sentry baggage to the headers.

    This function directly mutates the provided headers. The provided sentry_baggage
    is appended to the existing baggage. If the baggage already contains Sentry items,
    they are stripped out first.
    """
    existing_baggage = headers.get(BAGGAGE_HEADER_NAME, "")
    stripped_existing_baggage = Baggage.strip_sentry_baggage(existing_baggage)

    separator = "," if len(stripped_existing_baggage) > 0 else ""

    headers[BAGGAGE_HEADER_NAME] = (
        stripped_existing_baggage + separator + sentry_baggage
    )

Directory Contents

Dirs: 10 × Files: 70

Name Size Perms Modified Actions
celery DIR
- drwxr-xr-x 2025-12-03 08:02:53
Edit Download
django DIR
- drwxr-xr-x 2025-12-03 08:02:53
Edit Download
- drwxr-xr-x 2025-12-03 08:02:53
Edit Download
grpc DIR
- drwxr-xr-x 2025-12-03 08:02:53
Edit Download
- drwxr-xr-x 2025-12-03 08:02:53
Edit Download
- drwxr-xr-x 2025-12-03 08:02:53
Edit Download
- drwxr-xr-x 2025-12-03 08:02:53
Edit Download
redis DIR
- drwxr-xr-x 2025-12-03 08:02:53
Edit Download
spark DIR
- drwxr-xr-x 2025-12-03 08:02:53
Edit Download
- drwxr-xr-x 2025-12-03 08:02:53
Edit Download
12.70 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
14.17 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
911 B lrw-r--r-- 2025-12-03 08:02:53
Edit Download
5.70 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
7.70 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
12.50 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
4.50 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
6.37 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
1.61 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
17.65 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
5.06 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
4.31 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
6.46 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
4.59 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
5.96 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
7.60 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
9.18 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
1.93 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
7.28 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
2.35 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
1.95 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
9.28 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
4.48 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
8.54 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
8.26 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
2.75 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
4.69 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
4.92 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
5.94 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
5.32 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
14.60 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
39.02 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
11.56 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
1.89 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
9.99 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
11.55 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
13.57 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
6.38 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
19.58 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
820 B lrw-r--r-- 2025-12-03 08:02:53
Edit Download
24.54 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
1.10 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
3.06 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
4.50 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
6.23 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
7.19 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
7.24 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
5.20 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
5.18 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
8.87 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
12.66 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
1.76 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
3.09 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
4.24 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
25.62 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
10.31 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
1.20 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
8.76 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
13.82 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
2.43 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
6.94 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
7.04 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
1.61 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
1.77 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
1.03 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
1.71 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
10.56 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
3.11 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
7.38 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download
12.44 KB lrw-r--r-- 2025-12-03 08:02:53
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).