diff --git a/docs-requirements.txt b/docs-requirements.txt index f2d6ec2a57e..ca3db13e0a5 100644 --- a/docs-requirements.txt +++ b/docs-requirements.txt @@ -3,6 +3,7 @@ sphinx-rtd-theme~=0.4 sphinx-autodoc-typehints~=1.10.2 # Required by ext packages +aiohttp ~= 3.0 Deprecated>=1.2.6 PyMySQL~=0.9.3 flask~=1.0 diff --git a/docs/conf.py b/docs/conf.py index acd44c0c7af..c71995dfb4f 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -68,6 +68,7 @@ "https://opentracing-python.readthedocs.io/en/latest/", None, ), + "aiohttp": ("https://aiohttp.readthedocs.io/en/stable/", None), } # http://www.sphinx-doc.org/en/master/config.html#confval-nitpicky diff --git a/docs/ext/aiohttp_client/aiohttp_client.rst b/docs/ext/aiohttp_client/aiohttp_client.rst new file mode 100644 index 00000000000..e5ab26b0ea0 --- /dev/null +++ b/docs/ext/aiohttp_client/aiohttp_client.rst @@ -0,0 +1,7 @@ +OpenTelemetry aiohttp client Integration +======================================== + +.. automodule:: opentelemetry.ext.aiohttp_client + :members: + :undoc-members: + :show-inheritance: diff --git a/ext/opentelemetry-ext-aiohttp-client/CHANGELOG.md b/ext/opentelemetry-ext-aiohttp-client/CHANGELOG.md new file mode 100644 index 00000000000..3e04402cea9 --- /dev/null +++ b/ext/opentelemetry-ext-aiohttp-client/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + +## Unreleased + +- Initial release diff --git a/ext/opentelemetry-ext-aiohttp-client/LICENSE b/ext/opentelemetry-ext-aiohttp-client/LICENSE new file mode 100644 index 00000000000..261eeb9e9f8 --- /dev/null +++ b/ext/opentelemetry-ext-aiohttp-client/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/ext/opentelemetry-ext-aiohttp-client/MANIFEST.in b/ext/opentelemetry-ext-aiohttp-client/MANIFEST.in new file mode 100644 index 00000000000..aed3e33273b --- /dev/null +++ b/ext/opentelemetry-ext-aiohttp-client/MANIFEST.in @@ -0,0 +1,9 @@ +graft src +graft tests +global-exclude *.pyc +global-exclude *.pyo +global-exclude __pycache__/* +include CHANGELOG.md +include MANIFEST.in +include README.rst +include LICENSE diff --git a/ext/opentelemetry-ext-aiohttp-client/README.rst b/ext/opentelemetry-ext-aiohttp-client/README.rst new file mode 100644 index 00000000000..c257639cf34 --- /dev/null +++ b/ext/opentelemetry-ext-aiohttp-client/README.rst @@ -0,0 +1,24 @@ +OpenTelemetry aiohttp client Integration +======================================== + +|pypi| + +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-ext-aiohttp-client.svg + :target: https://pypi.org/project/opentelemetry-ext-aiohttp-client/ + +This library allows tracing HTTP requests made by the +`aiohttp client `_ library. + +Installation +------------ + +:: + + pip install opentelemetry-ext-aiohttp-client + + +References +---------- + +* `OpenTelemetry Project `_ +* `aiohttp client Tracing `_ diff --git a/ext/opentelemetry-ext-aiohttp-client/setup.cfg b/ext/opentelemetry-ext-aiohttp-client/setup.cfg new file mode 100644 index 00000000000..6c82039ecdf --- /dev/null +++ b/ext/opentelemetry-ext-aiohttp-client/setup.cfg @@ -0,0 +1,46 @@ +# Copyright 2020, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +[metadata] +name = opentelemetry-ext-aiohttp-client +description = OpenTelemetry aiohttp client integration +long_description = file: README.rst +long_description_content_type = text/x-rst +author = OpenTelemetry Authors +author_email = cncf-opentelemetry-contributors@lists.cncf.io +url = https://github.com/open-telemetry/opentelemetry-python/ext/opentelemetry-ext-aiohttp-client +platforms = any +license = Apache-2.0 +classifiers = + Development Status :: 3 - Alpha + Intended Audience :: Developers + License :: OSI Approved :: Apache Software License + Programming Language :: Python + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + +[options] +python_requires = >=3.5.3 +package_dir= + =src +packages=find_namespace: +install_requires = + opentelemetry-api >= 0.7.dev0 + aiohttp ~= 3.0 + +[options.packages.find] +where = src diff --git a/ext/opentelemetry-ext-aiohttp-client/setup.py b/ext/opentelemetry-ext-aiohttp-client/setup.py new file mode 100644 index 00000000000..1b44425f066 --- /dev/null +++ b/ext/opentelemetry-ext-aiohttp-client/setup.py @@ -0,0 +1,26 @@ +# Copyright 2020, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os + +import setuptools + +BASE_DIR = os.path.dirname(__file__) +VERSION_FILENAME = os.path.join( + BASE_DIR, "src", "opentelemetry", "ext", "aiohttp_client", "version.py" +) +PACKAGE_INFO = {} +with open(VERSION_FILENAME) as f: + exec(f.read(), PACKAGE_INFO) + +setuptools.setup(version=PACKAGE_INFO["__version__"]) diff --git a/ext/opentelemetry-ext-aiohttp-client/src/opentelemetry/ext/aiohttp_client/__init__.py b/ext/opentelemetry-ext-aiohttp-client/src/opentelemetry/ext/aiohttp_client/__init__.py new file mode 100644 index 00000000000..3c4e7f4eda8 --- /dev/null +++ b/ext/opentelemetry-ext-aiohttp-client/src/opentelemetry/ext/aiohttp_client/__init__.py @@ -0,0 +1,234 @@ +# Copyright 2020, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +The opentelemetry-ext-aiohttp-client package allows tracing HTTP requests +made by the aiohttp client library. + +Usage +----- + + .. code:: python + + import aiohttp + from opentelemetry.ext.aiohttp_client import ( + create_trace_config, + url_path_span_name + ) + import yarl + + def strip_query_params(url: yarl.URL) -> str: + return str(url.with_query(None)) + + async with aiohttp.ClientSession(trace_configs=[create_trace_config( + # Remove all query params from the URL attribute on the span. + url_filter=strip_query_params, + # Use the URL's path as the span name. + span_name=url_path_span_name + )]) as session: + async with session.get(url) as response: + await response.text() + +""" + +import contextlib +import socket +import types +import typing + +import aiohttp + +from opentelemetry import context as context_api +from opentelemetry import propagators, trace +from opentelemetry.ext.aiohttp_client.version import __version__ +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. + + A simple callable to extract the path portion of the requested URL + for use as the span name. + + :param aiohttp.TraceRequestStartParams params: Parameters describing + the traced request. + + :return: The URL path. + :rtype: str + """ + return params.url.path + + +def create_trace_config( + url_filter: typing.Optional[typing.Callable[[str], str]] = None, + span_name: typing.Optional[ + typing.Union[ + typing.Callable[[aiohttp.TraceRequestStartParams], str], str + ] + ] = None, +) -> aiohttp.TraceConfig: + """Create an aiohttp-compatible trace configuration. + + One span is created for the entire HTTP request, including initial + TCP/TLS setup if the connection doesn't exist. + + By default the span name is set to the HTTP request method. + + Example usage: + + .. code:: python + + import aiohttp + from opentelemetry.ext.aiohttp_client import create_trace_config + + async with aiohttp.ClientSession(trace_configs=[create_trace_config()]) as session: + async with session.get(url) as response: + await response.text() + + + :param url_filter: A callback to process the requested URL prior to adding + it as a span attribute. This can be useful to remove sensitive data + such as API keys or user personal information. + + :param str span_name: Override the default span name. + + :return: An object suitable for use with :py:class:`aiohttp.ClientSession`. + :rtype: :py:class:`aiohttp.TraceConfig` + """ + # `aiohttp.TraceRequestStartParams` resolves to `aiohttp.tracing.TraceRequestStartParams` + # which doesn't exist in the aiottp intersphinx inventory. + # Explicitly specify the type for the `span_name` param and rtype to work + # around this issue. + + tracer = trace.get_tracer_provider().get_tracer(__name__, __version__) + + def _end_trace(trace_config_ctx: types.SimpleNamespace): + context_api.detach(trace_config_ctx.token) + trace_config_ctx.span.end() + + async def on_request_start( + unused_session: aiohttp.ClientSession, + trace_config_ctx: types.SimpleNamespace, + params: aiohttp.TraceRequestStartParams, + ): + http_method = params.method.upper() + if trace_config_ctx.span_name is None: + request_span_name = http_method + elif callable(trace_config_ctx.span_name): + request_span_name = str(trace_config_ctx.span_name(params)) + else: + request_span_name = str(trace_config_ctx.span_name) + + trace_config_ctx.span = trace_config_ctx.tracer.start_span( + request_span_name, + kind=SpanKind.CLIENT, + attributes={ + "component": "http", + "http.method": http_method, + "http.url": trace_config_ctx.url_filter(params.url) + if callable(trace_config_ctx.url_filter) + else str(params.url), + }, + ) + + trace_config_ctx.token = context_api.attach( + trace.propagation.set_span_in_context(trace_config_ctx.span) + ) + + propagators.inject( + tracer, type(params.headers).__setitem__, params.headers + ) + + async def on_request_end( + unused_session: aiohttp.ClientSession, + trace_config_ctx: types.SimpleNamespace, + params: aiohttp.TraceRequestEndParams, + ): + trace_config_ctx.span.set_status( + Status(http_status_to_canonical_code(int(params.response.status))) + ) + trace_config_ctx.span.set_attribute( + "http.status_code", params.response.status + ) + trace_config_ctx.span.set_attribute( + "http.status_text", params.response.reason + ) + _end_trace(trace_config_ctx) + + async def on_request_exception( + unused_session: aiohttp.ClientSession, + trace_config_ctx: types.SimpleNamespace, + params: aiohttp.TraceRequestExceptionParams, + ): + if isinstance( + params.exception, + (aiohttp.ServerTimeoutError, aiohttp.TooManyRedirects), + ): + status = StatusCanonicalCode.DEADLINE_EXCEEDED + # Assume any getaddrinfo error is a DNS failure. + elif isinstance( + params.exception, aiohttp.ClientConnectorError + ) and isinstance(params.exception.os_error, socket.gaierror): + # DNS resolution failed + status = StatusCanonicalCode.UNKNOWN + else: + status = StatusCanonicalCode.UNAVAILABLE + + trace_config_ctx.span.set_status(Status(status)) + _end_trace(trace_config_ctx) + + def _trace_config_ctx_factory(**kwargs): + kwargs.setdefault("trace_request_ctx", {}) + return types.SimpleNamespace( + span_name=span_name, tracer=tracer, url_filter=url_filter, **kwargs + ) + + trace_config = aiohttp.TraceConfig( + trace_config_ctx_factory=_trace_config_ctx_factory + ) + + trace_config.on_request_start.append(on_request_start) + trace_config.on_request_end.append(on_request_end) + trace_config.on_request_exception.append(on_request_exception) + + return trace_config diff --git a/ext/opentelemetry-ext-aiohttp-client/src/opentelemetry/ext/aiohttp_client/version.py b/ext/opentelemetry-ext-aiohttp-client/src/opentelemetry/ext/aiohttp_client/version.py new file mode 100644 index 00000000000..fefa5ee9175 --- /dev/null +++ b/ext/opentelemetry-ext-aiohttp-client/src/opentelemetry/ext/aiohttp_client/version.py @@ -0,0 +1,15 @@ +# Copyright 2020, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +__version__ = "0.7.dev0" diff --git a/ext/opentelemetry-ext-aiohttp-client/tests/__init__.py b/ext/opentelemetry-ext-aiohttp-client/tests/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/ext/opentelemetry-ext-aiohttp-client/tests/test_aiohttp_client_integration.py b/ext/opentelemetry-ext-aiohttp-client/tests/test_aiohttp_client_integration.py new file mode 100644 index 00000000000..ae29801797c --- /dev/null +++ b/ext/opentelemetry-ext-aiohttp-client/tests/test_aiohttp_client_integration.py @@ -0,0 +1,338 @@ +# Copyright 2020, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import asyncio +import contextlib +import typing +import urllib.parse +from http import HTTPStatus + +import aiohttp +import aiohttp.test_utils +import yarl + +import opentelemetry.ext.aiohttp_client +from opentelemetry.test.test_base import TestBase +from opentelemetry.trace.status import StatusCanonicalCode + + +class TestAioHttpIntegration(TestBase): + maxDiff = None + + def assert_spans(self, spans): + self.assertEqual( + [ + ( + span.name, + (span.status.canonical_code, span.status.description), + dict(span.attributes), + ) + for span in self.memory_exporter.get_finished_spans() + ], + spans, + ) + + def test_url_path_span_name(self): + for url, expected in ( + ( + yarl.URL("http://hostname.local:1234/some/path?query=params"), + "/some/path", + ), + (yarl.URL("http://hostname.local:1234"), "/"), + ): + with self.subTest(url=url): + params = aiohttp.TraceRequestStartParams("METHOD", url, {}) + actual = opentelemetry.ext.aiohttp_client.url_path_span_name( + params + ) + self.assertEqual(actual, expected) + self.assertIsInstance(actual, str) + + @staticmethod + def _http_request( + trace_config, + url: str, + method: str = "GET", + status_code: int = HTTPStatus.OK, + request_handler: typing.Callable = None, + **kwargs + ) -> typing.Tuple[str, int]: + """Helper to start an aiohttp test server and send an actual HTTP request to it.""" + + async def do_request(): + async def default_handler(unused_request): + return aiohttp.web.Response(status=int(status_code)) + + handler = request_handler or default_handler + + app = aiohttp.web.Application() + parsed_url = urllib.parse.urlparse(url) + app.add_routes([aiohttp.web.get(parsed_url.path, handler)]) + app.add_routes([aiohttp.web.post(parsed_url.path, handler)]) + app.add_routes([aiohttp.web.patch(parsed_url.path, handler)]) + + with contextlib.suppress(aiohttp.ClientError): + async with aiohttp.test_utils.TestServer(app) as server: + netloc = (server.host, server.port) + async with aiohttp.test_utils.TestClient( + server, trace_configs=[trace_config] + ) as client: + await client.start_server() + await client.request( + method, url, trace_request_ctx={}, **kwargs + ) + return netloc + + 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), + (HTTPStatus.TEMPORARY_REDIRECT, StatusCanonicalCode.OK), + (HTTPStatus.SERVICE_UNAVAILABLE, StatusCanonicalCode.UNAVAILABLE), + ( + HTTPStatus.GATEWAY_TIMEOUT, + StatusCanonicalCode.DEADLINE_EXCEEDED, + ), + ): + with self.subTest(status_code=status_code): + host, port = self._http_request( + trace_config=opentelemetry.ext.aiohttp_client.create_trace_config(), + url="/test-path?query=param#foobar", + status_code=status_code, + ) + + self.assert_spans( + [ + ( + "GET", + (span_status, None), + { + "component": "http", + "http.method": "GET", + "http.url": "http://{}:{}/test-path?query=param#foobar".format( + host, port + ), + "http.status_code": int(status_code), + "http.status_text": status_code.phrase, + }, + ) + ] + ) + + self.memory_exporter.clear() + + def test_span_name_option(self): + for span_name, method, path, expected in ( + ("static", "POST", "/static-span-name", "static"), + ( + lambda params: "{} - {}".format( + params.method, params.url.path + ), + "PATCH", + "/some/path", + "PATCH - /some/path", + ), + ): + with self.subTest(span_name=span_name, method=method, path=path): + host, port = self._http_request( + trace_config=opentelemetry.ext.aiohttp_client.create_trace_config( + span_name=span_name + ), + method=method, + url=path, + status_code=HTTPStatus.OK, + ) + + self.assert_spans( + [ + ( + expected, + (StatusCanonicalCode.OK, None), + { + "component": "http", + "http.method": method, + "http.url": "http://{}:{}{}".format( + host, port, path + ), + "http.status_code": int(HTTPStatus.OK), + "http.status_text": HTTPStatus.OK.phrase, + }, + ) + ] + ) + self.memory_exporter.clear() + + def test_url_filter_option(self): + # Strips all query params from URL before adding as a span attribute. + def strip_query_params(url: yarl.URL) -> str: + return str(url.with_query(None)) + + host, port = self._http_request( + trace_config=opentelemetry.ext.aiohttp_client.create_trace_config( + url_filter=strip_query_params + ), + url="/some/path?query=param&other=param2", + status_code=HTTPStatus.OK, + ) + + self.assert_spans( + [ + ( + "GET", + (StatusCanonicalCode.OK, None), + { + "component": "http", + "http.method": "GET", + "http.url": "http://{}:{}/some/path".format( + host, port + ), + "http.status_code": int(HTTPStatus.OK), + "http.status_text": HTTPStatus.OK.phrase, + }, + ) + ] + ) + + def test_connection_errors(self): + trace_configs = [ + opentelemetry.ext.aiohttp_client.create_trace_config() + ] + + for url, expected_status in ( + ("http://this-is-unknown.local/", StatusCanonicalCode.UNKNOWN), + ("http://127.0.0.1:1/", StatusCanonicalCode.UNAVAILABLE), + ): + with self.subTest(expected_status=expected_status): + + async def do_request(url): + async with aiohttp.ClientSession( + trace_configs=trace_configs + ) as session: + async with session.get(url): + pass + + loop = asyncio.get_event_loop() + with self.assertRaises(aiohttp.ClientConnectorError): + loop.run_until_complete(do_request(url)) + + self.assert_spans( + [ + ( + "GET", + (expected_status, None), + { + "component": "http", + "http.method": "GET", + "http.url": url, + }, + ) + ] + ) + self.memory_exporter.clear() + + def test_timeout(self): + async def request_handler(unused_request): + await asyncio.sleep(1) + return aiohttp.web.Response() + + host, port = self._http_request( + trace_config=opentelemetry.ext.aiohttp_client.create_trace_config(), + url="/test_timeout", + request_handler=request_handler, + timeout=aiohttp.ClientTimeout(sock_read=0.01), + ) + + self.assert_spans( + [ + ( + "GET", + (StatusCanonicalCode.DEADLINE_EXCEEDED, None), + { + "component": "http", + "http.method": "GET", + "http.url": "http://{}:{}/test_timeout".format( + host, port + ), + }, + ) + ] + ) + + def test_too_many_redirects(self): + async def request_handler(request): + # Create a redirect loop. + location = request.url + raise aiohttp.web.HTTPFound(location=location) + + host, port = self._http_request( + trace_config=opentelemetry.ext.aiohttp_client.create_trace_config(), + url="/test_too_many_redirects", + request_handler=request_handler, + max_redirects=2, + ) + + self.assert_spans( + [ + ( + "GET", + (StatusCanonicalCode.DEADLINE_EXCEEDED, None), + { + "component": "http", + "http.method": "GET", + "http.url": "http://{}:{}/test_too_many_redirects".format( + host, port + ), + }, + ) + ] + ) diff --git a/ext/opentelemetry-ext-docker-tests/tests/docker-compose.yml b/ext/opentelemetry-ext-docker-tests/tests/docker-compose.yml index 3e642c584e5..f996db077e8 100644 --- a/ext/opentelemetry-ext-docker-tests/tests/docker-compose.yml +++ b/ext/opentelemetry-ext-docker-tests/tests/docker-compose.yml @@ -3,18 +3,18 @@ version: '3' services: otmongo: ports: - - "27017:27017" + - "27017:27017" image: mongo:latest otmysql: - ports: - - "3306:3306" - image: mysql:latest - restart: always - environment: - MYSQL_USER: testuser - MYSQL_PASSWORD: testpassword - MYSQL_ALLOW_EMPTY_PASSWORD: "yes" - MYSQL_DATABASE: opentelemetry-tests + ports: + - "3306:3306" + image: mysql:latest + restart: always + environment: + MYSQL_USER: testuser + MYSQL_PASSWORD: testpassword + MYSQL_ALLOW_EMPTY_PASSWORD: "yes" + MYSQL_DATABASE: opentelemetry-tests otpostgres: image: postgres ports: @@ -26,5 +26,16 @@ services: otredis: image: redis:4.0-alpine ports: - - "127.0.0.1:6379:6379" - + - "127.0.0.1:6379:6379" + otjaeger: + image: jaegertracing/all-in-one:1.8 + environment: + COLLECTOR_ZIPKIN_HTTP_PORT: "9411" + ports: + - "5775:5775/udp" + - "6831:6831/udp" + - "6832:6832/udp" + - "5778:5778" + - "16686:16686" + - "14268:14268" + - "9411:9411" diff --git a/scripts/coverage.sh b/scripts/coverage.sh index 839380c27fd..1ff42d9e539 100755 --- a/scripts/coverage.sh +++ b/scripts/coverage.sh @@ -12,6 +12,8 @@ function cov { ${1} } +PYTHON_VERSION=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:3])))') +PYTHON_VERSION_INFO=(${PYTHON_VERSION//./ }) coverage erase @@ -25,5 +27,10 @@ cov ext/opentelemetry-ext-wsgi cov ext/opentelemetry-ext-zipkin cov docs/examples/opentelemetry-example-app -coverage report +# aiohttp is only supported on Python 3.5+. +if [ ${PYTHON_VERSION_INFO[1]} -gt 4 ]; then + cov ext/opentelemetry-ext-aiohttp-client +fi + +coverage report --show-missing coverage xml diff --git a/tox.ini b/tox.ini index 84dd157dbaa..bb8f7858612 100644 --- a/tox.ini +++ b/tox.ini @@ -28,9 +28,14 @@ envlist = py3{4,5,6,7,8}-test-example-http pypy3-test-example-http + ; opentelemetry-ext-aiohttp-client + py3{5,6,7,8}-test-ext-aiohttp-client + pypy3-test-ext-aiohttp-client + + ; opentelemetry-ext-django py3{6,7,8}-test-ext-django pypy3-test-ext-django - + ; opentelemetry-ext-dbapi py3{4,5,6,7,8}-test-ext-dbapi pypy3-test-ext-dbapi @@ -127,6 +132,7 @@ changedir = test-sdk: opentelemetry-sdk/tests test-auto-instrumentation: opentelemetry-auto-instrumentation/tests test-ext-grpc: ext/opentelemetry-ext-grpc/tests + test-ext-aiohttp-client: ext/opentelemetry-ext-aiohttp-client/tests test-ext-requests: ext/opentelemetry-ext-requests/tests test-ext-jaeger: ext/opentelemetry-ext-jaeger/tests test-ext-dbapi: ext/opentelemetry-ext-dbapi/tests @@ -204,6 +210,9 @@ commands_pre = requests: pip install {toxinidir}/opentelemetry-auto-instrumentation requests: pip install {toxinidir}/ext/opentelemetry-ext-requests[test] + aiohttp-client: pip install {toxinidir}/opentelemetry-sdk + aiohttp-client: pip install {toxinidir}/ext/opentelemetry-ext-aiohttp-client + jaeger: pip install {toxinidir}/ext/opentelemetry-ext-jaeger opentracing-shim: pip install {toxinidir}/ext/opentelemetry-ext-opentracing-shim