Skip to content

Commit e38858e

Browse files
committed
Document HTTPX2 instrumentation
1 parent 692ec02 commit e38858e

3 files changed

Lines changed: 39 additions & 19 deletions

File tree

docs/integrations/http-clients/httpx.md

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
---
2-
title: "Instrument HTTPX: see every outgoing request your app makes"
3-
description: "Add a few lines to your HTTPX code and see every outgoing HTTP request in Logfire: the URL, status, how long it took, and any errors."
2+
title: "Instrument HTTPX and HTTPX2: see every outgoing request your app makes"
3+
description: "Add a few lines to your HTTPX or HTTPX2 code and see every outgoing HTTP request in Logfire: the URL, status, how long it took, and any errors."
44
integration: otel
55
---
6-
# HTTPX
6+
# HTTPX and HTTPX2
77

8-
See every HTTP request your app makes with [HTTPX][httpx]: the URL, the response status, how long it
9-
took, and any errors, as a **span** (one unit of work with a name, a start, and a duration) in
10-
Logfire. Related spans link together into a **trace** (the full journey of one request), so a slow
11-
outgoing call shows up right next to the code that triggered it.
8+
See every HTTP request your app makes with [HTTPX][httpx] or [HTTPX2][httpx2]: the URL, the response
9+
status, how long it took, and any errors, as a **span** (one unit of work with a name, a start, and a
10+
duration) in Logfire. Related spans link together into a **trace** (the full journey of one request),
11+
so a slow outgoing call shows up right next to the code that triggered it.
1212

13-
This works with both the synchronous `httpx.Client` and the asynchronous `httpx.AsyncClient`.
13+
This works with the synchronous `Client` and asynchronous `AsyncClient` from either library. If both
14+
libraries are installed, one call to `logfire.instrument_httpx()` instruments both.
1415

1516
## What you'll capture
1617

@@ -26,12 +27,21 @@ Install `logfire` with the `httpx` extra:
2627

2728
{{ install_logfire(extras=['httpx']) }}
2829

30+
The extra installs the OpenTelemetry integration that collects request data. It does not install
31+
`httpx` or `httpx2`; keep the client library you use as an application dependency.
32+
33+
HTTPX2 support requires `opentelemetry-instrumentation-httpx` 0.65b0 or newer. A fresh installation
34+
normally selects a compatible version. If an existing environment keeps an older version, use the
35+
upgrade command in [Troubleshooting](#troubleshooting).
36+
2937
## Usage
3038

3139
Add two lines to your app: `logfire.configure()` to connect to your project, and
32-
[`logfire.instrument_httpx()`][logfire.Logfire.instrument_httpx] to record every request.
40+
[`logfire.instrument_httpx()`][logfire.Logfire.instrument_httpx] to record every request. With no
41+
client argument, Logfire instruments both libraries when they are installed. Pass a client instance
42+
to instrument only that client.
3343

34-
=== "Instrument every client"
44+
=== "Instrument all installed clients"
3545

3646
```py title="main.py" hl_lines="8" skip-run="true" skip-reason="external-connection"
3747
import asyncio
@@ -57,26 +67,26 @@ Add two lines to your app: `logfire.configure()` to connect to your project, and
5767
asyncio.run(main())
5868
```
5969

60-
=== "Instrument a single client"
70+
=== "Instrument one HTTPX2 client"
6171

6272
```py title="main.py" hl_lines="12 18" skip-run="true" skip-reason="external-connection"
6373
import asyncio
6474

65-
import httpx
75+
import httpx2
6676

6777
import logfire
6878

6979
logfire.configure()
7080

7181
url = 'https://httpbin.org/get'
7282

73-
with httpx.Client() as client:
83+
with httpx2.Client() as client:
7484
logfire.instrument_httpx(client)
7585
client.get(url)
7686

7787

7888
async def main():
79-
async with httpx.AsyncClient() as client:
89+
async with httpx2.AsyncClient() as client:
8090
logfire.instrument_httpx(client)
8191
await client.get(url)
8292

@@ -86,6 +96,8 @@ Add two lines to your app: `logfire.configure()` to connect to your project, and
8696

8797
Run it with `python main.py`.
8898

99+
You can also pass one `httpx.Client` or `httpx.AsyncClient`; the Logfire call stays the same.
100+
89101
## Verify it worked
90102

91103
Run your program, then open your project in the
@@ -99,16 +111,23 @@ Not seeing your requests in Logfire? Check these first:
99111

100112
- **`logfire.configure()` runs before `logfire.instrument_httpx()`.** Configure the connection first,
101113
then instrument.
102-
- **You instrument the client you actually call.** `instrument_httpx()` with no argument covers all
103-
clients; if you pass a specific client, make sure it's the one making the request.
114+
- **You instrument the client you actually call.** `instrument_httpx()` with no argument covers both
115+
installed libraries; if you pass a specific client, make sure it's the one making the request.
116+
- **HTTPX2 reports that it needs newer OpenTelemetry instrumentation.** Upgrade Logfire and the HTTPX
117+
integration together so their OpenTelemetry dependencies remain compatible:
118+
119+
```bash
120+
pip install -U 'logfire[httpx]' 'opentelemetry-instrumentation-httpx>=0.65b0'
121+
```
104122
- **Your write token is set.** In local development, run `logfire projects use <your-project>`; in
105123
production, set the `LOGFIRE_TOKEN` environment variable. See [Getting Started](../../index.md).
106124
- **You actually made a request.** Spans appear only after a request completes.
107125

108126
## Advanced
109127

110128
The [`logfire.instrument_httpx()`][logfire.Logfire.instrument_httpx] method accepts several parameters
111-
to control what's captured.
129+
to control what's captured. The same capture settings and hooks apply to HTTPX and HTTPX2. The
130+
examples below use HTTPX.
112131

113132
### Capture everything
114133

@@ -226,4 +245,5 @@ client.post('https://httpbin.org/post', data='Hello, World!')
226245
- Underlying OpenTelemetry package: [HTTPX instrumentation][opentelemetry-httpx]
227246

228247
[httpx]: https://www.python-httpx.org/
248+
[httpx2]: https://github.com/pydantic/httpx2
229249
[opentelemetry-httpx]: https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/httpx/httpx.html

docs/integrations/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The below table lists these integrations and any corresponding `logfire.instrume
5555
| [FastAPI](web-frameworks/fastapi.md) | Web Framework | [`logfire.instrument_fastapi()`][logfire.Logfire.instrument_fastapi] |
5656
| [FastStream](event-streams/faststream.md) | Task Queue | N/A (built in, config needed) |
5757
| [Flask](web-frameworks/flask.md) | Web Framework | [`logfire.instrument_flask()`][logfire.Logfire.instrument_flask] |
58-
| [HTTPX](http-clients/httpx.md) | HTTP Client | [`logfire.instrument_httpx()`][logfire.Logfire.instrument_httpx] |
58+
| [HTTPX and HTTPX2](http-clients/httpx.md) | HTTP Client | [`logfire.instrument_httpx()`][logfire.Logfire.instrument_httpx] |
5959
| [LangChain](llms/langchain.md) | AI Framework | N/A (built-in OpenTelemetry support) |
6060
| [LlamaIndex](llms/llamaindex.md) | AI Framework | N/A (requires LlamaIndex OpenTelemetry package) |
6161
| [LiteLLM](llms/litellm.md) | AI Gateway | N/A (requires LiteLLM callback setup) |

docs/nav.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
{
9494
"label": "HTTP Clients",
9595
"items": [
96-
{ "label": "HTTPX", "link": "/logfire/integrations/http-clients/httpx/" },
96+
{ "label": "HTTPX and HTTPX2", "link": "/logfire/integrations/http-clients/httpx/" },
9797
{ "label": "Requests", "link": "/logfire/integrations/http-clients/requests/" },
9898
{ "label": "AIOHTTP", "link": "/logfire/integrations/http-clients/aiohttp/" }
9999
]

0 commit comments

Comments
 (0)