From d26d8ea4256a2c340e96b7a22ae183c8dc46f86b Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Tue, 7 Jul 2020 23:13:47 -0700 Subject: [PATCH 1/6] fastapi instrumentation --- docs-requirements.txt | 1 + docs/ext/fastapi/fastapi.rst | 9 ++ .../CHANGELOG.md | 9 ++ .../README.rst | 45 ++++++++ .../setup.cfg | 55 +++++++++ .../setup.py | 31 +++++ .../instrumentation/fastapi/__init__.py | 82 +++++++++++++ .../instrumentation/fastapi/version.py | 15 +++ .../tests/__init__.py | 0 .../tests/test_fastapi_instrumentation.py | 109 ++++++++++++++++++ tox.ini | 15 ++- 11 files changed, 368 insertions(+), 3 deletions(-) create mode 100644 docs/ext/fastapi/fastapi.rst create mode 100644 ext/opentelemetry-instrumentation-fastapi/CHANGELOG.md create mode 100644 ext/opentelemetry-instrumentation-fastapi/README.rst create mode 100644 ext/opentelemetry-instrumentation-fastapi/setup.cfg create mode 100644 ext/opentelemetry-instrumentation-fastapi/setup.py create mode 100644 ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py create mode 100644 ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/version.py create mode 100644 ext/opentelemetry-instrumentation-fastapi/tests/__init__.py create mode 100644 ext/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py diff --git a/docs-requirements.txt b/docs-requirements.txt index 230c76149cc..169bbb7f0b1 100644 --- a/docs-requirements.txt +++ b/docs-requirements.txt @@ -29,3 +29,4 @@ google-cloud-trace >=0.23.0 google-cloud-monitoring>=0.36.0 botocore~=1.0 starlette~=0.13 +fastapi~=0.58.1 \ No newline at end of file diff --git a/docs/ext/fastapi/fastapi.rst b/docs/ext/fastapi/fastapi.rst new file mode 100644 index 00000000000..9295261584b --- /dev/null +++ b/docs/ext/fastapi/fastapi.rst @@ -0,0 +1,9 @@ +.. include:: ../../../ext/opentelemetry-instrumentation-fastapi/README.rst + +API +--- + +.. automodule:: opentelemetry.instrumentation.fastapi + :members: + :undoc-members: + :show-inheritance: \ No newline at end of file diff --git a/ext/opentelemetry-instrumentation-fastapi/CHANGELOG.md b/ext/opentelemetry-instrumentation-fastapi/CHANGELOG.md new file mode 100644 index 00000000000..1991025f6cb --- /dev/null +++ b/ext/opentelemetry-instrumentation-fastapi/CHANGELOG.md @@ -0,0 +1,9 @@ +# Changelog + +## Unreleased + +## Version 0.10b0 + +Released 2020-06-23 + +- Initial release ([#777](https://github.com/open-telemetry/opentelemetry-python/pull/777)) \ No newline at end of file diff --git a/ext/opentelemetry-instrumentation-fastapi/README.rst b/ext/opentelemetry-instrumentation-fastapi/README.rst new file mode 100644 index 00000000000..db83d8916bd --- /dev/null +++ b/ext/opentelemetry-instrumentation-fastapi/README.rst @@ -0,0 +1,45 @@ +OpenTelemetry FastAPI Instrumentation +======================================= + +|pypi| + +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-fastapi.svg + :target: https://pypi.org/project/opentelemetry-instrumentation-fastapi/ + + +This library provides automatic and manual instrumentation of FastAPI web frameworks, +instrumenting http requests served by applications utilizing the framework. + +auto-instrumentation using the opentelemetry-instrumentation package is also supported. + +Installation +------------ + +:: + + pip install opentelemetry-instrumentation-fastapi + + +Usage +----- + +.. code-block:: python + + from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor + import fastapi + from fastapi.responses import PlainTextResponse + from fastapi.routing import Route + + def home(request): + return PlainTextResponse("hi") + + app = fastapi.FastAPI( + routes=[Route("/foobar", home)] + ) + FastAPIInstrumentor.instrument_app(app) + + +References +---------- + +* `OpenTelemetry Project `_ \ No newline at end of file diff --git a/ext/opentelemetry-instrumentation-fastapi/setup.cfg b/ext/opentelemetry-instrumentation-fastapi/setup.cfg new file mode 100644 index 00000000000..5f0a5e6c06b --- /dev/null +++ b/ext/opentelemetry-instrumentation-fastapi/setup.cfg @@ -0,0 +1,55 @@ +# 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-fastapi +description = OpenTelemetry FastAPI 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/ext/opentelemetry-instrumentation-fastapi +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.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + +[options] +python_requires = >=3.6 +package_dir= + =src +packages=find_namespace: +install_requires = + opentelemetry-api == 0.11.dev0 + opentelemetry-ext-asgi == 0.11.dev0 + +[options.entry_points] +opentelemetry_instrumentor = + fastapi = opentelemetry.instrumentation.fastapi:FastAPIInstrumentor + +[options.extras_require] +test = + opentelemetry-test == 0.11.dev0 + fastapi ~= 0.58.1 + requests ~= 2.23.0 # needed for testclient + +[options.packages.find] +where = src diff --git a/ext/opentelemetry-instrumentation-fastapi/setup.py b/ext/opentelemetry-instrumentation-fastapi/setup.py new file mode 100644 index 00000000000..13c7c5a99c0 --- /dev/null +++ b/ext/opentelemetry-instrumentation-fastapi/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", + "fastapi", + "version.py", +) +PACKAGE_INFO = {} +with open(VERSION_FILENAME) as f: + exec(f.read(), PACKAGE_INFO) + +setuptools.setup(version=PACKAGE_INFO["__version__"]) diff --git a/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py b/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py new file mode 100644 index 00000000000..933528d2edc --- /dev/null +++ b/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py @@ -0,0 +1,82 @@ +# 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 typing import Optional + +import fastapi +from starlette.routing import Match + +from opentelemetry.ext.asgi import OpenTelemetryMiddleware +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor +from opentelemetry.instrumentation.fastapi.version import __version__ # noqa + + +class FastAPIInstrumentor(BaseInstrumentor): + """An instrumentor for FastAPI + + See `BaseInstrumentor` + """ + + _original_fastapi = None + + @staticmethod + def instrument_app(app: fastapi.FastAPI): + """Instrument a previously instrumented FastAPI application. + """ + if not getattr(app, "is_instrumented_by_opentelemetry", False): + app.add_middleware( + OpenTelemetryMiddleware, + span_details_callback=_get_route_details, + ) + app.is_instrumented_by_opentelemetry = True + + def _instrument(self, **kwargs): + self._original_fastapi = fastapi.FastAPI + fastapi.FastAPI = _InstrumentedFastAPI + + def _uninstrument(self, **kwargs): + fastapi.FastAPI = self._original_fastapi + + +class _InstrumentedFastAPI(fastapi.FastAPI): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.add_middleware( + OpenTelemetryMiddleware, span_details_callback=_get_route_details + ) + + +def _get_route_details(scope): + """Callback to retrieve the starlette route being served. + + TODO: there is currently no way to retrieve http.route from + a starlette application from scope. + + See: https://github.com/encode/starlette/pull/804 + """ + app = scope["app"] + route = None + for starlette_route in app.routes: + match, _ = starlette_route.matches(scope) + if match == Match.FULL: + route = starlette_route.path + break + if match == Match.PARTIAL: + route = starlette_route.path + # method only exists for http, if websocket + # leave it blank. + span_name = route or scope.get("method", "") + attributes = {} + if route: + attributes["http.route"] = route + return span_name, attributes diff --git a/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/version.py b/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/version.py new file mode 100644 index 00000000000..858e73960ff --- /dev/null +++ b/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/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.11.dev0" diff --git a/ext/opentelemetry-instrumentation-fastapi/tests/__init__.py b/ext/opentelemetry-instrumentation-fastapi/tests/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/ext/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py b/ext/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py new file mode 100644 index 00000000000..b9bd044de16 --- /dev/null +++ b/ext/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py @@ -0,0 +1,109 @@ +# 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 unittest + +import fastapi +from fastapi.responses import PlainTextResponse +from fastapi.routing import Route +from fastapi.testclient import TestClient + +import opentelemetry.instrumentation.fastapi as otel_fastapi +from opentelemetry.test.test_base import TestBase + + +class TestFastAPIManualInstrumentation(TestBase): + def _create_app(self): + app = self._create_fastapi_app() + self._instrumentor.instrument_app(app) + return app + + def setUp(self): + super().setUp() + self._instrumentor = otel_fastapi.FastAPIInstrumentor() + self._app = self._create_app() + self._client = TestClient(self._app) + + def test_basic_fastapi_call(self): + self._client.get("/foobar") + spans = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans), 3) + for span in spans: + self.assertIn("/foobar", span.name) + + def test_fastapi_route_attribute_added(self): + """Ensure that fastapi routes are used as the span name.""" + self._client.get("/user/123") + spans = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans), 3) + for span in spans: + self.assertIn("/user/{username}", span.name) + self.assertEqual( + spans[-1].attributes["http.route"], "/user/{username}" + ) + # ensure that at least one attribute that is populated by + # the asgi instrumentation is successfully feeding though. + self.assertEqual(spans[-1].attributes["http.flavor"], "1.1") + + @staticmethod + def _create_fastapi_app(): + def home(_): + return PlainTextResponse("hi") + + app = fastapi.FastAPI( + routes=[Route("/foobar", home), Route("/user/{username}", home)] + ) + + @app.get("/foobar") + async def foobar(): + return {"message": "hello world"} + + @app.get("/user/{username}") + async def user(username: str): + return {"message": username} + + +class TestAutoInstrumentation(TestFastAPIManualInstrumentation): + """Test the auto-instrumented variant + + Extending the manual instrumentation as most test cases apply + to both. + """ + + def _create_app(self): + # instrumentation is handled by the instrument call + self._instrumentor.instrument() + return self._create_fastapi_app() + + def tearDown(self): + self._instrumentor.uninstrument() + super().tearDown() + + +class TestAutoInstrumentationLogic(unittest.TestCase): + def test_instrumentation(self): + """Verify that instrumentation methods are instrumenting and + removing as expected. + """ + instrumentor = otel_fastapi.FastAPIInstrumentor() + original = fastapi.FastAPI + instrumentor.instrument() + try: + instrumented = fastapi.FastAPI + self.assertIsNot(original, instrumented) + finally: + instrumentor.uninstrument() + + should_be_original = fastapi.FastAPI + self.assertIs(original, should_be_original) diff --git a/tox.ini b/tox.ini index e4ad8e17098..e1f46cf35c5 100644 --- a/tox.ini +++ b/tox.ini @@ -52,6 +52,11 @@ envlist = py3{4,5,6,7,8}-test-ext-elasticsearch{2,5,6,7} pypy3-test-ext-elasticsearch{2,5,6,7} + ; opentelemetry-instrumentation-fastapi + ; fastapi only supports 3.6 and above. + py3{6,7,8}-test-instrumentation-fastapi + pypy3-test-instrumentation-fastapi + ; opentelemetry-ext-flask py3{4,5,6,7,8}-test-ext-flask pypy3-test-ext-flask @@ -199,6 +204,7 @@ changedir = test-sdk: opentelemetry-sdk/tests instrumentation-base: opentelemetry-instrumentation/tests test-instrumentation-starlette: ext/opentelemetry-instrumentation-starlette/tests + test-instrumentation-fastapi: ext/opentelemetry-instrumentation-fastapi/tests test-proto: opentelemetry-proto/tests test-ext-grpc: ext/opentelemetry-ext-grpc/tests test-ext-aiohttp-client: ext/opentelemetry-ext-aiohttp-client/tests @@ -241,7 +247,9 @@ commands_pre = py3{4,5,6,7}: python -m pip install -U pip setuptools wheel ; Install common packages for all the tests. These are not needed in all the ; cases but it saves a lot of boilerplate in this file. - test: pip install {toxinidir}/opentelemetry-api {toxinidir}/opentelemetry-sdk {toxinidir}/tests/util + test: pip install {toxinidir}/opentelemetry-api + test: pip install {toxinidir}/opentelemetry-sdk + test: pip install {toxinidir}/tests/util test-proto: pip install {toxinidir}/opentelemetry-proto ext,instrumentation: pip install {toxinidir}/opentelemetry-instrumentation @@ -254,9 +262,8 @@ commands_pre = grpc: pip install {toxinidir}/ext/opentelemetry-ext-grpc[test] - wsgi,flask,django,asgi,pyramid,starlette: pip install {toxinidir}/tests/util wsgi,flask,django,pyramid: pip install {toxinidir}/ext/opentelemetry-ext-wsgi - asgi,starlette: pip install {toxinidir}/ext/opentelemetry-ext-asgi + asgi,starlette,fastapi: pip install {toxinidir}/ext/opentelemetry-ext-asgi asyncpg: pip install {toxinidir}/ext/opentelemetry-ext-asyncpg @@ -271,6 +278,8 @@ commands_pre = django: pip install {toxinidir}/ext/opentelemetry-ext-django[test] + fastapi: pip install {toxinidir}/ext/opentelemetry-instrumentation-fastapi[test] + mysql: pip install {toxinidir}/ext/opentelemetry-ext-dbapi {toxinidir}/ext/opentelemetry-ext-mysql[test] opencensusexporter: pip install {toxinidir}/ext/opentelemetry-ext-opencensusexporter From 24bb7711b4576c86c48ab05c2bdda86f83e76eaa Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Thu, 9 Jul 2020 08:50:35 -0700 Subject: [PATCH 2/6] adding setuptools update for tox tests setuptools + pip update didn't run for python3.8, despite our prevalent usage of it. --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index e1f46cf35c5..e9aee50693d 100644 --- a/tox.ini +++ b/tox.ini @@ -244,7 +244,7 @@ changedir = commands_pre = ; Install without -e to test the actual installation - py3{4,5,6,7}: python -m pip install -U pip setuptools wheel + py3{4,5,6,7,8}: python -m pip install -U pip setuptools wheel ; Install common packages for all the tests. These are not needed in all the ; cases but it saves a lot of boilerplate in this file. test: pip install {toxinidir}/opentelemetry-api From 040690f959d0841d6743aa2669862a628c6c1bac Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Thu, 9 Jul 2020 08:58:20 -0700 Subject: [PATCH 3/6] fixing until tests pass --- .../tests/test_fastapi_instrumentation.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/ext/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py b/ext/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py index b9bd044de16..bb616e44d7a 100644 --- a/ext/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py +++ b/ext/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py @@ -15,8 +15,6 @@ import unittest import fastapi -from fastapi.responses import PlainTextResponse -from fastapi.routing import Route from fastapi.testclient import TestClient import opentelemetry.instrumentation.fastapi as otel_fastapi @@ -58,12 +56,7 @@ def test_fastapi_route_attribute_added(self): @staticmethod def _create_fastapi_app(): - def home(_): - return PlainTextResponse("hi") - - app = fastapi.FastAPI( - routes=[Route("/foobar", home), Route("/user/{username}", home)] - ) + app = fastapi.FastAPI() @app.get("/foobar") async def foobar(): @@ -73,6 +66,8 @@ async def foobar(): async def user(username: str): return {"message": username} + return app + class TestAutoInstrumentation(TestFastAPIManualInstrumentation): """Test the auto-instrumented variant From d27b35eca0842e27962385ec72b36f99e742e44f Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Thu, 9 Jul 2020 09:04:51 -0700 Subject: [PATCH 4/6] more cleanup Fixing changelog to appropriate changes updating some incorrect links in setup.cfg reverting unneeded changes to tox. --- .../CHANGELOG.md | 6 +----- .../README.rst | 14 ++++++-------- .../setup.cfg | 2 +- .../setup.cfg | 2 +- tox.ini | 7 ++----- 5 files changed, 11 insertions(+), 20 deletions(-) diff --git a/ext/opentelemetry-instrumentation-fastapi/CHANGELOG.md b/ext/opentelemetry-instrumentation-fastapi/CHANGELOG.md index 1991025f6cb..684dece0c62 100644 --- a/ext/opentelemetry-instrumentation-fastapi/CHANGELOG.md +++ b/ext/opentelemetry-instrumentation-fastapi/CHANGELOG.md @@ -2,8 +2,4 @@ ## Unreleased -## Version 0.10b0 - -Released 2020-06-23 - -- Initial release ([#777](https://github.com/open-telemetry/opentelemetry-python/pull/777)) \ No newline at end of file +- Initial release ([#890](https://github.com/open-telemetry/opentelemetry-python/pull/890)) \ No newline at end of file diff --git a/ext/opentelemetry-instrumentation-fastapi/README.rst b/ext/opentelemetry-instrumentation-fastapi/README.rst index db83d8916bd..4cc612da760 100644 --- a/ext/opentelemetry-instrumentation-fastapi/README.rst +++ b/ext/opentelemetry-instrumentation-fastapi/README.rst @@ -25,17 +25,15 @@ Usage .. code-block:: python - from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor import fastapi - from fastapi.responses import PlainTextResponse - from fastapi.routing import Route + from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor + + app = fastapi.FastAPI() - def home(request): - return PlainTextResponse("hi") + @app.get("/foobar") + async def foobar(): + return {"message": "hello world"} - app = fastapi.FastAPI( - routes=[Route("/foobar", home)] - ) FastAPIInstrumentor.instrument_app(app) diff --git a/ext/opentelemetry-instrumentation-fastapi/setup.cfg b/ext/opentelemetry-instrumentation-fastapi/setup.cfg index 5f0a5e6c06b..5e7c2fafa6b 100644 --- a/ext/opentelemetry-instrumentation-fastapi/setup.cfg +++ b/ext/opentelemetry-instrumentation-fastapi/setup.cfg @@ -19,7 +19,7 @@ long_description = file: README.rst long_description_content_type = text/x-rst author = OpenTelemetry Authors author_email = cncf-opentelemetry-contributors@lists.cncf.io -url = https://github.com/open-telemetry/opentelemetry-python/ext/opentelemetry-instrumentation-fastapi +url = https://github.com/open-telemetry/opentelemetry-python/tree/master/ext/opentelemetry-instrumentation-fastapi platforms = any license = Apache-2.0 classifiers = diff --git a/ext/opentelemetry-instrumentation-starlette/setup.cfg b/ext/opentelemetry-instrumentation-starlette/setup.cfg index cea0cc11881..4c777a18c50 100644 --- a/ext/opentelemetry-instrumentation-starlette/setup.cfg +++ b/ext/opentelemetry-instrumentation-starlette/setup.cfg @@ -19,7 +19,7 @@ long_description = file: README.rst long_description_content_type = text/x-rst author = OpenTelemetry Authors author_email = cncf-opentelemetry-contributors@lists.cncf.io -url = https://github.com/open-telemetry/opentelemetry-python/ext/opentelemetry-instrumentation-starlette +url = https://github.com/open-telemetry/opentelemetry-python/tree/master/ext/opentelemetry-instrumentation-starlette platforms = any license = Apache-2.0 classifiers = diff --git a/tox.ini b/tox.ini index e9aee50693d..17b536ee973 100644 --- a/tox.ini +++ b/tox.ini @@ -196,8 +196,7 @@ deps = elasticsearch7: elasticsearch-dsl>=7.0,<8.0 elasticsearch7: elasticsearch>=7.0,<8.0 -setenv = - mypy: MYPYPATH={toxinidir}/opentelemetry-api/src/ +setenv = mypy: MYPYPATH={toxinidir}/opentelemetry-api/src/ changedir = test-api: opentelemetry-api/tests @@ -247,9 +246,7 @@ commands_pre = py3{4,5,6,7,8}: python -m pip install -U pip setuptools wheel ; Install common packages for all the tests. These are not needed in all the ; cases but it saves a lot of boilerplate in this file. - test: pip install {toxinidir}/opentelemetry-api - test: pip install {toxinidir}/opentelemetry-sdk - test: pip install {toxinidir}/tests/util + test: pip install {toxinidir}/opentelemetry-api {toxinidir}/opentelemetry-sdk {toxinidir}/tests/util test-proto: pip install {toxinidir}/opentelemetry-proto ext,instrumentation: pip install {toxinidir}/opentelemetry-instrumentation From 3a0b7ca1676f91cf9c9e3d7bd5ddb31c00e5b918 Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Fri, 10 Jul 2020 04:22:44 +0000 Subject: [PATCH 5/6] Update ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py Co-authored-by: Leighton Chen --- .../src/opentelemetry/instrumentation/fastapi/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py b/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py index 933528d2edc..34149b1dff1 100644 --- a/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py +++ b/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py @@ -57,7 +57,7 @@ def __init__(self, *args, **kwargs): def _get_route_details(scope): - """Callback to retrieve the starlette route being served. + """Callback to retrieve the fastapi route being served. TODO: there is currently no way to retrieve http.route from a starlette application from scope. From 2d6b9cc0df5dbfb8b066001ea07ecea446d73a84 Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Thu, 9 Jul 2020 21:23:13 -0700 Subject: [PATCH 6/6] fixing lint, addressing comments --- .../src/opentelemetry/instrumentation/fastapi/__init__.py | 4 ++-- .../tests/test_fastapi_instrumentation.py | 4 ++-- .../src/opentelemetry/instrumentation/starlette/__init__.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py b/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py index 34149b1dff1..65d608393d7 100644 --- a/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py +++ b/ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py @@ -17,8 +17,8 @@ from starlette.routing import Match from opentelemetry.ext.asgi import OpenTelemetryMiddleware -from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.fastapi.version import __version__ # noqa +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor class FastAPIInstrumentor(BaseInstrumentor): @@ -31,7 +31,7 @@ class FastAPIInstrumentor(BaseInstrumentor): @staticmethod def instrument_app(app: fastapi.FastAPI): - """Instrument a previously instrumented FastAPI application. + """Instrument an uninstrumented FastAPI application. """ if not getattr(app, "is_instrumented_by_opentelemetry", False): app.add_middleware( diff --git a/ext/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py b/ext/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py index bb616e44d7a..47617d4e959 100644 --- a/ext/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py +++ b/ext/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py @@ -59,11 +59,11 @@ def _create_fastapi_app(): app = fastapi.FastAPI() @app.get("/foobar") - async def foobar(): + async def _(): return {"message": "hello world"} @app.get("/user/{username}") - async def user(username: str): + async def _(username: str): return {"message": username} return app diff --git a/ext/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py b/ext/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py index 197a38d7591..b8763bba055 100644 --- a/ext/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py +++ b/ext/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py @@ -31,7 +31,7 @@ class StarletteInstrumentor(BaseInstrumentor): @staticmethod def instrument_app(app: applications.Starlette): - """Instrument a previously instrumented Starlette application. + """Instrument an uninstrumented Starlette application. """ if not getattr(app, "is_instrumented_by_opentelemetry", False): app.add_middleware(