|
| 1 | +--- |
| 2 | +title: Using instrumentation libraries |
| 3 | +linkTitle: Libraries |
| 4 | +weight: 40 |
| 5 | +cSpell:ignore: HTTPX httpx instrumentor uninstrument |
| 6 | +--- |
| 7 | + |
| 8 | +{{% docs/languages/libraries-intro "Python" %}} |
| 9 | + |
| 10 | +## Use instrumentation libraries |
| 11 | + |
| 12 | +If a library does not ship with native OpenTelemetry support, you can use |
| 13 | +[instrumentation libraries](/docs/specs/otel/glossary/#instrumentation-library) |
| 14 | +to generate telemetry data for a library or framework. |
| 15 | + |
| 16 | +For example, |
| 17 | +[the instrumentation library for HTTPX](https://pypi.org/project/opentelemetry-instrumentation-httpx/) |
| 18 | +automatically creates [spans](/docs/concepts/signals/traces/#spans) based on |
| 19 | +HTTP requests. |
| 20 | + |
| 21 | +## Setup |
| 22 | + |
| 23 | +You can install each instrumentation library separately using pip. For example: |
| 24 | + |
| 25 | +```sh |
| 26 | +pip install opentelemetry-instrumentation-{instrumented-library} |
| 27 | +``` |
| 28 | + |
| 29 | +In the previous example, `{instrumented-library}` is the name of the |
| 30 | +instrumentation. |
| 31 | + |
| 32 | +To install a development version, clone or fork the |
| 33 | +`opentelemetry-python-contrib` repository and run the following command to do an |
| 34 | +editable installation: |
| 35 | + |
| 36 | +```sh |
| 37 | +pip install -e ./instrumentation/opentelemetry-instrumentation-{integration} |
| 38 | +``` |
| 39 | + |
| 40 | +After installation, you will need to initialize the instrumentation library. |
| 41 | +Each library typically has its own way to initialize. |
| 42 | + |
| 43 | +## Example with HTTPX instrumentation |
| 44 | + |
| 45 | +Here's how you can instrument HTTP requests made using the `httpx` library. |
| 46 | + |
| 47 | +First, install the instrumentation library using pip: |
| 48 | + |
| 49 | +```sh |
| 50 | +pip install opentelemetry-instrumentation-httpx |
| 51 | +``` |
| 52 | + |
| 53 | +Next, use the instrumentor to automatically trace requests from all clients: |
| 54 | + |
| 55 | +```python |
| 56 | +import httpx |
| 57 | +from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor |
| 58 | + |
| 59 | +url = "https://some.url/get" |
| 60 | +HTTPXClientInstrumentor().instrument() |
| 61 | + |
| 62 | +with httpx.Client() as client: |
| 63 | + response = client.get(url) |
| 64 | + |
| 65 | +async with httpx.AsyncClient() as client: |
| 66 | + response = await client.get(url) |
| 67 | +``` |
| 68 | + |
| 69 | +### Turn off instrumentations |
| 70 | + |
| 71 | +If needed, you can uninstrument specific clients or all clients using the |
| 72 | +`uninstrument_client` method. For example: |
| 73 | + |
| 74 | +```python |
| 75 | +import httpx |
| 76 | +from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor |
| 77 | + |
| 78 | +HTTPXClientInstrumentor().instrument() |
| 79 | +client = httpx.Client() |
| 80 | + |
| 81 | +# Uninstrument a specific client |
| 82 | +HTTPXClientInstrumentor.uninstrument_client(client) |
| 83 | + |
| 84 | +# Uninstrument all clients |
| 85 | +HTTPXClientInstrumentor().uninstrument() |
| 86 | +``` |
| 87 | + |
| 88 | +## Available instrumentation libraries |
| 89 | + |
| 90 | +A full list of instrumentation libraries produced by OpenTelemetry is available |
| 91 | +from the [opentelemetry-python-contrib][] repository. |
| 92 | + |
| 93 | +You can also find more instrumentations available in the |
| 94 | +[registry](/ecosystem/registry/?language=python&component=instrumentation). |
| 95 | + |
| 96 | +## Next steps |
| 97 | + |
| 98 | +After you have set up instrumentation libraries, you might want to add your own |
| 99 | +[instrumentation](/docs/languages/python/instrumentation) to your code, to |
| 100 | +collect custom telemetry data. |
| 101 | + |
| 102 | +You might also want to configure an appropriate exporter to |
| 103 | +[export your telemetry data](/docs/languages/python/exporters) to one or more |
| 104 | +telemetry backends. |
| 105 | + |
| 106 | +You can also check the |
| 107 | +[automatic instrumentation for Python](/docs/languages/python/automatic). |
| 108 | + |
| 109 | +[opentelemetry-python-contrib]: |
| 110 | + https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation#readme |
0 commit comments