Skip to content
242 changes: 173 additions & 69 deletions sentry_sdk/integrations/httpx.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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 (
should_propagate_trace,
add_http_request_source,
should_propagate_trace,
add_sentry_baggage_to_headers,
has_span_streaming_enabled,
)
from sentry_sdk.utils import (
SENSITIVE_DATA_SUBSTITUTE,
Expand All @@ -20,6 +20,7 @@

if TYPE_CHECKING:
from typing import Any
from sentry_sdk._types import Attributes


try:
Expand Down Expand Up @@ -49,48 +50,99 @@

@ensure_integration_enabled(HttpxIntegration, real_send)
def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response":
Copy link
Copy Markdown
Contributor

@sentrivana sentrivana Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my comments on the async send since this is essentially the same changeset

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)

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 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": HttpxIntegration.origin,
"http.request.method": request.method,
},
) as streamed_span:
attributes: "Attributes" = {}

if parsed_url is not None:
attributes["url.full"] = parsed_url.url
attributes["url.query"] = parsed_url.query
attributes["url.fragment"] = parsed_url.fragment

Check warning on line 78 in sentry_sdk/integrations/httpx.py

View check run for this annotation

@sentry/warden / warden: find-bugs

[SAZ-CR4] Test will fail for sync client due to inconsistent url.query/url.fragment handling (additional location)

The test `test_http_url_attributes_no_query_or_fragment_span_streaming` asserts that `url.query` and `url.fragment` should NOT be present when the URL has no query/fragment. However, the sync client implementation in `httpx.py` (lines 77-78) unconditionally sets these attributes, while the async client implementation (lines 184-187) conditionally sets them only when truthy. This test will pass for `httpx.AsyncClient()` but fail for `httpx.Client()` because the sync implementation always includes these attributes (with empty string values).

Check warning on line 78 in sentry_sdk/integrations/httpx.py

View check run for this annotation

@sentry/warden / warden: code-review

[P3F-V3Y] Test will fail for sync client due to inconsistent attribute handling (additional location)

The test `test_http_url_attributes_no_query_or_fragment_span_streaming` asserts that `url.query` and `url.fragment` are NOT in the span attributes when the URL has no query/fragment. However, this test is parametrized with both sync (`httpx.Client()`) and async (`httpx.AsyncClient()`) clients. The sync client implementation (httpx.py lines 75-78) unconditionally sets `attributes["url.query"]` and `attributes["url.fragment"]` even when they are empty, while the async client (lines 182-187) conditionally sets them only when truthy. This inconsistency will cause the test to fail for the sync client.
Comment thread
sentry[bot] marked this conversation as resolved.
Outdated
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated

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
if key == BAGGAGE_HEADER_NAME:
add_sentry_baggage_to_headers(request.headers, value)
else:
request.headers[key] = value

rv = real_send(self, request, **kwargs)
rv = real_send(self, request, **kwargs)

span.set_http_status(rv.status_code)
span.set_data("reason", rv.reason_phrase)
streamed_span.status = "error" if rv.status_code >= 400 else "ok"
attributes["http.response.status_code"] = rv.status_code

with capture_internal_exceptions():
add_http_request_source(span)
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)
Comment thread
cursor[bot] marked this conversation as resolved.
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=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(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

Expand All @@ -103,50 +155,102 @@
async def send(
self: "AsyncClient", request: "Request", **kwargs: "Any"
) -> "Response":
if sentry_sdk.get_client().get_integration(HttpxIntegration) is None:
client = sentry_sdk.get_client()
if client.get_integration(HttpxIntegration) 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)

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 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": HttpxIntegration.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

rv = await real_send(self, request, **kwargs)
if key == BAGGAGE_HEADER_NAME:
add_sentry_baggage_to_headers(request.headers, value)
else:
request.headers[key] = value

span.set_http_status(rv.status_code)
span.set_data("reason", rv.reason_phrase)
rv = await real_send(self, request, **kwargs)

with capture_internal_exceptions():
add_http_request_source(span)
streamed_span.status = "error" if rv.status_code >= 400 else "ok"
attributes["http.response.status_code"] = rv.status_code

streamed_span.set_attributes(attributes)
Comment thread
sentry[bot] marked this conversation as resolved.
Outdated

# 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=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(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

Expand Down
21 changes: 13 additions & 8 deletions sentry_sdk/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,9 @@ def __init__(
self._start_timestamp = datetime.now(timezone.utc)
self._timestamp: "Optional[datetime]" = None

try:
# profiling depends on this value and requires that
# it is measured in nanoseconds
self._start_timestamp_monotonic_ns = nanosecond_time()
except AttributeError:
pass
# profiling depends on this value and requires that
# it is measured in nanoseconds
self._start_timestamp_monotonic_ns = nanosecond_time()
Comment thread
ericapisani marked this conversation as resolved.

self._span_id: "Optional[str]" = None

Expand Down Expand Up @@ -385,12 +382,12 @@ def _end(self, end_timestamp: "Optional[Union[float, datetime]]" = None) -> None
)

if self._timestamp is None:
try:
if self._start_timestamp_monotonic_ns is not None:
elapsed = nanosecond_time() - self._start_timestamp_monotonic_ns
self._timestamp = self._start_timestamp + timedelta(
microseconds=elapsed / 1000
)
except AttributeError:
else:
self._timestamp = datetime.now(timezone.utc)

Comment thread
sentry[bot] marked this conversation as resolved.
Outdated
client = sentry_sdk.get_client()
Expand Down Expand Up @@ -467,6 +464,10 @@ def sampled(self) -> "Optional[bool]":
def start_timestamp(self) -> "Optional[datetime]":
return self._start_timestamp

@property
def start_timestamp_monotonic_ns(self) -> "Optional[int]":
return self._start_timestamp_monotonic_ns
Comment thread
ericapisani marked this conversation as resolved.
Outdated
Comment thread
ericapisani marked this conversation as resolved.
Outdated

@property
def timestamp(self) -> "Optional[datetime]":
return self._timestamp
Expand Down Expand Up @@ -681,6 +682,10 @@ def sampled(self) -> "Optional[bool]":
def start_timestamp(self) -> "Optional[datetime]":
return None

@property
def start_timestamp_monotonic_ns(self) -> "Optional[int]":
return None

@property
def timestamp(self) -> "Optional[datetime]":
return None
Expand Down
Loading
Loading