Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add common utils to opentelemetry-auto-instrumentation, rename opentelemetry-auto-instrumentation #741

Merged
merged 11 commits into from
Jun 8, 2020
15 changes: 0 additions & 15 deletions docs/auto_instrumentation/auto_instrumentation.rst

This file was deleted.

7 changes: 0 additions & 7 deletions docs/auto_instrumentation/instrumentor.rst

This file was deleted.

2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
source_dirs = [
os.path.abspath("../opentelemetry-api/src/"),
os.path.abspath("../opentelemetry-sdk/src/"),
os.path.abspath("../opentelemetry-auto-instrumentation/src/"),
os.path.abspath("../opentelemetry-instrumentation/src/"),
]

ext = "../ext"
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/auto-instrumentation/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Installation
.. code:: sh

$ pip install opentelemetry-sdk
$ pip install opentelemetry-auto-instrumentation
$ pip install opentelemetry-instrumentation
$ pip install opentelemetry-ext-flask
$ pip install requests

Expand Down Expand Up @@ -138,7 +138,7 @@ and run this instead:

.. code:: sh

$ opentelemetry-auto-instrumentation python server_uninstrumented.py
$ opentelemetry-instrument python server_uninstrumented.py

In the console where you previously executed ``client.py``, run again
this again:
Expand Down
8 changes: 4 additions & 4 deletions docs/examples/datadog_exporter/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Auto-Instrumention Example
pip install opentelemetry-api
pip install opentelemetry-sdk
pip install opentelemetry-ext-datadog
pip install opentelemetry-auto-instrumentation
pip install opentelemetry-instrumentation
pip install opentelemetry-ext-flask
pip install flask
pip install requests
Expand All @@ -66,16 +66,16 @@ Auto-Instrumention Example

.. code-block:: sh

opentelemetry-auto-instrumentation python server.py
opentelemetry-instrument python server.py

* Run client

.. code-block:: sh

opentelemetry-auto-instrumentation python client.py testing
opentelemetry-instrument python client.py testing

* Run client with parameter to raise error

.. code-block:: sh

opentelemetry-auto-instrumentation python client.py error
opentelemetry-instrument python client.py error
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ install <https://pip.pypa.io/en/stable/reference/pip_install/#editable-installs>

api/api
sdk/sdk
auto_instrumentation/auto_instrumentation
instrumentation/instrumentation

.. toctree::
:maxdepth: 2
Expand Down
15 changes: 15 additions & 0 deletions docs/instrumentation/instrumentation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
OpenTelemetry Python Instrumentation
====================================

.. automodule:: opentelemetry.instrumentation
:members:
:undoc-members:
:show-inheritance:

Submodules
----------

.. toctree::
:maxdepth: 1

instrumentor
7 changes: 7 additions & 0 deletions docs/instrumentation/instrumentor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
opentelemetry.instrumentation.instrumentor package
==================================================

.. automodule:: opentelemetry.instrumentation.instrumentor
:members:
:undoc-members:
:show-inheritance:
2 changes: 1 addition & 1 deletion eachdist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
sortfirst=
opentelemetry-api
opentelemetry-sdk
opentelemetry-auto-instrumentation
opentelemetry-instrumentation
ext/opentelemetry-ext-wsgi
ext/opentelemetry-ext-dbapi
ext/*
Expand Down
1 change: 1 addition & 0 deletions ext/opentelemetry-ext-aiohttp-client/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ package_dir=
packages=find_namespace:
install_requires =
opentelemetry-api >= 0.9.dev0
opentelemetry-instrumentation == 0.9.dev0
aiohttp ~= 3.0

[options.packages.find]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,11 @@ def strip_query_params(url: yarl.URL) -> str:
from opentelemetry import context as context_api
from opentelemetry import propagators, trace
from opentelemetry.ext.aiohttp_client.version import __version__
from opentelemetry.instrumentation.utils import http_status_to_canonical_code
from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import Status, StatusCanonicalCode


# TODO: refactor this code to some common utility
def http_status_to_canonical_code(status: int) -> StatusCanonicalCode:
# pylint:disable=too-many-branches,too-many-return-statements
if status < 100:
return StatusCanonicalCode.UNKNOWN
if status <= 399:
return StatusCanonicalCode.OK
if status <= 499:
if status == 401: # HTTPStatus.UNAUTHORIZED:
return StatusCanonicalCode.UNAUTHENTICATED
if status == 403: # HTTPStatus.FORBIDDEN:
return StatusCanonicalCode.PERMISSION_DENIED
if status == 404: # HTTPStatus.NOT_FOUND:
return StatusCanonicalCode.NOT_FOUND
if status == 429: # HTTPStatus.TOO_MANY_REQUESTS:
return StatusCanonicalCode.RESOURCE_EXHAUSTED
return StatusCanonicalCode.INVALID_ARGUMENT
if status <= 599:
if status == 501: # HTTPStatus.NOT_IMPLEMENTED:
return StatusCanonicalCode.UNIMPLEMENTED
if status == 503: # HTTPStatus.SERVICE_UNAVAILABLE:
return StatusCanonicalCode.UNAVAILABLE
if status == 504: # HTTPStatus.GATEWAY_TIMEOUT:
return StatusCanonicalCode.DEADLINE_EXCEEDED
return StatusCanonicalCode.INTERNAL
return StatusCanonicalCode.UNKNOWN


def url_path_span_name(params: aiohttp.TraceRequestStartParams) -> str:
"""Extract a span name from the request URL path.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,43 +98,6 @@ async def default_handler(request):
loop = asyncio.get_event_loop()
return loop.run_until_complete(do_request())

def test_http_status_to_canonical_code(self):
for status_code, expected in (
(HTTPStatus.OK, StatusCanonicalCode.OK),
(HTTPStatus.ACCEPTED, StatusCanonicalCode.OK),
(HTTPStatus.IM_USED, StatusCanonicalCode.OK),
(HTTPStatus.MULTIPLE_CHOICES, StatusCanonicalCode.OK),
(HTTPStatus.BAD_REQUEST, StatusCanonicalCode.INVALID_ARGUMENT),
(HTTPStatus.UNAUTHORIZED, StatusCanonicalCode.UNAUTHENTICATED),
(HTTPStatus.FORBIDDEN, StatusCanonicalCode.PERMISSION_DENIED),
(HTTPStatus.NOT_FOUND, StatusCanonicalCode.NOT_FOUND),
(
HTTPStatus.UNPROCESSABLE_ENTITY,
StatusCanonicalCode.INVALID_ARGUMENT,
),
(
HTTPStatus.TOO_MANY_REQUESTS,
StatusCanonicalCode.RESOURCE_EXHAUSTED,
),
(HTTPStatus.NOT_IMPLEMENTED, StatusCanonicalCode.UNIMPLEMENTED),
(HTTPStatus.SERVICE_UNAVAILABLE, StatusCanonicalCode.UNAVAILABLE),
(
HTTPStatus.GATEWAY_TIMEOUT,
StatusCanonicalCode.DEADLINE_EXCEEDED,
),
(
HTTPStatus.HTTP_VERSION_NOT_SUPPORTED,
StatusCanonicalCode.INTERNAL,
),
(600, StatusCanonicalCode.UNKNOWN),
(99, StatusCanonicalCode.UNKNOWN),
):
with self.subTest(status_code=status_code):
actual = opentelemetry.ext.aiohttp_client.http_status_to_canonical_code(
int(status_code)
)
self.assertEqual(actual, expected, status_code)

def test_status_codes(self):
for status_code, span_status in (
(HTTPStatus.OK, StatusCanonicalCode.OK),
Expand Down
2 changes: 1 addition & 1 deletion ext/opentelemetry-ext-boto/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ packages=find_namespace:
install_requires =
boto ~= 2.0
opentelemetry-api == 0.9.dev0
opentelemetry-auto-instrumentation == 0.9.dev0
opentelemetry-instrumentation == 0.9.dev0

[options.extras_require]
test =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Instrument `Boto`_ to trace service requests.

There are two options for instrumenting code. The first option is to use the
``opentelemetry-auto-instrumentation`` executable which will automatically
``opentelemetry-instrument`` executable which will automatically
instrument your Boto client. The second is to programmatically enable
instrumentation via the following code:

Expand Down Expand Up @@ -50,8 +50,8 @@
from boto.connection import AWSAuthConnection, AWSQueryConnection
from wrapt import ObjectProxy, wrap_function_wrapper

from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.ext.boto.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.trace import SpanKind, get_tracer

logger = logging.getLogger(__name__)
Expand Down
1 change: 1 addition & 0 deletions ext/opentelemetry-ext-dbapi/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ package_dir=
packages=find_namespace:
install_requires =
opentelemetry-api == 0.9.dev0
opentelemetry-instrumentation == 0.9.dev0
wrapt >= 1.0.0, < 2.0.0

[options.extras_require]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import wrapt

from opentelemetry.ext.dbapi.version import __version__
from opentelemetry.instrumentation.utils import unwrap
from opentelemetry.trace import SpanKind, Tracer, TracerProvider, get_tracer
from opentelemetry.trace.status import Status, StatusCanonicalCode

Expand Down Expand Up @@ -141,9 +142,7 @@ def unwrap_connect(
connect_module: Module name where the connect method is available.
connect_method_name: The connect method name.
"""
conn = getattr(connect_module, connect_method_name, None)
if isinstance(conn, wrapt.ObjectProxy):
setattr(connect_module, connect_method_name, conn.__wrapped__)
unwrap(connect_module, connect_method_name)


def instrument_connection(
Expand Down
2 changes: 1 addition & 1 deletion ext/opentelemetry-ext-django/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ packages=find_namespace:
install_requires =
django >= 1.10
opentelemetry-ext-wsgi == 0.9.dev0
opentelemetry-auto-instrumentation == 0.9.dev0
opentelemetry-instrumentation == 0.9.dev0
opentelemetry-api == 0.9.dev0

[options.extras_require]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

from django.conf import settings

from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.configuration import Configuration
from opentelemetry.ext.django.middleware import _DjangoMiddleware
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor

_logger = getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion ext/opentelemetry-ext-flask/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ packages=find_namespace:
install_requires =
flask ~= 1.0
opentelemetry-ext-wsgi == 0.9.dev0
opentelemetry-auto-instrumentation == 0.9.dev0
opentelemetry-instrumentation == 0.9.dev0
opentelemetry-api == 0.9.dev0

[options.extras_require]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def hello():

import opentelemetry.ext.wsgi as otel_wsgi
from opentelemetry import configuration, context, propagators, trace
from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.ext.flask.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.util import disable_trace, time_ns

_logger = getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion ext/opentelemetry-ext-jinja2/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ package_dir=
packages=find_namespace:
install_requires =
opentelemetry-api == 0.9.dev0
opentelemetry-auto-instrumentation == 0.9.dev0
opentelemetry-instrumentation == 0.9.dev0
jinja2~=2.7
wrapt >= 1.0.0, < 2.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@
from wrapt import ObjectProxy
from wrapt import wrap_function_wrapper as _wrap

from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.ext.jinja2.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.utils import unwrap
from opentelemetry.trace import SpanKind, get_tracer
from opentelemetry.trace.status import Status, StatusCanonicalCode

Expand Down Expand Up @@ -115,12 +116,6 @@ def _wrap_load_template(tracer, wrapped, _, args, kwargs):
)


def _unwrap(obj, attr):
func = getattr(obj, attr, None)
if func and isinstance(func, ObjectProxy) and hasattr(func, "__wrapped__"):
setattr(obj, attr, func.__wrapped__)


class Jinja2Instrumentor(BaseInstrumentor):
"""An instrumentor for jinja2

Expand All @@ -141,7 +136,7 @@ def _instrument(self, **kwargs):
)

def _uninstrument(self, **kwargs):
_unwrap(jinja2.Template, "render")
_unwrap(jinja2.Template, "generate")
_unwrap(jinja2.Environment, "compile")
_unwrap(jinja2.Environment, "_load_template")
unwrap(jinja2.Template, "render")
unwrap(jinja2.Template, "generate")
unwrap(jinja2.Environment, "compile")
unwrap(jinja2.Environment, "_load_template")
2 changes: 1 addition & 1 deletion ext/opentelemetry-ext-mysql/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ packages=find_namespace:
install_requires =
opentelemetry-api == 0.9.dev0
opentelemetry-ext-dbapi == 0.9.dev0
opentelemetry-auto-instrumentation == 0.9.dev0
opentelemetry-instrumentation == 0.9.dev0
mysql-connector-python ~= 8.0
wrapt >= 1.0.0, < 2.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@

import mysql.connector

from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.ext import dbapi
from opentelemetry.ext.mysql.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.trace import TracerProvider, get_tracer


Expand Down
2 changes: 1 addition & 1 deletion ext/opentelemetry-ext-psycopg2/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ packages=find_namespace:
install_requires =
opentelemetry-api == 0.9.dev0
opentelemetry-ext-dbapi == 0.9.dev0
opentelemetry-auto-instrumentation == 0.9.dev0
opentelemetry-instrumentation == 0.9.dev0
psycopg2-binary >= 2.7.3.1
wrapt >= 1.0.0, < 2.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
import psycopg2
import wrapt

from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.ext import dbapi
from opentelemetry.ext.psycopg2.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.trace import TracerProvider, get_tracer


Expand Down
Loading