Skip to content

Commit d8844b5

Browse files
authored
Add missing using instrumentation libraries pages: Python (#4369)
1 parent b2f1a44 commit d8844b5

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

static/refcache.json

+8
Original file line numberDiff line numberDiff line change
@@ -3823,6 +3823,10 @@
38233823
"StatusCode": 200,
38243824
"LastSeen": "2024-01-18T19:37:26.943394-05:00"
38253825
},
3826+
"https://github.com/open-telemetry/opentelemetry-python-contrib/": {
3827+
"StatusCode": 200,
3828+
"LastSeen": "2024-04-26T12:45:03.066448+02:00"
3829+
},
38263830
"https://github.com/open-telemetry/opentelemetry-python/": {
38273831
"StatusCode": 200,
38283832
"LastSeen": "2024-01-10T11:27:53.222542-08:00"
@@ -7127,6 +7131,10 @@
71277131
"StatusCode": 206,
71287132
"LastSeen": "2024-01-30T16:14:48.105173-05:00"
71297133
},
7134+
"https://pypi.org/project/opentelemetry-instrumentation-httpx/": {
7135+
"StatusCode": 206,
7136+
"LastSeen": "2024-04-28T16:14:45.577134727Z"
7137+
},
71307138
"https://qryn.metrico.in/#/support": {
71317139
"StatusCode": 206,
71327140
"LastSeen": "2024-01-30T16:03:54.589618-05:00"

0 commit comments

Comments
 (0)