Skip to content

Improve httpx instrumentation examples #3387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions instrumentation/opentelemetry-instrumentation-httpx/README.rst
Original file line number Diff line number Diff line change
@@ -27,17 +27,22 @@ When using the instrumentor, all clients will automatically trace requests.

.. code-block:: python

import httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
import httpx
import asyncio
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

url = "https://example.com"
HTTPXClientInstrumentor().instrument()

with httpx.Client() as client:
response = client.get(url)

url = "https://some.url/get"
HTTPXClientInstrumentor().instrument()
async def get(url):
async with httpx.AsyncClient() as client:
response = await client.get(url)

with httpx.Client() as client:
response = client.get(url)
asyncio.run(get(url))

async with httpx.AsyncClient() as client:
response = await client.get(url)

Instrumenting single clients
****************************
@@ -49,18 +54,21 @@ use the `instrument_client` method.
.. code-block:: python

import httpx
import asyncio
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

url = "https://some.url/get"
url = "https://example.com"

with httpx.Client(transport=telemetry_transport) as client:
with httpx.Client() as client:
HTTPXClientInstrumentor.instrument_client(client)
response = client.get(url)

async with httpx.AsyncClient(transport=telemetry_transport) as client:
HTTPXClientInstrumentor.instrument_client(client)
response = await client.get(url)
async def get(url):
async with httpx.AsyncClient() as client:
HTTPXClientInstrumentor.instrument_client(client)
response = await client.get(url)

asyncio.run(get(url))

Uninstrument
************
@@ -91,12 +99,13 @@ If you don't want to use the instrumentor class, you can use the transport class
.. code-block:: python

import httpx
import asyncio
from opentelemetry.instrumentation.httpx import (
AsyncOpenTelemetryTransport,
SyncOpenTelemetryTransport,
)

url = "https://some.url/get"
url = "https://example.com"
transport = httpx.HTTPTransport()
telemetry_transport = SyncOpenTelemetryTransport(transport)

@@ -106,8 +115,11 @@ If you don't want to use the instrumentor class, you can use the transport class
transport = httpx.AsyncHTTPTransport()
telemetry_transport = AsyncOpenTelemetryTransport(transport)

async with httpx.AsyncClient(transport=telemetry_transport) as client:
response = await client.get(url)
async def get(url):
async with httpx.AsyncClient(transport=telemetry_transport) as client:
response = await client.get(url)

asyncio.run(get(url))


Request and response hooks
@@ -158,6 +170,7 @@ Or if you are using the transport classes directly:

.. code-block:: python

import httpx
from opentelemetry.instrumentation.httpx import SyncOpenTelemetryTransport, AsyncOpenTelemetryTransport

def request_hook(span, request):
Original file line number Diff line number Diff line change
@@ -23,17 +23,21 @@

.. code-block:: python

import httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
import httpx
import asyncio
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

url = "https://some.url/get"
HTTPXClientInstrumentor().instrument()
url = "https://example.com"
HTTPXClientInstrumentor().instrument()

with httpx.Client() as client:
response = client.get(url)
with httpx.Client() as client:
response = client.get(url)

async with httpx.AsyncClient() as client:
response = await client.get(url)
async def get(url):
async with httpx.AsyncClient() as client:
response = await client.get(url)

asyncio.run(get(url))

Instrumenting single clients
****************************
@@ -45,18 +49,21 @@
.. code-block:: python

import httpx
import asyncio
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

url = "https://some.url/get"
url = "https://example.com"

with httpx.Client(transport=telemetry_transport) as client:
with httpx.Client() as client:
HTTPXClientInstrumentor.instrument_client(client)
response = client.get(url)

async with httpx.AsyncClient(transport=telemetry_transport) as client:
HTTPXClientInstrumentor.instrument_client(client)
response = await client.get(url)
async def get(url):
async with httpx.AsyncClient() as client:
HTTPXClientInstrumentor.instrument_client(client)
response = await client.get(url)

asyncio.run(get(url))

Uninstrument
************
@@ -65,17 +72,17 @@

.. code-block:: python

import httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
import httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

HTTPXClientInstrumentor().instrument()
client = httpx.Client()
HTTPXClientInstrumentor().instrument()
client = httpx.Client()

# Uninstrument a specific client
HTTPXClientInstrumentor.uninstrument_client(client)
# Uninstrument a specific client
HTTPXClientInstrumentor.uninstrument_client(client)

# Uninstrument all clients
HTTPXClientInstrumentor().uninstrument()
# Uninstrument all clients
HTTPXClientInstrumentor().uninstrument()


Using transports directly
@@ -87,12 +94,13 @@
.. code-block:: python

import httpx
import asyncio
from opentelemetry.instrumentation.httpx import (
AsyncOpenTelemetryTransport,
SyncOpenTelemetryTransport,
)

url = "https://some.url/get"
url = "https://example.com"
transport = httpx.HTTPTransport()
telemetry_transport = SyncOpenTelemetryTransport(transport)

@@ -102,9 +110,11 @@
transport = httpx.AsyncHTTPTransport()
telemetry_transport = AsyncOpenTelemetryTransport(transport)

async with httpx.AsyncClient(transport=telemetry_transport) as client:
response = await client.get(url)
async def get(url):
async with httpx.AsyncClient(transport=telemetry_transport) as client:
response = await client.get(url)

asyncio.run(get(url))

Request and response hooks
***************************
@@ -154,6 +164,7 @@ async def async_response_hook(span, request, response):

.. code-block:: python

import httpx
from opentelemetry.instrumentation.httpx import SyncOpenTelemetryTransport, AsyncOpenTelemetryTransport

def request_hook(span, request):