|
23 | 23 |
|
24 | 24 | .. code-block:: python
|
25 | 25 |
|
26 |
| - import httpx |
27 |
| - from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor |
| 26 | + import httpx |
| 27 | + import asyncio |
| 28 | + from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor |
28 | 29 |
|
29 |
| - url = "https://some.url/get" |
30 |
| - HTTPXClientInstrumentor().instrument() |
| 30 | + url = "https://example.com" |
| 31 | + HTTPXClientInstrumentor().instrument() |
31 | 32 |
|
32 |
| - with httpx.Client() as client: |
33 |
| - response = client.get(url) |
| 33 | + with httpx.Client() as client: |
| 34 | + response = client.get(url) |
34 | 35 |
|
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)) |
37 | 41 |
|
38 | 42 | Instrumenting single clients
|
39 | 43 | ****************************
|
|
45 | 49 | .. code-block:: python
|
46 | 50 |
|
47 | 51 | import httpx
|
| 52 | + import asyncio |
48 | 53 | from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
|
49 | 54 |
|
50 |
| - url = "https://some.url/get" |
| 55 | + url = "https://example.com" |
51 | 56 |
|
52 |
| - with httpx.Client(transport=telemetry_transport) as client: |
| 57 | + with httpx.Client() as client: |
53 | 58 | HTTPXClientInstrumentor.instrument_client(client)
|
54 | 59 | response = client.get(url)
|
55 | 60 |
|
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) |
59 | 65 |
|
| 66 | + asyncio.run(get(url)) |
60 | 67 |
|
61 | 68 | Uninstrument
|
62 | 69 | ************
|
|
65 | 72 |
|
66 | 73 | .. code-block:: python
|
67 | 74 |
|
68 |
| - import httpx |
69 |
| - from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor |
| 75 | + import httpx |
| 76 | + from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor |
70 | 77 |
|
71 |
| - HTTPXClientInstrumentor().instrument() |
72 |
| - client = httpx.Client() |
| 78 | + HTTPXClientInstrumentor().instrument() |
| 79 | + client = httpx.Client() |
73 | 80 |
|
74 |
| - # Uninstrument a specific client |
75 |
| - HTTPXClientInstrumentor.uninstrument_client(client) |
| 81 | + # Uninstrument a specific client |
| 82 | + HTTPXClientInstrumentor.uninstrument_client(client) |
76 | 83 |
|
77 |
| - # Uninstrument all clients |
78 |
| - HTTPXClientInstrumentor().uninstrument() |
| 84 | + # Uninstrument all clients |
| 85 | + HTTPXClientInstrumentor().uninstrument() |
79 | 86 |
|
80 | 87 |
|
81 | 88 | Using transports directly
|
|
87 | 94 | .. code-block:: python
|
88 | 95 |
|
89 | 96 | import httpx
|
| 97 | + import asyncio |
90 | 98 | from opentelemetry.instrumentation.httpx import (
|
91 | 99 | AsyncOpenTelemetryTransport,
|
92 | 100 | SyncOpenTelemetryTransport,
|
93 | 101 | )
|
94 | 102 |
|
95 |
| - url = "https://some.url/get" |
| 103 | + url = "https://example.com" |
96 | 104 | transport = httpx.HTTPTransport()
|
97 | 105 | telemetry_transport = SyncOpenTelemetryTransport(transport)
|
98 | 106 |
|
|
102 | 110 | transport = httpx.AsyncHTTPTransport()
|
103 | 111 | telemetry_transport = AsyncOpenTelemetryTransport(transport)
|
104 | 112 |
|
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) |
107 | 116 |
|
| 117 | + asyncio.run(get(url)) |
108 | 118 |
|
109 | 119 | Request and response hooks
|
110 | 120 | ***************************
|
@@ -154,6 +164,7 @@ async def async_response_hook(span, request, response):
|
154 | 164 |
|
155 | 165 | .. code-block:: python
|
156 | 166 |
|
| 167 | + import httpx |
157 | 168 | from opentelemetry.instrumentation.httpx import SyncOpenTelemetryTransport, AsyncOpenTelemetryTransport
|
158 | 169 |
|
159 | 170 | def request_hook(span, request):
|
|
0 commit comments