Skip to content

Commit 02f3c18

Browse files
cnnradamsAlex Boten
authored and
Alex Boten
committed
refactor: Add common utils to opentelemetry-auto-instrumentation, rename opentelemetry-auto-instrumentation (open-telemetry#741)
1 parent 584c22f commit 02f3c18

File tree

60 files changed

+279
-295
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+279
-295
lines changed

docs/auto_instrumentation/auto_instrumentation.rst

Lines changed: 0 additions & 15 deletions
This file was deleted.

docs/auto_instrumentation/instrumentor.rst

Lines changed: 0 additions & 7 deletions
This file was deleted.

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
source_dirs = [
2828
os.path.abspath("../opentelemetry-api/src/"),
2929
os.path.abspath("../opentelemetry-sdk/src/"),
30-
os.path.abspath("../opentelemetry-auto-instrumentation/src/"),
30+
os.path.abspath("../opentelemetry-instrumentation/src/"),
3131
]
3232

3333
ext = "../ext"

docs/examples/auto-instrumentation/README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Installation
7272
.. code:: sh
7373
7474
$ pip install opentelemetry-sdk
75-
$ pip install opentelemetry-auto-instrumentation
75+
$ pip install opentelemetry-instrumentation
7676
$ pip install opentelemetry-ext-flask
7777
$ pip install requests
7878
@@ -138,7 +138,7 @@ and run this instead:
138138

139139
.. code:: sh
140140
141-
$ opentelemetry-auto-instrumentation python server_uninstrumented.py
141+
$ opentelemetry-instrument python server_uninstrumented.py
142142
143143
In the console where you previously executed ``client.py``, run again
144144
this again:

docs/examples/datadog_exporter/README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Auto-Instrumention Example
4444
pip install opentelemetry-api
4545
pip install opentelemetry-sdk
4646
pip install opentelemetry-ext-datadog
47-
pip install opentelemetry-auto-instrumentation
47+
pip install opentelemetry-instrumentation
4848
pip install opentelemetry-ext-flask
4949
pip install flask
5050
pip install requests
@@ -66,16 +66,16 @@ Auto-Instrumention Example
6666

6767
.. code-block:: sh
6868
69-
opentelemetry-auto-instrumentation python server.py
69+
opentelemetry-instrument python server.py
7070
7171
* Run client
7272

7373
.. code-block:: sh
7474
75-
opentelemetry-auto-instrumentation python client.py testing
75+
opentelemetry-instrument python client.py testing
7676
7777
* Run client with parameter to raise error
7878

7979
.. code-block:: sh
8080
81-
opentelemetry-auto-instrumentation python client.py error
81+
opentelemetry-instrument python client.py error

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ install <https://pip.pypa.io/en/stable/reference/pip_install/#editable-installs>
6868

6969
api/api
7070
sdk/sdk
71-
auto_instrumentation/auto_instrumentation
71+
instrumentation/instrumentation
7272

7373
.. toctree::
7474
:maxdepth: 2
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
OpenTelemetry Python Instrumentation
2+
====================================
3+
4+
.. automodule:: opentelemetry.instrumentation
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
8+
9+
Submodules
10+
----------
11+
12+
.. toctree::
13+
:maxdepth: 1
14+
15+
instrumentor

docs/instrumentation/instrumentor.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
opentelemetry.instrumentation.instrumentor package
2+
==================================================
3+
4+
.. automodule:: opentelemetry.instrumentation.instrumentor
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

eachdist.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
sortfirst=
55
opentelemetry-api
66
opentelemetry-sdk
7-
opentelemetry-auto-instrumentation
7+
opentelemetry-instrumentation
88
ext/opentelemetry-ext-wsgi
99
ext/opentelemetry-ext-dbapi
1010
ext/*

ext/opentelemetry-ext-aiohttp-client/setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ package_dir=
4040
packages=find_namespace:
4141
install_requires =
4242
opentelemetry-api >= 0.9.dev0
43+
opentelemetry-instrumentation == 0.9.dev0
4344
aiohttp ~= 3.0
4445

4546
[options.packages.find]

ext/opentelemetry-ext-aiohttp-client/src/opentelemetry/ext/aiohttp_client/__init__.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,38 +52,11 @@ def strip_query_params(url: yarl.URL) -> str:
5252
from opentelemetry import context as context_api
5353
from opentelemetry import propagators, trace
5454
from opentelemetry.ext.aiohttp_client.version import __version__
55+
from opentelemetry.instrumentation.utils import http_status_to_canonical_code
5556
from opentelemetry.trace import SpanKind
5657
from opentelemetry.trace.status import Status, StatusCanonicalCode
5758

5859

59-
# TODO: refactor this code to some common utility
60-
def http_status_to_canonical_code(status: int) -> StatusCanonicalCode:
61-
# pylint:disable=too-many-branches,too-many-return-statements
62-
if status < 100:
63-
return StatusCanonicalCode.UNKNOWN
64-
if status <= 399:
65-
return StatusCanonicalCode.OK
66-
if status <= 499:
67-
if status == 401: # HTTPStatus.UNAUTHORIZED:
68-
return StatusCanonicalCode.UNAUTHENTICATED
69-
if status == 403: # HTTPStatus.FORBIDDEN:
70-
return StatusCanonicalCode.PERMISSION_DENIED
71-
if status == 404: # HTTPStatus.NOT_FOUND:
72-
return StatusCanonicalCode.NOT_FOUND
73-
if status == 429: # HTTPStatus.TOO_MANY_REQUESTS:
74-
return StatusCanonicalCode.RESOURCE_EXHAUSTED
75-
return StatusCanonicalCode.INVALID_ARGUMENT
76-
if status <= 599:
77-
if status == 501: # HTTPStatus.NOT_IMPLEMENTED:
78-
return StatusCanonicalCode.UNIMPLEMENTED
79-
if status == 503: # HTTPStatus.SERVICE_UNAVAILABLE:
80-
return StatusCanonicalCode.UNAVAILABLE
81-
if status == 504: # HTTPStatus.GATEWAY_TIMEOUT:
82-
return StatusCanonicalCode.DEADLINE_EXCEEDED
83-
return StatusCanonicalCode.INTERNAL
84-
return StatusCanonicalCode.UNKNOWN
85-
86-
8760
def url_path_span_name(params: aiohttp.TraceRequestStartParams) -> str:
8861
"""Extract a span name from the request URL path.
8962

ext/opentelemetry-ext-aiohttp-client/tests/test_aiohttp_client_integration.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -98,43 +98,6 @@ async def default_handler(request):
9898
loop = asyncio.get_event_loop()
9999
return loop.run_until_complete(do_request())
100100

101-
def test_http_status_to_canonical_code(self):
102-
for status_code, expected in (
103-
(HTTPStatus.OK, StatusCanonicalCode.OK),
104-
(HTTPStatus.ACCEPTED, StatusCanonicalCode.OK),
105-
(HTTPStatus.IM_USED, StatusCanonicalCode.OK),
106-
(HTTPStatus.MULTIPLE_CHOICES, StatusCanonicalCode.OK),
107-
(HTTPStatus.BAD_REQUEST, StatusCanonicalCode.INVALID_ARGUMENT),
108-
(HTTPStatus.UNAUTHORIZED, StatusCanonicalCode.UNAUTHENTICATED),
109-
(HTTPStatus.FORBIDDEN, StatusCanonicalCode.PERMISSION_DENIED),
110-
(HTTPStatus.NOT_FOUND, StatusCanonicalCode.NOT_FOUND),
111-
(
112-
HTTPStatus.UNPROCESSABLE_ENTITY,
113-
StatusCanonicalCode.INVALID_ARGUMENT,
114-
),
115-
(
116-
HTTPStatus.TOO_MANY_REQUESTS,
117-
StatusCanonicalCode.RESOURCE_EXHAUSTED,
118-
),
119-
(HTTPStatus.NOT_IMPLEMENTED, StatusCanonicalCode.UNIMPLEMENTED),
120-
(HTTPStatus.SERVICE_UNAVAILABLE, StatusCanonicalCode.UNAVAILABLE),
121-
(
122-
HTTPStatus.GATEWAY_TIMEOUT,
123-
StatusCanonicalCode.DEADLINE_EXCEEDED,
124-
),
125-
(
126-
HTTPStatus.HTTP_VERSION_NOT_SUPPORTED,
127-
StatusCanonicalCode.INTERNAL,
128-
),
129-
(600, StatusCanonicalCode.UNKNOWN),
130-
(99, StatusCanonicalCode.UNKNOWN),
131-
):
132-
with self.subTest(status_code=status_code):
133-
actual = opentelemetry.ext.aiohttp_client.http_status_to_canonical_code(
134-
int(status_code)
135-
)
136-
self.assertEqual(actual, expected, status_code)
137-
138101
def test_status_codes(self):
139102
for status_code, span_status in (
140103
(HTTPStatus.OK, StatusCanonicalCode.OK),

ext/opentelemetry-ext-boto/setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ packages=find_namespace:
4242
install_requires =
4343
boto ~= 2.0
4444
opentelemetry-api == 0.9.dev0
45-
opentelemetry-auto-instrumentation == 0.9.dev0
45+
opentelemetry-instrumentation == 0.9.dev0
4646

4747
[options.extras_require]
4848
test =

ext/opentelemetry-ext-boto/src/opentelemetry/ext/boto/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Instrument `Boto`_ to trace service requests.
1616
1717
There are two options for instrumenting code. The first option is to use the
18-
``opentelemetry-auto-instrumentation`` executable which will automatically
18+
``opentelemetry-instrument`` executable which will automatically
1919
instrument your Boto client. The second is to programmatically enable
2020
instrumentation via the following code:
2121
@@ -50,8 +50,8 @@
5050
from boto.connection import AWSAuthConnection, AWSQueryConnection
5151
from wrapt import ObjectProxy, wrap_function_wrapper
5252

53-
from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
5453
from opentelemetry.ext.boto.version import __version__
54+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
5555
from opentelemetry.trace import SpanKind, get_tracer
5656

5757
logger = logging.getLogger(__name__)

ext/opentelemetry-ext-dbapi/setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ package_dir=
4141
packages=find_namespace:
4242
install_requires =
4343
opentelemetry-api == 0.9.dev0
44+
opentelemetry-instrumentation == 0.9.dev0
4445
wrapt >= 1.0.0, < 2.0.0
4546

4647
[options.extras_require]

ext/opentelemetry-ext-dbapi/src/opentelemetry/ext/dbapi/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import wrapt
4848

4949
from opentelemetry.ext.dbapi.version import __version__
50+
from opentelemetry.instrumentation.utils import unwrap
5051
from opentelemetry.trace import SpanKind, Tracer, TracerProvider, get_tracer
5152
from opentelemetry.trace.status import Status, StatusCanonicalCode
5253

@@ -141,9 +142,7 @@ def unwrap_connect(
141142
connect_module: Module name where the connect method is available.
142143
connect_method_name: The connect method name.
143144
"""
144-
conn = getattr(connect_module, connect_method_name, None)
145-
if isinstance(conn, wrapt.ObjectProxy):
146-
setattr(connect_module, connect_method_name, conn.__wrapped__)
145+
unwrap(connect_module, connect_method_name)
147146

148147

149148
def instrument_connection(

ext/opentelemetry-ext-django/setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ packages=find_namespace:
4242
install_requires =
4343
django >= 1.10
4444
opentelemetry-ext-wsgi == 0.9.dev0
45-
opentelemetry-auto-instrumentation == 0.9.dev0
45+
opentelemetry-instrumentation == 0.9.dev0
4646
opentelemetry-api == 0.9.dev0
4747

4848
[options.extras_require]

ext/opentelemetry-ext-django/src/opentelemetry/ext/django/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
from django.conf import settings
1818

19-
from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
2019
from opentelemetry.configuration import Configuration
2120
from opentelemetry.ext.django.middleware import _DjangoMiddleware
21+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
2222

2323
_logger = getLogger(__name__)
2424

ext/opentelemetry-ext-flask/setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ packages=find_namespace:
4242
install_requires =
4343
flask ~= 1.0
4444
opentelemetry-ext-wsgi == 0.9.dev0
45-
opentelemetry-auto-instrumentation == 0.9.dev0
45+
opentelemetry-instrumentation == 0.9.dev0
4646
opentelemetry-api == 0.9.dev0
4747

4848
[options.extras_require]

ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def hello():
5353

5454
import opentelemetry.ext.wsgi as otel_wsgi
5555
from opentelemetry import configuration, context, propagators, trace
56-
from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
5756
from opentelemetry.ext.flask.version import __version__
57+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
5858
from opentelemetry.util import disable_trace, time_ns
5959

6060
_logger = getLogger(__name__)

ext/opentelemetry-ext-jinja2/setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ package_dir=
4040
packages=find_namespace:
4141
install_requires =
4242
opentelemetry-api == 0.9.dev0
43-
opentelemetry-auto-instrumentation == 0.9.dev0
43+
opentelemetry-instrumentation == 0.9.dev0
4444
jinja2~=2.7
4545
wrapt >= 1.0.0, < 2.0.0
4646

ext/opentelemetry-ext-jinja2/src/opentelemetry/ext/jinja2/__init__.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@
4848
from wrapt import ObjectProxy
4949
from wrapt import wrap_function_wrapper as _wrap
5050

51-
from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
5251
from opentelemetry.ext.jinja2.version import __version__
52+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
53+
from opentelemetry.instrumentation.utils import unwrap
5354
from opentelemetry.trace import SpanKind, get_tracer
5455
from opentelemetry.trace.status import Status, StatusCanonicalCode
5556

@@ -115,12 +116,6 @@ def _wrap_load_template(tracer, wrapped, _, args, kwargs):
115116
)
116117

117118

118-
def _unwrap(obj, attr):
119-
func = getattr(obj, attr, None)
120-
if func and isinstance(func, ObjectProxy) and hasattr(func, "__wrapped__"):
121-
setattr(obj, attr, func.__wrapped__)
122-
123-
124119
class Jinja2Instrumentor(BaseInstrumentor):
125120
"""An instrumentor for jinja2
126121
@@ -141,7 +136,7 @@ def _instrument(self, **kwargs):
141136
)
142137

143138
def _uninstrument(self, **kwargs):
144-
_unwrap(jinja2.Template, "render")
145-
_unwrap(jinja2.Template, "generate")
146-
_unwrap(jinja2.Environment, "compile")
147-
_unwrap(jinja2.Environment, "_load_template")
139+
unwrap(jinja2.Template, "render")
140+
unwrap(jinja2.Template, "generate")
141+
unwrap(jinja2.Environment, "compile")
142+
unwrap(jinja2.Environment, "_load_template")

ext/opentelemetry-ext-mysql/setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ packages=find_namespace:
4242
install_requires =
4343
opentelemetry-api == 0.9.dev0
4444
opentelemetry-ext-dbapi == 0.9.dev0
45-
opentelemetry-auto-instrumentation == 0.9.dev0
45+
opentelemetry-instrumentation == 0.9.dev0
4646
mysql-connector-python ~= 8.0
4747
wrapt >= 1.0.0, < 2.0.0
4848

ext/opentelemetry-ext-mysql/src/opentelemetry/ext/mysql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646

4747
import mysql.connector
4848

49-
from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
5049
from opentelemetry.ext import dbapi
5150
from opentelemetry.ext.mysql.version import __version__
51+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
5252
from opentelemetry.trace import TracerProvider, get_tracer
5353

5454

ext/opentelemetry-ext-psycopg2/setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ packages=find_namespace:
4242
install_requires =
4343
opentelemetry-api == 0.9.dev0
4444
opentelemetry-ext-dbapi == 0.9.dev0
45-
opentelemetry-auto-instrumentation == 0.9.dev0
45+
opentelemetry-instrumentation == 0.9.dev0
4646
psycopg2-binary >= 2.7.3.1
4747
wrapt >= 1.0.0, < 2.0.0
4848

ext/opentelemetry-ext-psycopg2/src/opentelemetry/ext/psycopg2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
import psycopg2
4848
import wrapt
4949

50-
from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
5150
from opentelemetry.ext import dbapi
5251
from opentelemetry.ext.psycopg2.version import __version__
52+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
5353
from opentelemetry.trace import TracerProvider, get_tracer
5454

5555

0 commit comments

Comments
 (0)