Skip to content

Commit 6daf581

Browse files
andremmemdnetoxrmx
authored
Improve urllib3 instrumentation examples (#3347)
* Improve urllib3 instrumentation example * Port urllib3 instrumentation example improvements to README.rst * Add trailing commas to urllib3 instrumentation examples --------- Co-authored-by: Emídio Neto <[email protected]> Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent db52193 commit 6daf581

File tree

2 files changed

+53
-12
lines changed
  • instrumentation/opentelemetry-instrumentation-urllib3

2 files changed

+53
-12
lines changed

instrumentation/opentelemetry-instrumentation-urllib3/README.rst

+38-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ Installation
1616

1717
pip install opentelemetry-instrumentation-urllib3
1818

19+
Usage
20+
-----
21+
.. code-block:: python
22+
23+
import urllib3
24+
from opentelemetry.instrumentation.urllib3 import URLLib3Instrumentor
25+
26+
def strip_query_params(url: str) -> str:
27+
return url.split("?")[0]
28+
29+
URLLib3Instrumentor().instrument(
30+
# Remove all query params from the URL attribute on the span.
31+
url_filter=strip_query_params,
32+
)
33+
34+
http = urllib3.PoolManager()
35+
response = http.request("GET", "https://www.example.org/")
36+
1937
Configuration
2038
-------------
2139

@@ -29,17 +47,31 @@ The hooks can be configured as follows:
2947

3048
.. code:: python
3149
32-
# `request` is an instance of urllib3.connectionpool.HTTPConnectionPool
33-
def request_hook(span, request):
50+
from typing import Any
51+
52+
from urllib3.connectionpool import HTTPConnectionPool
53+
from urllib3.response import HTTPResponse
54+
55+
from opentelemetry.instrumentation.urllib3 import RequestInfo, URLLib3Instrumentor
56+
from opentelemetry.trace import Span
57+
58+
def request_hook(
59+
span: Span,
60+
pool: HTTPConnectionPool,
61+
request_info: RequestInfo,
62+
) -> Any:
3463
pass
3564
36-
# `request` is an instance of urllib3.connectionpool.HTTPConnectionPool
37-
# `response` is an instance of urllib3.response.HTTPResponse
38-
def response_hook(span, request, response):
65+
def response_hook(
66+
span: Span,
67+
pool: HTTPConnectionPool,
68+
response: HTTPResponse,
69+
) -> Any:
3970
pass
4071
4172
URLLib3Instrumentor().instrument(
42-
request_hook=request_hook, response_hook=response_hook
73+
request_hook=request_hook,
74+
response_hook=response_hook,
4375
)
4476
4577
Exclude lists

instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,31 @@ def strip_query_params(url: str) -> str:
4747
4848
.. code:: python
4949
50+
from typing import Any
51+
52+
from urllib3.connectionpool import HTTPConnectionPool
53+
from urllib3.response import HTTPResponse
54+
55+
from opentelemetry.instrumentation.urllib3 import RequestInfo, URLLib3Instrumentor
56+
from opentelemetry.trace import Span
57+
5058
def request_hook(
5159
span: Span,
52-
pool: urllib3.connectionpool.HTTPConnectionPool,
60+
pool: HTTPConnectionPool,
5361
request_info: RequestInfo,
5462
) -> Any:
55-
...
63+
pass
5664
5765
def response_hook(
5866
span: Span,
59-
pool: urllib3.connectionpool.HTTPConnectionPool,
60-
response: urllib3.response.HTTPResponse,
67+
pool: HTTPConnectionPool,
68+
response: HTTPResponse,
6169
) -> Any:
62-
...
70+
pass
6371
6472
URLLib3Instrumentor().instrument(
65-
request_hook=request_hook, response_hook=response_hook
73+
request_hook=request_hook,
74+
response_hook=response_hook,
6675
)
6776
6877
Exclude lists

0 commit comments

Comments
 (0)