diff --git a/instrumentation/opentelemetry-instrumentation-httpx/README.rst b/instrumentation/opentelemetry-instrumentation-httpx/README.rst index cc465dd615..ca6f2ff489 100644 --- a/instrumentation/opentelemetry-instrumentation-httpx/README.rst +++ b/instrumentation/opentelemetry-instrumentation-httpx/README.rst @@ -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): diff --git a/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py b/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py index ba4e5b4fd4..ef70c244e7 100644 --- a/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py @@ -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):