From c74c6d346e933478c3a85a24194261a3daf41c01 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 3 Aug 2020 10:10:45 -0700 Subject: [PATCH 01/17] Rename web framework packages from "ext" to "instrumentation" (#961) --- .../CHANGELOG.md | 35 ++ .../LICENSE | 201 ++++++++++++ .../MANIFEST.in | 9 + .../README.rst | 23 ++ .../setup.cfg | 57 ++++ .../setup.py | 31 ++ .../instrumentation/requests/__init__.py | 189 +++++++++++ .../instrumentation/requests/version.py | 15 + .../tests/__init__.py | 0 .../tests/test_requests_integration.py | 307 ++++++++++++++++++ 10 files changed, 867 insertions(+) create mode 100644 instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md create mode 100644 instrumentation/opentelemetry-instrumentation-requests/LICENSE create mode 100644 instrumentation/opentelemetry-instrumentation-requests/MANIFEST.in create mode 100644 instrumentation/opentelemetry-instrumentation-requests/README.rst create mode 100644 instrumentation/opentelemetry-instrumentation-requests/setup.cfg create mode 100644 instrumentation/opentelemetry-instrumentation-requests/setup.py create mode 100644 instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py create mode 100644 instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py create mode 100644 instrumentation/opentelemetry-instrumentation-requests/tests/__init__.py create mode 100644 instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py diff --git a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md new file mode 100644 index 000000000..dda179d91 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md @@ -0,0 +1,35 @@ +# Changelog + +## Unreleased + +- Change package name to opentelemetry-instrumentation-requests + ([#961](https://github.com/open-telemetry/opentelemetry-python/pull/961)) + +## 0.7b1 + +Released 2020-05-12 + +- Rename package to opentelemetry-ext-requests + ([#619](https://github.com/open-telemetry/opentelemetry-python/pull/619)) +- Implement instrumentor interface, enabling auto-instrumentation + ([#597](https://github.com/open-telemetry/opentelemetry-python/pull/597)) +- Adding disable_session for more granular instrumentation control + ([#573](https://github.com/open-telemetry/opentelemetry-python/pull/573)) +- Add a callback for custom attributes + ([#656](https://github.com/open-telemetry/opentelemetry-python/pull/656)) + +## 0.3a0 + +Released 2019-10-29 + +## 0.2a0 + +Released 2019-10-29 + +- Updates for core library changes + +## 0.1a0 + +Released 2019-09-30 + +- Initial release diff --git a/instrumentation/opentelemetry-instrumentation-requests/LICENSE b/instrumentation/opentelemetry-instrumentation-requests/LICENSE new file mode 100644 index 000000000..261eeb9e9 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-requests/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/instrumentation/opentelemetry-instrumentation-requests/MANIFEST.in b/instrumentation/opentelemetry-instrumentation-requests/MANIFEST.in new file mode 100644 index 000000000..aed3e3327 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-requests/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/instrumentation/opentelemetry-instrumentation-requests/README.rst b/instrumentation/opentelemetry-instrumentation-requests/README.rst new file mode 100644 index 000000000..d4944d352 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-requests/README.rst @@ -0,0 +1,23 @@ +OpenTelemetry Requests Instrumentation +====================================== + +|pypi| + +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-requests.svg + :target: https://pypi.org/project/opentelemetry-instrumentation-requests/ + +This library allows tracing HTTP requests made by the +`requests `_ library. + +Installation +------------ + +:: + + pip install opentelemetry-instrumentation-requests + +References +---------- + +* `OpenTelemetry requests Instrumentation `_ +* `OpenTelemetry Project `_ diff --git a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg new file mode 100644 index 000000000..77205b581 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg @@ -0,0 +1,57 @@ +# Copyright The 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-instrumentation-requests +description = OpenTelemetry requests instrumentation +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/tree/master/instrumentation/opentelemetry-instrumentation-requests +platforms = any +license = Apache-2.0 +classifiers = + Development Status :: 4 - Beta + Intended Audience :: Developers + License :: OSI Approved :: Apache Software License + Programming Language :: Python + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.4 + Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + +[options] +python_requires = >=3.4 +package_dir= + =src +packages=find_namespace: +install_requires = + opentelemetry-api == 0.12.dev0 + opentelemetry-instrumentation == 0.12.dev0 + requests ~= 2.0 + +[options.extras_require] +test = + opentelemetry-test == 0.12.dev0 + httpretty ~= 1.0 + +[options.packages.find] +where = src + +[options.entry_points] +opentelemetry_instrumentor = + requests = opentelemetry.instrumentation.requests:RequestsInstrumentor diff --git a/instrumentation/opentelemetry-instrumentation-requests/setup.py b/instrumentation/opentelemetry-instrumentation-requests/setup.py new file mode 100644 index 000000000..237fef583 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-requests/setup.py @@ -0,0 +1,31 @@ +# Copyright The 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", + "instrumentation", + "requests", + "version.py", +) +PACKAGE_INFO = {} +with open(VERSION_FILENAME) as f: + exec(f.read(), PACKAGE_INFO) + +setuptools.setup(version=PACKAGE_INFO["__version__"]) diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py new file mode 100644 index 000000000..a12f05999 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -0,0 +1,189 @@ +# Copyright The 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. + +""" +This library allows tracing HTTP requests made by the +`requests `_ library. + +Usage +----- + +.. code-block:: python + + import requests + import opentelemetry.instrumentation.requests + + # You can optionally pass a custom TracerProvider to + RequestInstrumentor.instrument() + opentelemetry.instrumentation.requests.RequestsInstrumentor().instrument() + response = requests.get(url="https://www.example.org/") + +Limitations +----------- + +Note that calls that do not use the higher-level APIs but use +:code:`requests.sessions.Session.send` (or an alias thereof) directly, are +currently not traced. If you find any other way to trigger an untraced HTTP +request, please report it via a GitHub issue with :code:`[requests: untraced +API]` in the title. + +API +--- +""" + +import functools +import types +from urllib.parse import urlparse + +from requests import Timeout, URLRequired +from requests.exceptions import InvalidSchema, InvalidURL, MissingSchema +from requests.sessions import Session + +from opentelemetry import context, propagators +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor +from opentelemetry.instrumentation.requests.version import __version__ +from opentelemetry.instrumentation.utils import http_status_to_canonical_code +from opentelemetry.trace import SpanKind, get_tracer +from opentelemetry.trace.status import Status, StatusCanonicalCode + + +# pylint: disable=unused-argument +def _instrument(tracer_provider=None, span_callback=None): + """Enables tracing of all requests calls that go through + :code:`requests.session.Session.request` (this includes + :code:`requests.get`, etc.).""" + + # Since + # https://github.com/psf/requests/commit/d72d1162142d1bf8b1b5711c664fbbd674f349d1 + # (v0.7.0, Oct 23, 2011), get, post, etc are implemented via request which + # again, is implemented via Session.request (`Session` was named `session` + # before v1.0.0, Dec 17, 2012, see + # https://github.com/psf/requests/commit/4e5c4a6ab7bb0195dececdd19bb8505b872fe120) + + wrapped = Session.request + + @functools.wraps(wrapped) + def instrumented_request(self, method, url, *args, **kwargs): + if context.get_value("suppress_instrumentation"): + return wrapped(self, method, url, *args, **kwargs) + + # See + # https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md#http-client + try: + parsed_url = urlparse(url) + span_name = parsed_url.path + except ValueError as exc: # Invalid URL + span_name = "".format(exc) + + exception = None + + with get_tracer( + __name__, __version__, tracer_provider + ).start_as_current_span(span_name, kind=SpanKind.CLIENT) as span: + span.set_attribute("component", "http") + span.set_attribute("http.method", method.upper()) + span.set_attribute("http.url", url) + + headers = kwargs.get("headers", {}) or {} + propagators.inject(type(headers).__setitem__, headers) + kwargs["headers"] = headers + + try: + result = wrapped( + self, method, url, *args, **kwargs + ) # *** PROCEED + except Exception as exc: # pylint: disable=W0703 + exception = exc + result = getattr(exc, "response", None) + + if exception is not None: + span.set_status( + Status(_exception_to_canonical_code(exception)) + ) + + if result is not None: + span.set_attribute("http.status_code", result.status_code) + span.set_attribute("http.status_text", result.reason) + span.set_status( + Status(http_status_to_canonical_code(result.status_code)) + ) + + if span_callback is not None: + span_callback(span, result) + + if exception is not None: + raise exception.with_traceback(exception.__traceback__) + + return result + + instrumented_request.opentelemetry_ext_requests_applied = True + + Session.request = instrumented_request + + # TODO: We should also instrument requests.sessions.Session.send + # but to avoid doubled spans, we would need some context-local + # state (i.e., only create a Span if the current context's URL is + # different, then push the current URL, pop it afterwards) + + +def _uninstrument(): + # pylint: disable=global-statement + """Disables instrumentation of :code:`requests` through this module. + + Note that this only works if no other module also patches requests.""" + if getattr(Session.request, "opentelemetry_ext_requests_applied", False): + original = Session.request.__wrapped__ # pylint:disable=no-member + Session.request = original + + +def _exception_to_canonical_code(exc: Exception) -> StatusCanonicalCode: + if isinstance( + exc, + (InvalidURL, InvalidSchema, MissingSchema, URLRequired, ValueError), + ): + return StatusCanonicalCode.INVALID_ARGUMENT + if isinstance(exc, Timeout): + return StatusCanonicalCode.DEADLINE_EXCEEDED + return StatusCanonicalCode.UNKNOWN + + +class RequestsInstrumentor(BaseInstrumentor): + """An instrumentor for requests + See `BaseInstrumentor` + """ + + def _instrument(self, **kwargs): + """Instruments requests module + + Args: + **kwargs: Optional arguments + ``tracer_provider``: a TracerProvider, defaults to global + ``span_callback``: An optional callback invoked before returning the http response. Invoked with Span and requests.Response + """ + _instrument( + tracer_provider=kwargs.get("tracer_provider"), + span_callback=kwargs.get("span_callback"), + ) + + def _uninstrument(self, **kwargs): + _uninstrument() + + @staticmethod + def uninstrument_session(session): + """Disables instrumentation on the session object.""" + if getattr( + session.request, "opentelemetry_ext_requests_applied", False + ): + original = session.request.__wrapped__ # pylint:disable=no-member + session.request = types.MethodType(original, session) diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py new file mode 100644 index 000000000..780a92b6a --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py @@ -0,0 +1,15 @@ +# Copyright The 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.12.dev0" diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py new file mode 100644 index 000000000..afec0a88d --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py @@ -0,0 +1,307 @@ +# Copyright The 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. + +from unittest import mock + +import httpretty +import requests + +import opentelemetry.instrumentation.requests +from opentelemetry import context, propagators, trace +from opentelemetry.instrumentation.requests import RequestsInstrumentor +from opentelemetry.sdk import resources +from opentelemetry.test.mock_httptextformat import MockHTTPTextFormat +from opentelemetry.test.test_base import TestBase +from opentelemetry.trace.status import StatusCanonicalCode + + +class TestRequestsIntegration(TestBase): + URL = "http://httpbin.org/status/200" + + def setUp(self): + super().setUp() + RequestsInstrumentor().instrument() + httpretty.enable() + httpretty.register_uri( + httpretty.GET, self.URL, body="Hello!", + ) + + def tearDown(self): + super().tearDown() + RequestsInstrumentor().uninstrument() + httpretty.disable() + + def test_basic(self): + result = requests.get(self.URL) + self.assertEqual(result.text, "Hello!") + + span_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(span_list), 1) + span = span_list[0] + + self.assertIs(span.kind, trace.SpanKind.CLIENT) + self.assertEqual(span.name, "/status/200") + + self.assertEqual( + span.attributes, + { + "component": "http", + "http.method": "GET", + "http.url": self.URL, + "http.status_code": 200, + "http.status_text": "OK", + }, + ) + + self.assertIs( + span.status.canonical_code, trace.status.StatusCanonicalCode.OK + ) + + self.check_span_instrumentation_info( + span, opentelemetry.instrumentation.requests + ) + + def test_not_foundbasic(self): + url_404 = "http://httpbin.org/status/404" + httpretty.register_uri( + httpretty.GET, url_404, status=404, + ) + result = requests.get(url_404) + self.assertEqual(result.status_code, 404) + + span_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(span_list), 1) + span = span_list[0] + + self.assertEqual(span.attributes.get("http.status_code"), 404) + self.assertEqual(span.attributes.get("http.status_text"), "Not Found") + + self.assertIs( + span.status.canonical_code, + trace.status.StatusCanonicalCode.NOT_FOUND, + ) + + def test_invalid_url(self): + url = "http://[::1/nope" + + with self.assertRaises(ValueError): + requests.post(url) + + span_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(span_list), 1) + span = span_list[0] + + self.assertTrue(span.name.startswith(" Date: Sat, 15 Aug 2020 18:06:27 -0700 Subject: [PATCH 02/17] chore: 0.13.dev0 version update (#991) --- .../opentelemetry-instrumentation-requests/CHANGELOG.md | 4 ++++ .../opentelemetry-instrumentation-requests/setup.cfg | 6 +++--- .../src/opentelemetry/instrumentation/requests/version.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md index dda179d91..179f0f687 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +## Version 0.12b0 + +Released 2020-08-14 + - Change package name to opentelemetry-instrumentation-requests ([#961](https://github.com/open-telemetry/opentelemetry-python/pull/961)) diff --git a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg index 77205b581..ece501317 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg +++ b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg @@ -40,13 +40,13 @@ package_dir= =src packages=find_namespace: install_requires = - opentelemetry-api == 0.12.dev0 - opentelemetry-instrumentation == 0.12.dev0 + opentelemetry-api == 0.13dev0 + opentelemetry-instrumentation == 0.13dev0 requests ~= 2.0 [options.extras_require] test = - opentelemetry-test == 0.12.dev0 + opentelemetry-test == 0.13dev0 httpretty ~= 1.0 [options.packages.find] diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py index 780a92b6a..9cc445d09 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.12.dev0" +__version__ = "0.13dev0" From 8d061a079ff4bb950286ef40551613170b952009 Mon Sep 17 00:00:00 2001 From: alrex Date: Mon, 17 Aug 2020 23:50:09 -0500 Subject: [PATCH 03/17] Span name updated to follow semantic conventions to reduce cardinality (#972) --- .../opentelemetry-instrumentation-requests/CHANGELOG.md | 2 ++ .../src/opentelemetry/instrumentation/requests/__init__.py | 7 ++----- .../tests/test_requests_integration.py | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md index 179f0f687..3f18f6101 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md @@ -8,6 +8,8 @@ Released 2020-08-14 - Change package name to opentelemetry-instrumentation-requests ([#961](https://github.com/open-telemetry/opentelemetry-python/pull/961)) +- Span name reported updated to follow semantic conventions to reduce + cardinality ([#972](https://github.com/open-telemetry/opentelemetry-python/pull/972)) ## 0.7b1 diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index a12f05999..e2c54b7f1 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -80,11 +80,7 @@ def _instrument(tracer_provider=None, span_callback=None): # See # https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md#http-client - try: - parsed_url = urlparse(url) - span_name = parsed_url.path - except ValueError as exc: # Invalid URL - span_name = "".format(exc) + span_name = "HTTP {}".format(method) exception = None @@ -111,6 +107,7 @@ def _instrument(tracer_provider=None, span_callback=None): span.set_status( Status(_exception_to_canonical_code(exception)) ) + span.record_exception(exception) if result is not None: span.set_attribute("http.status_code", result.status_code) diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py index afec0a88d..da09118e5 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py +++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py @@ -51,7 +51,7 @@ class TestRequestsIntegration(TestBase): span = span_list[0] self.assertIs(span.kind, trace.SpanKind.CLIENT) - self.assertEqual(span.name, "/status/200") + self.assertEqual(span.name, "HTTP get") self.assertEqual( span.attributes, @@ -102,7 +102,7 @@ class TestRequestsIntegration(TestBase): self.assertEqual(len(span_list), 1) span = span_list[0] - self.assertTrue(span.name.startswith(" Date: Wed, 2 Sep 2020 07:42:01 +0200 Subject: [PATCH 04/17] RequestsInstrumentor: Add support for prepared requests (#1040) in addition to Session.request instrument Session.send to also create spans for prepared requests. --- .../CHANGELOG.md | 3 + .../instrumentation/requests/__init__.py | 110 +++++++--- .../tests/test_requests_integration.py | 198 ++++++++++-------- 3 files changed, 185 insertions(+), 126 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md index 3f18f6101..eeb2e8372 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Add support for instrumenting prepared requests + ([#1040](https://github.com/open-telemetry/opentelemetry-python/pull/1040)) + ## Version 0.12b0 Released 2020-08-14 diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index e2c54b7f1..16e8952fe 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -29,26 +29,17 @@ Usage opentelemetry.instrumentation.requests.RequestsInstrumentor().instrument() response = requests.get(url="https://www.example.org/") -Limitations ------------ - -Note that calls that do not use the higher-level APIs but use -:code:`requests.sessions.Session.send` (or an alias thereof) directly, are -currently not traced. If you find any other way to trigger an untraced HTTP -request, please report it via a GitHub issue with :code:`[requests: untraced -API]` in the title. - API --- """ import functools import types -from urllib.parse import urlparse from requests import Timeout, URLRequired from requests.exceptions import InvalidSchema, InvalidURL, MissingSchema from requests.sessions import Session +from requests.structures import CaseInsensitiveDict from opentelemetry import context, propagators from opentelemetry.instrumentation.instrumentor import BaseInstrumentor @@ -57,6 +48,10 @@ from opentelemetry.instrumentation.utils import http_status_to_canonical_code from opentelemetry.trace import SpanKind, get_tracer from opentelemetry.trace.status import Status, StatusCanonicalCode +# A key to a context variable to avoid creating duplicate spans when instrumenting +# both, Session.request and Session.send, since Session.request calls into Session.send +_SUPPRESS_REQUESTS_INSTRUMENTATION_KEY = "suppress_requests_instrumentation" + # pylint: disable=unused-argument def _instrument(tracer_provider=None, span_callback=None): @@ -71,15 +66,54 @@ def _instrument(tracer_provider=None, span_callback=None): # before v1.0.0, Dec 17, 2012, see # https://github.com/psf/requests/commit/4e5c4a6ab7bb0195dececdd19bb8505b872fe120) - wrapped = Session.request + wrapped_request = Session.request + wrapped_send = Session.send - @functools.wraps(wrapped) + @functools.wraps(wrapped_request) def instrumented_request(self, method, url, *args, **kwargs): - if context.get_value("suppress_instrumentation"): - return wrapped(self, method, url, *args, **kwargs) + def get_or_create_headers(): + headers = kwargs.get("headers") + if headers is None: + headers = {} + kwargs["headers"] = headers + + return headers + + def call_wrapped(): + return wrapped_request(self, method, url, *args, **kwargs) + + return _instrumented_requests_call( + method, url, call_wrapped, get_or_create_headers + ) + + @functools.wraps(wrapped_send) + def instrumented_send(self, request, **kwargs): + def get_or_create_headers(): + request.headers = ( + request.headers + if request.headers is not None + else CaseInsensitiveDict() + ) + return request.headers + + def call_wrapped(): + return wrapped_send(self, request, **kwargs) + + return _instrumented_requests_call( + request.method, request.url, call_wrapped, get_or_create_headers + ) + + def _instrumented_requests_call( + method: str, url: str, call_wrapped, get_or_create_headers + ): + if context.get_value("suppress_instrumentation") or context.get_value( + _SUPPRESS_REQUESTS_INSTRUMENTATION_KEY + ): + return call_wrapped() # See # https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md#http-client + method = method.upper() span_name = "HTTP {}".format(method) exception = None @@ -91,17 +125,19 @@ def _instrument(tracer_provider=None, span_callback=None): span.set_attribute("http.method", method.upper()) span.set_attribute("http.url", url) - headers = kwargs.get("headers", {}) or {} + headers = get_or_create_headers() propagators.inject(type(headers).__setitem__, headers) - kwargs["headers"] = headers + token = context.attach( + context.set_value(_SUPPRESS_REQUESTS_INSTRUMENTATION_KEY, True) + ) try: - result = wrapped( - self, method, url, *args, **kwargs - ) # *** PROCEED + result = call_wrapped() # *** PROCEED except Exception as exc: # pylint: disable=W0703 exception = exc result = getattr(exc, "response", None) + finally: + context.detach(token) if exception is not None: span.set_status( @@ -124,24 +160,34 @@ def _instrument(tracer_provider=None, span_callback=None): return result - instrumented_request.opentelemetry_ext_requests_applied = True - + instrumented_request.opentelemetry_instrumentation_requests_applied = True Session.request = instrumented_request - # TODO: We should also instrument requests.sessions.Session.send - # but to avoid doubled spans, we would need some context-local - # state (i.e., only create a Span if the current context's URL is - # different, then push the current URL, pop it afterwards) + instrumented_send.opentelemetry_instrumentation_requests_applied = True + Session.send = instrumented_send def _uninstrument(): - # pylint: disable=global-statement """Disables instrumentation of :code:`requests` through this module. Note that this only works if no other module also patches requests.""" - if getattr(Session.request, "opentelemetry_ext_requests_applied", False): - original = Session.request.__wrapped__ # pylint:disable=no-member - Session.request = original + _uninstrument_from(Session) + + +def _uninstrument_from(instr_root, restore_as_bound_func=False): + for instr_func_name in ("request", "send"): + instr_func = getattr(instr_root, instr_func_name) + if not getattr( + instr_func, + "opentelemetry_instrumentation_requests_applied", + False, + ): + continue + + original = instr_func.__wrapped__ # pylint:disable=no-member + if restore_as_bound_func: + original = types.MethodType(original, instr_root) + setattr(instr_root, instr_func_name, original) def _exception_to_canonical_code(exc: Exception) -> StatusCanonicalCode: @@ -179,8 +225,4 @@ class RequestsInstrumentor(BaseInstrumentor): @staticmethod def uninstrument_session(session): """Disables instrumentation on the session object.""" - if getattr( - session.request, "opentelemetry_ext_requests_applied", False - ): - original = session.request.__wrapped__ # pylint:disable=no-member - session.request = types.MethodType(original, session) + _uninstrument_from(session, restore_as_bound_func=True) diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py index da09118e5..0e0492f47 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py +++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import abc from unittest import mock import httpretty @@ -26,32 +27,47 @@ from opentelemetry.test.test_base import TestBase from opentelemetry.trace.status import StatusCanonicalCode -class TestRequestsIntegration(TestBase): +class RequestsIntegrationTestBase(abc.ABC): + # pylint: disable=no-member + URL = "http://httpbin.org/status/200" + # pylint: disable=invalid-name def setUp(self): super().setUp() RequestsInstrumentor().instrument() httpretty.enable() - httpretty.register_uri( - httpretty.GET, self.URL, body="Hello!", - ) + httpretty.register_uri(httpretty.GET, self.URL, body="Hello!") + # pylint: disable=invalid-name def tearDown(self): super().tearDown() RequestsInstrumentor().uninstrument() httpretty.disable() - def test_basic(self): - result = requests.get(self.URL) - self.assertEqual(result.text, "Hello!") + def assert_span(self, exporter=None, num_spans=1): + if exporter is None: + exporter = self.memory_exporter + span_list = exporter.get_finished_spans() + self.assertEqual(num_spans, len(span_list)) + if num_spans == 0: + return None + if num_spans == 1: + return span_list[0] + return span_list - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) - span = span_list[0] + @staticmethod + @abc.abstractmethod + def perform_request(url: str, session: requests.Session = None): + pass + + def test_basic(self): + result = self.perform_request(self.URL) + self.assertEqual(result.text, "Hello!") + span = self.assert_span() self.assertIs(span.kind, trace.SpanKind.CLIENT) - self.assertEqual(span.name, "HTTP get") + self.assertEqual(span.name, "HTTP GET") self.assertEqual( span.attributes, @@ -77,12 +93,10 @@ class TestRequestsIntegration(TestBase): httpretty.register_uri( httpretty.GET, url_404, status=404, ) - result = requests.get(url_404) + result = self.perform_request(url_404) self.assertEqual(result.status_code, 404) - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) - span = span_list[0] + span = self.assert_span() self.assertEqual(span.attributes.get("http.status_code"), 404) self.assertEqual(span.attributes.get("http.status_text"), "Not Found") @@ -92,31 +106,11 @@ class TestRequestsIntegration(TestBase): trace.status.StatusCanonicalCode.NOT_FOUND, ) - def test_invalid_url(self): - url = "http://[::1/nope" - - with self.assertRaises(ValueError): - requests.post(url) - - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) - span = span_list[0] - - self.assertEqual(span.name, "HTTP post") - self.assertEqual( - span.attributes, - {"component": "http", "http.method": "POST", "http.url": url}, - ) - self.assertEqual( - span.status.canonical_code, StatusCanonicalCode.INVALID_ARGUMENT - ) - def test_uninstrument(self): RequestsInstrumentor().uninstrument() - result = requests.get(self.URL) + result = self.perform_request(self.URL) self.assertEqual(result.text, "Hello!") - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 0) + self.assert_span(num_spans=0) # instrument again to avoid annoying warning message RequestsInstrumentor().instrument() @@ -124,49 +118,43 @@ class TestRequestsIntegration(TestBase): session1 = requests.Session() RequestsInstrumentor().uninstrument_session(session1) - result = session1.get(self.URL) + result = self.perform_request(self.URL, session1) self.assertEqual(result.text, "Hello!") - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 0) + self.assert_span(num_spans=0) # Test that other sessions as well as global requests is still # instrumented session2 = requests.Session() - result = session2.get(self.URL) + result = self.perform_request(self.URL, session2) self.assertEqual(result.text, "Hello!") - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) + self.assert_span() self.memory_exporter.clear() - result = requests.get(self.URL) + result = self.perform_request(self.URL) self.assertEqual(result.text, "Hello!") - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) + self.assert_span() def test_suppress_instrumentation(self): token = context.attach( context.set_value("suppress_instrumentation", True) ) try: - result = requests.get(self.URL) + result = self.perform_request(self.URL) self.assertEqual(result.text, "Hello!") finally: context.detach(token) - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 0) + self.assert_span(num_spans=0) def test_distributed_context(self): previous_propagator = propagators.get_global_httptextformat() try: propagators.set_global_httptextformat(MockHTTPTextFormat()) - result = requests.get(self.URL) + result = self.perform_request(self.URL) self.assertEqual(result.text, "Hello!") - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) - span = span_list[0] + span = self.assert_span() headers = dict(httpretty.last_request().headers) self.assertIn(MockHTTPTextFormat.TRACE_ID_KEY, headers) @@ -195,13 +183,10 @@ class TestRequestsIntegration(TestBase): tracer_provider=self.tracer_provider, span_callback=span_callback, ) - result = requests.get(self.URL) + result = self.perform_request(self.URL) self.assertEqual(result.text, "Hello!") - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) - span = span_list[0] - + span = self.assert_span() self.assertEqual( span.attributes, { @@ -221,28 +206,21 @@ class TestRequestsIntegration(TestBase): RequestsInstrumentor().uninstrument() RequestsInstrumentor().instrument(tracer_provider=tracer_provider) - result = requests.get(self.URL) + result = self.perform_request(self.URL) self.assertEqual(result.text, "Hello!") - span_list = exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) - span = span_list[0] - + span = self.assert_span(exporter=exporter) self.assertIs(span.resource, resource) - def test_if_headers_equals_none(self): - result = requests.get(self.URL, headers=None) - self.assertEqual(result.text, "Hello!") - - @mock.patch("requests.Session.send", side_effect=requests.RequestException) + @mock.patch( + "requests.adapters.HTTPAdapter.send", + side_effect=requests.RequestException, + ) def test_requests_exception_without_response(self, *_, **__): - with self.assertRaises(requests.RequestException): - requests.get(self.URL) + self.perform_request(self.URL) - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) - span = span_list[0] + span = self.assert_span() self.assertEqual( span.attributes, {"component": "http", "http.method": "GET", "http.url": self.URL}, @@ -256,17 +234,14 @@ class TestRequestsIntegration(TestBase): mocked_response.reason = "Internal Server Error" @mock.patch( - "requests.Session.send", + "requests.adapters.HTTPAdapter.send", side_effect=requests.RequestException(response=mocked_response), ) def test_requests_exception_with_response(self, *_, **__): - with self.assertRaises(requests.RequestException): - requests.get(self.URL) + self.perform_request(self.URL) - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) - span = span_list[0] + span = self.assert_span() self.assertEqual( span.attributes, { @@ -281,27 +256,66 @@ class TestRequestsIntegration(TestBase): span.status.canonical_code, StatusCanonicalCode.INTERNAL ) - @mock.patch("requests.Session.send", side_effect=Exception) + @mock.patch("requests.adapters.HTTPAdapter.send", side_effect=Exception) def test_requests_basic_exception(self, *_, **__): - with self.assertRaises(Exception): - requests.get(self.URL) + self.perform_request(self.URL) - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) + span = self.assert_span() self.assertEqual( - span_list[0].status.canonical_code, StatusCanonicalCode.UNKNOWN + span.status.canonical_code, StatusCanonicalCode.UNKNOWN ) - @mock.patch("requests.Session.send", side_effect=requests.Timeout) + @mock.patch( + "requests.adapters.HTTPAdapter.send", side_effect=requests.Timeout + ) def test_requests_timeout_exception(self, *_, **__): - with self.assertRaises(Exception): - requests.get(self.URL) + self.perform_request(self.URL) - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) + span = self.assert_span() self.assertEqual( - span_list[0].status.canonical_code, - StatusCanonicalCode.DEADLINE_EXCEEDED, + span.status.canonical_code, StatusCanonicalCode.DEADLINE_EXCEEDED ) + + +class TestRequestsIntegration(RequestsIntegrationTestBase, TestBase): + @staticmethod + def perform_request(url: str, session: requests.Session = None): + if session is None: + return requests.get(url) + return session.get(url) + + def test_invalid_url(self): + url = "http://[::1/nope" + + with self.assertRaises(ValueError): + requests.post(url) + + span = self.assert_span() + + self.assertEqual(span.name, "HTTP POST") + self.assertEqual( + span.attributes, + {"component": "http", "http.method": "POST", "http.url": url}, + ) + self.assertEqual( + span.status.canonical_code, StatusCanonicalCode.INVALID_ARGUMENT + ) + + def test_if_headers_equals_none(self): + result = requests.get(self.URL, headers=None) + self.assertEqual(result.text, "Hello!") + self.assert_span() + + +class TestRequestsIntegrationPreparedRequest( + RequestsIntegrationTestBase, TestBase +): + @staticmethod + def perform_request(url: str, session: requests.Session = None): + if session is None: + session = requests.Session() + request = requests.Request("GET", url) + prepared_request = session.prepare_request(request) + return session.send(prepared_request) From 2d5adef109028a9381c3e70610c70f7080331473 Mon Sep 17 00:00:00 2001 From: alrex Date: Wed, 9 Sep 2020 13:23:56 -0700 Subject: [PATCH 05/17] Rename HTTPTextFormat to TextMapPropagator (#1085) --- .../tests/test_requests_integration.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py index 0e0492f47..41f5bc39d 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py +++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py @@ -22,7 +22,7 @@ import opentelemetry.instrumentation.requests from opentelemetry import context, propagators, trace from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.sdk import resources -from opentelemetry.test.mock_httptextformat import MockHTTPTextFormat +from opentelemetry.test.mock_textmap import MockTextMapPropagator from opentelemetry.test.test_base import TestBase from opentelemetry.trace.status import StatusCanonicalCode @@ -148,28 +148,28 @@ class RequestsIntegrationTestBase(abc.ABC): self.assert_span(num_spans=0) def test_distributed_context(self): - previous_propagator = propagators.get_global_httptextformat() + previous_propagator = propagators.get_global_textmap() try: - propagators.set_global_httptextformat(MockHTTPTextFormat()) + propagators.set_global_textmap(MockTextMapPropagator()) result = self.perform_request(self.URL) self.assertEqual(result.text, "Hello!") span = self.assert_span() headers = dict(httpretty.last_request().headers) - self.assertIn(MockHTTPTextFormat.TRACE_ID_KEY, headers) + self.assertIn(MockTextMapPropagator.TRACE_ID_KEY, headers) self.assertEqual( str(span.get_context().trace_id), - headers[MockHTTPTextFormat.TRACE_ID_KEY], + headers[MockTextMapPropagator.TRACE_ID_KEY], ) - self.assertIn(MockHTTPTextFormat.SPAN_ID_KEY, headers) + self.assertIn(MockTextMapPropagator.SPAN_ID_KEY, headers) self.assertEqual( str(span.get_context().span_id), - headers[MockHTTPTextFormat.SPAN_ID_KEY], + headers[MockTextMapPropagator.SPAN_ID_KEY], ) finally: - propagators.set_global_httptextformat(previous_propagator) + propagators.set_global_textmap(previous_propagator) def test_span_callback(self): RequestsInstrumentor().uninstrument() From da202b05f688bd0d71a00b6a18890f68ec57d0d5 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 14 Sep 2020 13:13:44 -0700 Subject: [PATCH 06/17] Use is_recording flag in requests instrumentation (#1087) --- .../instrumentation/requests/__init__.py | 15 ++++++++------- .../tests/test_requests_integration.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index 16e8952fe..fef67c5d0 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -56,8 +56,8 @@ _SUPPRESS_REQUESTS_INSTRUMENTATION_KEY = "suppress_requests_instrumentation" # pylint: disable=unused-argument def _instrument(tracer_provider=None, span_callback=None): """Enables tracing of all requests calls that go through - :code:`requests.session.Session.request` (this includes - :code:`requests.get`, etc.).""" + :code:`requests.session.Session.request` (this includes + :code:`requests.get`, etc.).""" # Since # https://github.com/psf/requests/commit/d72d1162142d1bf8b1b5711c664fbbd674f349d1 @@ -121,9 +121,10 @@ def _instrument(tracer_provider=None, span_callback=None): with get_tracer( __name__, __version__, tracer_provider ).start_as_current_span(span_name, kind=SpanKind.CLIENT) as span: - span.set_attribute("component", "http") - span.set_attribute("http.method", method.upper()) - span.set_attribute("http.url", url) + if span.is_recording(): + span.set_attribute("component", "http") + span.set_attribute("http.method", method.upper()) + span.set_attribute("http.url", url) headers = get_or_create_headers() propagators.inject(type(headers).__setitem__, headers) @@ -139,13 +140,13 @@ def _instrument(tracer_provider=None, span_callback=None): finally: context.detach(token) - if exception is not None: + if exception is not None and span.is_recording(): span.set_status( Status(_exception_to_canonical_code(exception)) ) span.record_exception(exception) - if result is not None: + if result is not None and span.is_recording(): span.set_attribute("http.status_code", result.status_code) span.set_attribute("http.status_text", result.reason) span.set_status( diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py index 41f5bc39d..c3457b739 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py +++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py @@ -147,6 +147,23 @@ class RequestsIntegrationTestBase(abc.ABC): self.assert_span(num_spans=0) + def test_not_recording(self): + with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span: + RequestsInstrumentor().uninstrument() + # original_tracer_provider returns a default tracer provider, which + # in turn will return an INVALID_SPAN, which is always not recording + RequestsInstrumentor().instrument( + tracer_provider=self.original_tracer_provider + ) + mock_span.is_recording.return_value = False + result = self.perform_request(self.URL) + self.assertEqual(result.text, "Hello!") + self.assert_span(None, 0) + self.assertFalse(mock_span.is_recording()) + self.assertTrue(mock_span.is_recording.called) + self.assertFalse(mock_span.set_attribute.called) + self.assertFalse(mock_span.set_status.called) + def test_distributed_context(self): previous_propagator = propagators.get_global_textmap() try: From dd3d4bf3227700734db3e6274eefcf239d2fd1be Mon Sep 17 00:00:00 2001 From: alrex Date: Mon, 14 Sep 2020 15:11:56 -0700 Subject: [PATCH 07/17] dropping support for python 3.4 (#1099) * dropping support for python 3.4 --- .../opentelemetry-instrumentation-requests/CHANGELOG.md | 2 ++ .../opentelemetry-instrumentation-requests/setup.cfg | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md index eeb2e8372..d42fd8f82 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md @@ -4,6 +4,8 @@ - Add support for instrumenting prepared requests ([#1040](https://github.com/open-telemetry/opentelemetry-python/pull/1040)) +- Drop support for Python 3.4 + ([#1099](https://github.com/open-telemetry/opentelemetry-python/pull/1099)) ## Version 0.12b0 diff --git a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg index ece501317..fbfa848ab 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg +++ b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg @@ -28,14 +28,13 @@ classifiers = License :: OSI Approved :: Apache Software License Programming Language :: Python Programming Language :: Python :: 3 - Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 [options] -python_requires = >=3.4 +python_requires = >=3.5 package_dir= =src packages=find_namespace: From 854da842350df153e4e505290fa309ead41cf742 Mon Sep 17 00:00:00 2001 From: alrex Date: Thu, 17 Sep 2020 08:23:52 -0700 Subject: [PATCH 08/17] release: updating changelogs and version to 0.13b0 (#1129) * updating changelogs and version to 0.13b0 --- .../opentelemetry-instrumentation-requests/CHANGELOG.md | 4 ++++ .../opentelemetry-instrumentation-requests/setup.cfg | 6 +++--- .../src/opentelemetry/instrumentation/requests/version.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md index d42fd8f82..5b876bd4a 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +## Version 0.13b0 + +Released 2020-09-17 + - Add support for instrumenting prepared requests ([#1040](https://github.com/open-telemetry/opentelemetry-python/pull/1040)) - Drop support for Python 3.4 diff --git a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg index fbfa848ab..e0a9838a0 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg +++ b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg @@ -39,13 +39,13 @@ package_dir= =src packages=find_namespace: install_requires = - opentelemetry-api == 0.13dev0 - opentelemetry-instrumentation == 0.13dev0 + opentelemetry-api == 0.13b0 + opentelemetry-instrumentation == 0.13b0 requests ~= 2.0 [options.extras_require] test = - opentelemetry-test == 0.13dev0 + opentelemetry-test == 0.13b0 httpretty ~= 1.0 [options.packages.find] diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py index 9cc445d09..2015e87c7 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.13dev0" +__version__ = "0.13b0" From ce1820cd2a2d6264e9959f894b22768eb620f975 Mon Sep 17 00:00:00 2001 From: alrex Date: Thu, 17 Sep 2020 12:21:39 -0700 Subject: [PATCH 09/17] chore: bump dev version (#1131) --- .../opentelemetry-instrumentation-requests/setup.cfg | 6 +++--- .../src/opentelemetry/instrumentation/requests/version.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg index e0a9838a0..9020ae58a 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg +++ b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg @@ -39,13 +39,13 @@ package_dir= =src packages=find_namespace: install_requires = - opentelemetry-api == 0.13b0 - opentelemetry-instrumentation == 0.13b0 + opentelemetry-api == 0.14.dev0 + opentelemetry-instrumentation == 0.14.dev0 requests ~= 2.0 [options.extras_require] test = - opentelemetry-test == 0.13b0 + opentelemetry-test == 0.14.dev0 httpretty ~= 1.0 [options.packages.find] diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py index 2015e87c7..0f9902789 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.13b0" +__version__ = "0.14.dev0" From 8fee5d7ade2c69a58ed86cad95c2a731385692f6 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Fri, 25 Sep 2020 07:31:35 -0700 Subject: [PATCH 10/17] Adding metric collection as part of instrumentations - Requests (#1116) --- .../CHANGELOG.md | 2 + .../instrumentation/requests/__init__.py | 95 +++++++++++++------ .../tests/test_requests_integration.py | 57 +++++++++++ 3 files changed, 123 insertions(+), 31 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md index 5b876bd4a..c2e4dcdbb 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md @@ -10,6 +10,8 @@ Released 2020-09-17 ([#1040](https://github.com/open-telemetry/opentelemetry-python/pull/1040)) - Drop support for Python 3.4 ([#1099](https://github.com/open-telemetry/opentelemetry-python/pull/1099)) +- Add support for http metrics + ([#1116](https://github.com/open-telemetry/opentelemetry-python/pull/1116)) ## Version 0.12b0 diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index fef67c5d0..d0336184e 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -25,7 +25,7 @@ Usage import opentelemetry.instrumentation.requests # You can optionally pass a custom TracerProvider to - RequestInstrumentor.instrument() + # RequestInstrumentor.instrument() opentelemetry.instrumentation.requests.RequestsInstrumentor().instrument() response = requests.get(url="https://www.example.org/") @@ -43,6 +43,10 @@ from requests.structures import CaseInsensitiveDict from opentelemetry import context, propagators from opentelemetry.instrumentation.instrumentor import BaseInstrumentor +from opentelemetry.instrumentation.metric import ( + HTTPMetricRecorder, + MetricMixin, +) from opentelemetry.instrumentation.requests.version import __version__ from opentelemetry.instrumentation.utils import http_status_to_canonical_code from opentelemetry.trace import SpanKind, get_tracer @@ -54,6 +58,7 @@ _SUPPRESS_REQUESTS_INSTRUMENTATION_KEY = "suppress_requests_instrumentation" # pylint: disable=unused-argument +# pylint: disable=R0915 def _instrument(tracer_provider=None, span_callback=None): """Enables tracing of all requests calls that go through :code:`requests.session.Session.request` (this includes @@ -118,43 +123,66 @@ def _instrument(tracer_provider=None, span_callback=None): exception = None + recorder = RequestsInstrumentor().metric_recorder + + labels = {} + labels["http.method"] = method + labels["http.url"] = url + with get_tracer( __name__, __version__, tracer_provider ).start_as_current_span(span_name, kind=SpanKind.CLIENT) as span: - if span.is_recording(): - span.set_attribute("component", "http") - span.set_attribute("http.method", method.upper()) - span.set_attribute("http.url", url) + with recorder.record_duration(labels): + if span.is_recording(): + span.set_attribute("component", "http") + span.set_attribute("http.method", method) + span.set_attribute("http.url", url) - headers = get_or_create_headers() - propagators.inject(type(headers).__setitem__, headers) + headers = get_or_create_headers() + propagators.inject(type(headers).__setitem__, headers) - token = context.attach( - context.set_value(_SUPPRESS_REQUESTS_INSTRUMENTATION_KEY, True) - ) - try: - result = call_wrapped() # *** PROCEED - except Exception as exc: # pylint: disable=W0703 - exception = exc - result = getattr(exc, "response", None) - finally: - context.detach(token) - - if exception is not None and span.is_recording(): - span.set_status( - Status(_exception_to_canonical_code(exception)) + token = context.attach( + context.set_value( + _SUPPRESS_REQUESTS_INSTRUMENTATION_KEY, True + ) ) - span.record_exception(exception) + try: + result = call_wrapped() # *** PROCEED + except Exception as exc: # pylint: disable=W0703 + exception = exc + result = getattr(exc, "response", None) + finally: + context.detach(token) - if result is not None and span.is_recording(): - span.set_attribute("http.status_code", result.status_code) - span.set_attribute("http.status_text", result.reason) - span.set_status( - Status(http_status_to_canonical_code(result.status_code)) - ) + if exception is not None and span.is_recording(): + span.set_status( + Status(_exception_to_canonical_code(exception)) + ) + span.record_exception(exception) - if span_callback is not None: - span_callback(span, result) + if result is not None: + if span.is_recording(): + span.set_attribute( + "http.status_code", result.status_code + ) + span.set_attribute("http.status_text", result.reason) + span.set_status( + Status( + http_status_to_canonical_code( + result.status_code + ) + ) + ) + labels["http.status_code"] = str(result.status_code) + labels["http.status_text"] = result.reason + if result.raw and result.raw.version: + labels["http.flavor"] = ( + str(result.raw.version)[:1] + + "." + + str(result.raw.version)[:-1] + ) + if span_callback is not None: + span_callback(span, result) if exception is not None: raise exception.with_traceback(exception.__traceback__) @@ -202,7 +230,7 @@ def _exception_to_canonical_code(exc: Exception) -> StatusCanonicalCode: return StatusCanonicalCode.UNKNOWN -class RequestsInstrumentor(BaseInstrumentor): +class RequestsInstrumentor(BaseInstrumentor, MetricMixin): """An instrumentor for requests See `BaseInstrumentor` """ @@ -219,6 +247,11 @@ class RequestsInstrumentor(BaseInstrumentor): tracer_provider=kwargs.get("tracer_provider"), span_callback=kwargs.get("span_callback"), ) + self.init_metrics( + __name__, __version__, + ) + # pylint: disable=W0201 + self.metric_recorder = HTTPMetricRecorder(self.meter, SpanKind.CLIENT) def _uninstrument(self, **kwargs): _uninstrument() diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py index c3457b739..2d3636284 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py +++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py @@ -22,6 +22,7 @@ import opentelemetry.instrumentation.requests from opentelemetry import context, propagators, trace from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.sdk import resources +from opentelemetry.sdk.util import get_dict_as_key from opentelemetry.test.mock_textmap import MockTextMapPropagator from opentelemetry.test.test_base import TestBase from opentelemetry.trace.status import StatusCanonicalCode @@ -88,6 +89,27 @@ class RequestsIntegrationTestBase(abc.ABC): span, opentelemetry.instrumentation.requests ) + self.assertIsNotNone(RequestsInstrumentor().meter) + self.assertEqual(len(RequestsInstrumentor().meter.metrics), 1) + recorder = RequestsInstrumentor().meter.metrics.pop() + match_key = get_dict_as_key( + { + "http.flavor": "1.1", + "http.method": "GET", + "http.status_code": "200", + "http.status_text": "OK", + "http.url": "http://httpbin.org/status/200", + } + ) + for key in recorder.bound_instruments.keys(): + self.assertEqual(key, match_key) + # pylint: disable=protected-access + bound = recorder.bound_instruments.get(key) + for view_data in bound.view_datas: + self.assertEqual(view_data.labels, key) + self.assertEqual(view_data.aggregator.current.count, 1) + self.assertGreater(view_data.aggregator.current.sum, 0) + def test_not_foundbasic(self): url_404 = "http://httpbin.org/status/404" httpretty.register_uri( @@ -246,6 +268,23 @@ class RequestsIntegrationTestBase(abc.ABC): span.status.canonical_code, StatusCanonicalCode.UNKNOWN ) + self.assertIsNotNone(RequestsInstrumentor().meter) + self.assertEqual(len(RequestsInstrumentor().meter.metrics), 1) + recorder = RequestsInstrumentor().meter.metrics.pop() + match_key = get_dict_as_key( + { + "http.method": "GET", + "http.url": "http://httpbin.org/status/200", + } + ) + for key in recorder.bound_instruments.keys(): + self.assertEqual(key, match_key) + # pylint: disable=protected-access + bound = recorder.bound_instruments.get(key) + for view_data in bound.view_datas: + self.assertEqual(view_data.labels, key) + self.assertEqual(view_data.aggregator.current.count, 1) + mocked_response = requests.Response() mocked_response.status_code = 500 mocked_response.reason = "Internal Server Error" @@ -272,6 +311,24 @@ class RequestsIntegrationTestBase(abc.ABC): self.assertEqual( span.status.canonical_code, StatusCanonicalCode.INTERNAL ) + self.assertIsNotNone(RequestsInstrumentor().meter) + self.assertEqual(len(RequestsInstrumentor().meter.metrics), 1) + recorder = RequestsInstrumentor().meter.metrics.pop() + match_key = get_dict_as_key( + { + "http.method": "GET", + "http.status_code": "500", + "http.status_text": "Internal Server Error", + "http.url": "http://httpbin.org/status/200", + } + ) + for key in recorder.bound_instruments.keys(): + self.assertEqual(key, match_key) + # pylint: disable=protected-access + bound = recorder.bound_instruments.get(key) + for view_data in bound.view_datas: + self.assertEqual(view_data.labels, key) + self.assertEqual(view_data.aggregator.current.count, 1) @mock.patch("requests.adapters.HTTPAdapter.send", side_effect=Exception) def test_requests_basic_exception(self, *_, **__): From 534eaa404f3ec7a430abba248ea92ded568117eb Mon Sep 17 00:00:00 2001 From: Owais Lone Date: Wed, 7 Oct 2020 10:22:40 +0530 Subject: [PATCH 11/17] Record exception on context manager exit (#1162) --- .../instrumentation/requests/__init__.py | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index d0336184e..4b5f73de9 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -50,7 +50,11 @@ from opentelemetry.instrumentation.metric import ( from opentelemetry.instrumentation.requests.version import __version__ from opentelemetry.instrumentation.utils import http_status_to_canonical_code from opentelemetry.trace import SpanKind, get_tracer -from opentelemetry.trace.status import Status, StatusCanonicalCode +from opentelemetry.trace.status import ( + EXCEPTION_STATUS_FIELD, + Status, + StatusCanonicalCode, +) # A key to a context variable to avoid creating duplicate spans when instrumenting # both, Session.request and Session.send, since Session.request calls into Session.send @@ -121,8 +125,6 @@ def _instrument(tracer_provider=None, span_callback=None): method = method.upper() span_name = "HTTP {}".format(method) - exception = None - recorder = RequestsInstrumentor().metric_recorder labels = {} @@ -132,6 +134,7 @@ def _instrument(tracer_provider=None, span_callback=None): with get_tracer( __name__, __version__, tracer_provider ).start_as_current_span(span_name, kind=SpanKind.CLIENT) as span: + exception = None with recorder.record_duration(labels): if span.is_recording(): span.set_attribute("component", "http") @@ -150,16 +153,15 @@ def _instrument(tracer_provider=None, span_callback=None): result = call_wrapped() # *** PROCEED except Exception as exc: # pylint: disable=W0703 exception = exc + setattr( + exception, + EXCEPTION_STATUS_FIELD, + _exception_to_canonical_code(exception), + ) result = getattr(exc, "response", None) finally: context.detach(token) - if exception is not None and span.is_recording(): - span.set_status( - Status(_exception_to_canonical_code(exception)) - ) - span.record_exception(exception) - if result is not None: if span.is_recording(): span.set_attribute( @@ -184,8 +186,8 @@ def _instrument(tracer_provider=None, span_callback=None): if span_callback is not None: span_callback(span, result) - if exception is not None: - raise exception.with_traceback(exception.__traceback__) + if exception is not None: + raise exception.with_traceback(exception.__traceback__) return result From e412e65cf69b16745b927f6599f38aec0483fe6c Mon Sep 17 00:00:00 2001 From: alrex Date: Thu, 8 Oct 2020 08:39:04 -0700 Subject: [PATCH 12/17] Parent is now always passed in via Context, intead of Span or SpanContext (#1146) Co-authored-by: Diego Hurtado --- .../tests/test_requests_integration.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py index 2d3636284..678499e87 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py +++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py @@ -198,12 +198,12 @@ class RequestsIntegrationTestBase(abc.ABC): headers = dict(httpretty.last_request().headers) self.assertIn(MockTextMapPropagator.TRACE_ID_KEY, headers) self.assertEqual( - str(span.get_context().trace_id), + str(span.get_span_context().trace_id), headers[MockTextMapPropagator.TRACE_ID_KEY], ) self.assertIn(MockTextMapPropagator.SPAN_ID_KEY, headers) self.assertEqual( - str(span.get_context().span_id), + str(span.get_span_context().span_id), headers[MockTextMapPropagator.SPAN_ID_KEY], ) From 85deb4158bdf772ad06a7d0a97558aff607c5710 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 13 Oct 2020 14:38:09 -0400 Subject: [PATCH 13/17] chore: bump dev version (#1235) --- .../opentelemetry-instrumentation-requests/setup.cfg | 6 +++--- .../src/opentelemetry/instrumentation/requests/version.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg index 9020ae58a..cf3c11d7e 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg +++ b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg @@ -39,13 +39,13 @@ package_dir= =src packages=find_namespace: install_requires = - opentelemetry-api == 0.14.dev0 - opentelemetry-instrumentation == 0.14.dev0 + opentelemetry-api == 0.15.dev0 + opentelemetry-instrumentation == 0.15.dev0 requests ~= 2.0 [options.extras_require] test = - opentelemetry-test == 0.14.dev0 + opentelemetry-test == 0.15.dev0 httpretty ~= 1.0 [options.packages.find] diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py index 0f9902789..e7b342d64 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.14.dev0" +__version__ = "0.15.dev0" From adedd560a4383fe72fba5d4eab16592cbba766fe Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Thu, 15 Oct 2020 20:24:29 -0400 Subject: [PATCH 14/17] Adding metric collection as part of instrumentations - Django (#1230) --- .../opentelemetry-instrumentation-requests/CHANGELOG.md | 2 +- .../opentelemetry/instrumentation/requests/__init__.py | 8 +++++--- .../tests/test_requests_integration.py | 4 +--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md index c2e4dcdbb..96089a9da 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md @@ -10,7 +10,7 @@ Released 2020-09-17 ([#1040](https://github.com/open-telemetry/opentelemetry-python/pull/1040)) - Drop support for Python 3.4 ([#1099](https://github.com/open-telemetry/opentelemetry-python/pull/1099)) -- Add support for http metrics +- Add support for tracking http metrics ([#1116](https://github.com/open-telemetry/opentelemetry-python/pull/1116)) ## Version 0.12b0 diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index 4b5f73de9..770eacf5e 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -45,6 +45,7 @@ from opentelemetry import context, propagators from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.metric import ( HTTPMetricRecorder, + HTTPMetricType, MetricMixin, ) from opentelemetry.instrumentation.requests.version import __version__ @@ -135,7 +136,7 @@ def _instrument(tracer_provider=None, span_callback=None): __name__, __version__, tracer_provider ).start_as_current_span(span_name, kind=SpanKind.CLIENT) as span: exception = None - with recorder.record_duration(labels): + with recorder.record_client_duration(labels): if span.is_recording(): span.set_attribute("component", "http") span.set_attribute("http.method", method) @@ -176,7 +177,6 @@ def _instrument(tracer_provider=None, span_callback=None): ) ) labels["http.status_code"] = str(result.status_code) - labels["http.status_text"] = result.reason if result.raw and result.raw.version: labels["http.flavor"] = ( str(result.raw.version)[:1] @@ -253,7 +253,9 @@ class RequestsInstrumentor(BaseInstrumentor, MetricMixin): __name__, __version__, ) # pylint: disable=W0201 - self.metric_recorder = HTTPMetricRecorder(self.meter, SpanKind.CLIENT) + self.metric_recorder = HTTPMetricRecorder( + self.meter, HTTPMetricType.CLIENT + ) def _uninstrument(self, **kwargs): _uninstrument() diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py index 678499e87..f41e597b2 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py +++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py @@ -97,7 +97,6 @@ class RequestsIntegrationTestBase(abc.ABC): "http.flavor": "1.1", "http.method": "GET", "http.status_code": "200", - "http.status_text": "OK", "http.url": "http://httpbin.org/status/200", } ) @@ -108,7 +107,7 @@ class RequestsIntegrationTestBase(abc.ABC): for view_data in bound.view_datas: self.assertEqual(view_data.labels, key) self.assertEqual(view_data.aggregator.current.count, 1) - self.assertGreater(view_data.aggregator.current.sum, 0) + self.assertGreaterEqual(view_data.aggregator.current.sum, 0) def test_not_foundbasic(self): url_404 = "http://httpbin.org/status/404" @@ -318,7 +317,6 @@ class RequestsIntegrationTestBase(abc.ABC): { "http.method": "GET", "http.status_code": "500", - "http.status_text": "Internal Server Error", "http.url": "http://httpbin.org/status/200", } ) From d02dec58786c84c07995613407b36fcbb215590f Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 28 Oct 2020 17:28:58 -0400 Subject: [PATCH 15/17] Change status codes from grpc status codes, remove setting status in instrumentations except on ERROR (#1282) --- .../instrumentation/requests/__init__.py | 23 +++------------ .../tests/test_requests_integration.py | 29 +++++-------------- 2 files changed, 12 insertions(+), 40 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index 770eacf5e..b4738647d 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -49,12 +49,12 @@ from opentelemetry.instrumentation.metric import ( MetricMixin, ) from opentelemetry.instrumentation.requests.version import __version__ -from opentelemetry.instrumentation.utils import http_status_to_canonical_code +from opentelemetry.instrumentation.utils import http_status_to_status_code from opentelemetry.trace import SpanKind, get_tracer from opentelemetry.trace.status import ( EXCEPTION_STATUS_FIELD, Status, - StatusCanonicalCode, + StatusCode, ) # A key to a context variable to avoid creating duplicate spans when instrumenting @@ -155,9 +155,7 @@ def _instrument(tracer_provider=None, span_callback=None): except Exception as exc: # pylint: disable=W0703 exception = exc setattr( - exception, - EXCEPTION_STATUS_FIELD, - _exception_to_canonical_code(exception), + exception, EXCEPTION_STATUS_FIELD, StatusCode.ERROR, ) result = getattr(exc, "response", None) finally: @@ -171,9 +169,7 @@ def _instrument(tracer_provider=None, span_callback=None): span.set_attribute("http.status_text", result.reason) span.set_status( Status( - http_status_to_canonical_code( - result.status_code - ) + http_status_to_status_code(result.status_code) ) ) labels["http.status_code"] = str(result.status_code) @@ -221,17 +217,6 @@ def _uninstrument_from(instr_root, restore_as_bound_func=False): setattr(instr_root, instr_func_name, original) -def _exception_to_canonical_code(exc: Exception) -> StatusCanonicalCode: - if isinstance( - exc, - (InvalidURL, InvalidSchema, MissingSchema, URLRequired, ValueError), - ): - return StatusCanonicalCode.INVALID_ARGUMENT - if isinstance(exc, Timeout): - return StatusCanonicalCode.DEADLINE_EXCEEDED - return StatusCanonicalCode.UNKNOWN - - class RequestsInstrumentor(BaseInstrumentor, MetricMixin): """An instrumentor for requests See `BaseInstrumentor` diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py index f41e597b2..f5209108e 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py +++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py @@ -25,7 +25,7 @@ from opentelemetry.sdk import resources from opentelemetry.sdk.util import get_dict_as_key from opentelemetry.test.mock_textmap import MockTextMapPropagator from opentelemetry.test.test_base import TestBase -from opentelemetry.trace.status import StatusCanonicalCode +from opentelemetry.trace.status import StatusCode class RequestsIntegrationTestBase(abc.ABC): @@ -81,9 +81,7 @@ class RequestsIntegrationTestBase(abc.ABC): }, ) - self.assertIs( - span.status.canonical_code, trace.status.StatusCanonicalCode.OK - ) + self.assertIs(span.status.status_code, trace.status.StatusCode.UNSET) self.check_span_instrumentation_info( span, opentelemetry.instrumentation.requests @@ -123,8 +121,7 @@ class RequestsIntegrationTestBase(abc.ABC): self.assertEqual(span.attributes.get("http.status_text"), "Not Found") self.assertIs( - span.status.canonical_code, - trace.status.StatusCanonicalCode.NOT_FOUND, + span.status.status_code, trace.status.StatusCode.ERROR, ) def test_uninstrument(self): @@ -263,9 +260,7 @@ class RequestsIntegrationTestBase(abc.ABC): span.attributes, {"component": "http", "http.method": "GET", "http.url": self.URL}, ) - self.assertEqual( - span.status.canonical_code, StatusCanonicalCode.UNKNOWN - ) + self.assertEqual(span.status.status_code, StatusCode.ERROR) self.assertIsNotNone(RequestsInstrumentor().meter) self.assertEqual(len(RequestsInstrumentor().meter.metrics), 1) @@ -307,9 +302,7 @@ class RequestsIntegrationTestBase(abc.ABC): "http.status_text": "Internal Server Error", }, ) - self.assertEqual( - span.status.canonical_code, StatusCanonicalCode.INTERNAL - ) + self.assertEqual(span.status.status_code, StatusCode.ERROR) self.assertIsNotNone(RequestsInstrumentor().meter) self.assertEqual(len(RequestsInstrumentor().meter.metrics), 1) recorder = RequestsInstrumentor().meter.metrics.pop() @@ -334,9 +327,7 @@ class RequestsIntegrationTestBase(abc.ABC): self.perform_request(self.URL) span = self.assert_span() - self.assertEqual( - span.status.canonical_code, StatusCanonicalCode.UNKNOWN - ) + self.assertEqual(span.status.status_code, StatusCode.ERROR) @mock.patch( "requests.adapters.HTTPAdapter.send", side_effect=requests.Timeout @@ -346,9 +337,7 @@ class RequestsIntegrationTestBase(abc.ABC): self.perform_request(self.URL) span = self.assert_span() - self.assertEqual( - span.status.canonical_code, StatusCanonicalCode.DEADLINE_EXCEEDED - ) + self.assertEqual(span.status.status_code, StatusCode.ERROR) class TestRequestsIntegration(RequestsIntegrationTestBase, TestBase): @@ -371,9 +360,7 @@ class TestRequestsIntegration(RequestsIntegrationTestBase, TestBase): span.attributes, {"component": "http", "http.method": "POST", "http.url": url}, ) - self.assertEqual( - span.status.canonical_code, StatusCanonicalCode.INVALID_ARGUMENT - ) + self.assertEqual(span.status.status_code, StatusCode.ERROR) def test_if_headers_equals_none(self): result = requests.get(self.URL, headers=None) From 86d6b3d53596ec5108feaace5c244b3a55ec0f36 Mon Sep 17 00:00:00 2001 From: alrex Date: Mon, 2 Nov 2020 08:01:02 -0800 Subject: [PATCH 16/17] fixing changelogs (#1315) Found a few discrepancies in the changelogs that needed to be fixed --- .../opentelemetry-instrumentation-requests/CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md index 96089a9da..40088edcf 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Add support for tracking http metrics + ([#1230](https://github.com/open-telemetry/opentelemetry-python/pull/1230)) + ## Version 0.13b0 Released 2020-09-17 @@ -10,7 +13,7 @@ Released 2020-09-17 ([#1040](https://github.com/open-telemetry/opentelemetry-python/pull/1040)) - Drop support for Python 3.4 ([#1099](https://github.com/open-telemetry/opentelemetry-python/pull/1099)) -- Add support for tracking http metrics +- Add support for http metrics ([#1116](https://github.com/open-telemetry/opentelemetry-python/pull/1116)) ## Version 0.12b0 From 4eee0a0ca9f81c4f31e83c3b1c73c34831527709 Mon Sep 17 00:00:00 2001 From: alrex Date: Mon, 2 Nov 2020 09:00:06 -0800 Subject: [PATCH 17/17] [pre-release] Update changelogs, version [0.15b0] (#1320) --- .../opentelemetry-instrumentation-requests/CHANGELOG.md | 4 ++++ .../opentelemetry-instrumentation-requests/setup.cfg | 6 +++--- .../src/opentelemetry/instrumentation/requests/version.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md index 40088edcf..00d730f4f 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-requests/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +## Version 0.15b0 + +Released 2020-11-02 + - Add support for tracking http metrics ([#1230](https://github.com/open-telemetry/opentelemetry-python/pull/1230)) diff --git a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg index cf3c11d7e..9cdb5b8d0 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/setup.cfg +++ b/instrumentation/opentelemetry-instrumentation-requests/setup.cfg @@ -39,13 +39,13 @@ package_dir= =src packages=find_namespace: install_requires = - opentelemetry-api == 0.15.dev0 - opentelemetry-instrumentation == 0.15.dev0 + opentelemetry-api == 0.15b0 + opentelemetry-instrumentation == 0.15b0 requests ~= 2.0 [options.extras_require] test = - opentelemetry-test == 0.15.dev0 + opentelemetry-test == 0.15b0 httpretty ~= 1.0 [options.packages.find] diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py index e7b342d64..ff494d225 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.15.dev0" +__version__ = "0.15b0"