HTTPX2 for Python
Learn about the HTTPX2 integration and how it adds support for the HTTPX2 HTTP client.
The HTTPX2 integration instruments outgoing HTTP requests using either the sync or the async HTTPX2 clients.
Use this integration to create spans for outgoing requests and ensure traces are properly propagated to downstream services.
If you're using stream mode, this page's references to "transaction" should be applied to service spans instead. See Streamed Spans for more information.
Install sentry-sdk from PyPI:
Copied
pip install sentry-sdk
pip install sentry-sdk
uv add sentry-sdk
The HTTPX2 integration is enabled automatically if you have the httpx2 package installed.
Copied
import sentry_sdk
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
# Add data like request headers and IP for users, if applicable;
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
send_default_pii=True,
# ___PRODUCT_OPTION_START___ performance
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# ___PRODUCT_OPTION_END___ performance
# ___PRODUCT_OPTION_START___ profiling
# To collect profiles for all profile sessions,
# set `profile_session_sample_rate` to 1.0.
profile_session_sample_rate=1.0,
# Profiles will be automatically collected while
# there is an active span.
profile_lifecycle="trace",
# ___PRODUCT_OPTION_END___ profiling
)
import sentry_sdk
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
# Add data like request headers and IP for users, if applicable;
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
send_default_pii=True,
# ___PRODUCT_OPTION_START___ performance
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# ___PRODUCT_OPTION_END___ performance
# ___PRODUCT_OPTION_START___ profiling
# To collect profiles for all profile sessions,
# set `profile_session_sample_rate` to 1.0.
profile_session_sample_rate=1.0,
# Profiles will be automatically collected while
# there is an active span.
profile_lifecycle="trace",
# ___PRODUCT_OPTION_END___ profiling
)
Copied
import httpx2
def main():
sentry_init(...) # same as above
# or sentry_sdk.traces.start_span(name="your_span_name", parent_span=None) in stream mode
with sentry_sdk.start_transaction(name="testing_sentry"):
r1 = httpx2.get("https://sentry.io/")
r2 = httpx2.post("http://httpbin.org/post")
main()
import httpx2
def main():
sentry_init(...) # same as above
# or sentry_sdk.traces.start_span(name="your_span_name", parent_span=None) in stream mode
with sentry_sdk.start_transaction(name="testing_sentry"):
r1 = httpx2.get("https://sentry.io/")
r2 = httpx2.post("http://httpbin.org/post")
main()
This will create a transaction called testing_sentry in the Performance section of sentry.io, and create spans for the outgoing HTTP requests.
It takes a couple of moments for the data to appear in sentry.io.
- HTTPX2: 2.0+
- Python: 3.10+
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").