Skip to content

Commit 3a585b4

Browse files
authored
Improve httpx instrumentation examples (#3387)
1 parent e50cb32 commit 3a585b4

File tree

2 files changed

+64
-40
lines changed
  • instrumentation/opentelemetry-instrumentation-httpx

2 files changed

+64
-40
lines changed

instrumentation/opentelemetry-instrumentation-httpx/README.rst

+29-16
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,22 @@ When using the instrumentor, all clients will automatically trace requests.
2727

2828
.. code-block:: python
2929
30-
import httpx
31-
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
30+
import httpx
31+
import asyncio
32+
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
33+
34+
url = "https://example.com"
35+
HTTPXClientInstrumentor().instrument()
36+
37+
with httpx.Client() as client:
38+
response = client.get(url)
3239
33-
url = "https://some.url/get"
34-
HTTPXClientInstrumentor().instrument()
40+
async def get(url):
41+
async with httpx.AsyncClient() as client:
42+
response = await client.get(url)
3543
36-
with httpx.Client() as client:
37-
response = client.get(url)
44+
asyncio.run(get(url))
3845
39-
async with httpx.AsyncClient() as client:
40-
response = await client.get(url)
4146
4247
Instrumenting single clients
4348
****************************
@@ -49,18 +54,21 @@ use the `instrument_client` method.
4954
.. code-block:: python
5055
5156
import httpx
57+
import asyncio
5258
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
5359
54-
url = "https://some.url/get"
60+
url = "https://example.com"
5561
56-
with httpx.Client(transport=telemetry_transport) as client:
62+
with httpx.Client() as client:
5763
HTTPXClientInstrumentor.instrument_client(client)
5864
response = client.get(url)
5965
60-
async with httpx.AsyncClient(transport=telemetry_transport) as client:
61-
HTTPXClientInstrumentor.instrument_client(client)
62-
response = await client.get(url)
66+
async def get(url):
67+
async with httpx.AsyncClient() as client:
68+
HTTPXClientInstrumentor.instrument_client(client)
69+
response = await client.get(url)
6370
71+
asyncio.run(get(url))
6472
6573
Uninstrument
6674
************
@@ -91,12 +99,13 @@ If you don't want to use the instrumentor class, you can use the transport class
9199
.. code-block:: python
92100
93101
import httpx
102+
import asyncio
94103
from opentelemetry.instrumentation.httpx import (
95104
AsyncOpenTelemetryTransport,
96105
SyncOpenTelemetryTransport,
97106
)
98107
99-
url = "https://some.url/get"
108+
url = "https://example.com"
100109
transport = httpx.HTTPTransport()
101110
telemetry_transport = SyncOpenTelemetryTransport(transport)
102111
@@ -106,8 +115,11 @@ If you don't want to use the instrumentor class, you can use the transport class
106115
transport = httpx.AsyncHTTPTransport()
107116
telemetry_transport = AsyncOpenTelemetryTransport(transport)
108117
109-
async with httpx.AsyncClient(transport=telemetry_transport) as client:
110-
response = await client.get(url)
118+
async def get(url):
119+
async with httpx.AsyncClient(transport=telemetry_transport) as client:
120+
response = await client.get(url)
121+
122+
asyncio.run(get(url))
111123
112124
113125
Request and response hooks
@@ -158,6 +170,7 @@ Or if you are using the transport classes directly:
158170

159171
.. code-block:: python
160172
173+
import httpx
161174
from opentelemetry.instrumentation.httpx import SyncOpenTelemetryTransport, AsyncOpenTelemetryTransport
162175
163176
def request_hook(span, request):

instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py

+35-24
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,21 @@
2323
2424
.. code-block:: python
2525
26-
import httpx
27-
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
26+
import httpx
27+
import asyncio
28+
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
2829
29-
url = "https://some.url/get"
30-
HTTPXClientInstrumentor().instrument()
30+
url = "https://example.com"
31+
HTTPXClientInstrumentor().instrument()
3132
32-
with httpx.Client() as client:
33-
response = client.get(url)
33+
with httpx.Client() as client:
34+
response = client.get(url)
3435
35-
async with httpx.AsyncClient() as client:
36-
response = await client.get(url)
36+
async def get(url):
37+
async with httpx.AsyncClient() as client:
38+
response = await client.get(url)
39+
40+
asyncio.run(get(url))
3741
3842
Instrumenting single clients
3943
****************************
@@ -45,18 +49,21 @@
4549
.. code-block:: python
4650
4751
import httpx
52+
import asyncio
4853
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
4954
50-
url = "https://some.url/get"
55+
url = "https://example.com"
5156
52-
with httpx.Client(transport=telemetry_transport) as client:
57+
with httpx.Client() as client:
5358
HTTPXClientInstrumentor.instrument_client(client)
5459
response = client.get(url)
5560
56-
async with httpx.AsyncClient(transport=telemetry_transport) as client:
57-
HTTPXClientInstrumentor.instrument_client(client)
58-
response = await client.get(url)
61+
async def get(url):
62+
async with httpx.AsyncClient() as client:
63+
HTTPXClientInstrumentor.instrument_client(client)
64+
response = await client.get(url)
5965
66+
asyncio.run(get(url))
6067
6168
Uninstrument
6269
************
@@ -65,17 +72,17 @@
6572
6673
.. code-block:: python
6774
68-
import httpx
69-
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
75+
import httpx
76+
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
7077
71-
HTTPXClientInstrumentor().instrument()
72-
client = httpx.Client()
78+
HTTPXClientInstrumentor().instrument()
79+
client = httpx.Client()
7380
74-
# Uninstrument a specific client
75-
HTTPXClientInstrumentor.uninstrument_client(client)
81+
# Uninstrument a specific client
82+
HTTPXClientInstrumentor.uninstrument_client(client)
7683
77-
# Uninstrument all clients
78-
HTTPXClientInstrumentor().uninstrument()
84+
# Uninstrument all clients
85+
HTTPXClientInstrumentor().uninstrument()
7986
8087
8188
Using transports directly
@@ -87,12 +94,13 @@
8794
.. code-block:: python
8895
8996
import httpx
97+
import asyncio
9098
from opentelemetry.instrumentation.httpx import (
9199
AsyncOpenTelemetryTransport,
92100
SyncOpenTelemetryTransport,
93101
)
94102
95-
url = "https://some.url/get"
103+
url = "https://example.com"
96104
transport = httpx.HTTPTransport()
97105
telemetry_transport = SyncOpenTelemetryTransport(transport)
98106
@@ -102,9 +110,11 @@
102110
transport = httpx.AsyncHTTPTransport()
103111
telemetry_transport = AsyncOpenTelemetryTransport(transport)
104112
105-
async with httpx.AsyncClient(transport=telemetry_transport) as client:
106-
response = await client.get(url)
113+
async def get(url):
114+
async with httpx.AsyncClient(transport=telemetry_transport) as client:
115+
response = await client.get(url)
107116
117+
asyncio.run(get(url))
108118
109119
Request and response hooks
110120
***************************
@@ -154,6 +164,7 @@ async def async_response_hook(span, request, response):
154164
155165
.. code-block:: python
156166
167+
import httpx
157168
from opentelemetry.instrumentation.httpx import SyncOpenTelemetryTransport, AsyncOpenTelemetryTransport
158169
159170
def request_hook(span, request):

0 commit comments

Comments
 (0)