Skip to content

fastapi instrumentation #890

New issue

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

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

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions docs/ext/fastapi/fastapi.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.. include:: ../../../ext/opentelemetry-instrumentation-fastapi/README.rst

API
---

.. automodule:: opentelemetry.instrumentation.fastapi
:members:
:undoc-members:
:show-inheritance:
5 changes: 5 additions & 0 deletions ext/opentelemetry-instrumentation-fastapi/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## Unreleased

- Initial release ([#890](https://github.com/open-telemetry/opentelemetry-python/pull/890))
43 changes: 43 additions & 0 deletions ext/opentelemetry-instrumentation-fastapi/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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

import fastapi
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

app = fastapi.FastAPI()

@app.get("/foobar")
async def foobar():
return {"message": "hello world"}

FastAPIInstrumentor.instrument_app(app)


References
----------

* `OpenTelemetry Project <https://opentelemetry.io/>`_
55 changes: 55 additions & 0 deletions ext/opentelemetry-instrumentation-fastapi/setup.cfg
Original file line number Diff line number Diff line change
@@ -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 = [email protected]
url = https://github.com/open-telemetry/opentelemetry-python/tree/master/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
31 changes: 31 additions & 0 deletions ext/opentelemetry-instrumentation-fastapi/setup.py
Original file line number Diff line number Diff line change
@@ -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__"])
Original file line number Diff line number Diff line change
@@ -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.fastapi.version import __version__ # noqa
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor


class FastAPIInstrumentor(BaseInstrumentor):
"""An instrumentor for FastAPI

See `BaseInstrumentor`
"""

_original_fastapi = None

@staticmethod
def instrument_app(app: fastapi.FastAPI):
"""Instrument an uninstrumented 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 fastapi route being served.

TODO: there is currently no way to retrieve http.route from
a starlette application from scope.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
a starlette application from scope.
a fastapi application from scope.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually this is still utilizing the starlette components of fastapi. So either is correct.

I wanted to link to the actual blocker ticket here, which is in the starlette repo, rather than FastAPI.


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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you briefly explain what this "hack" does to get the http.route? What is in app.routes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app.routes is the list of routes added to starlette: https://www.starlette.io/routing/

This mimics logic in the starlette app router itself to match to one of the routes in the list, and find the appropriate one.

In the future the code in the linked pr in the docstring will add the appropriate objects in the scope, at which point we will be able to extract the 'http.route' object by querying the first object in the "routes" value added to the scope: encode/starlette#804.

By doing this, we can avoid two iterations of all match objects, which will lead to performance hits depending on the complexity and number of routes added.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right that makes sense. What I'm confused about is, if starlette_route is one of the list of routes added, why match it with scope? Does scope contain the actual route that is being used, but we don't know the key? Also, is matches a method of the Route object in starlette? I can't seem to find documentation on it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, the documentation around starlette is a bit rough. I found it via looking through the code (particularly the router part. the starlette PR above is still a good reference).

a starlette_route is just an object, and you iterate through all routes mounted on the app, regardless of whether it's appropriate for the http route or not. the scope contains the url and path, which is what the route is matching against (as well as other things like HTTP method).

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
Original file line number Diff line number Diff line change
@@ -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"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# 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.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():
app = fastapi.FastAPI()

@app.get("/foobar")
async def _():
return {"message": "hello world"}

@app.get("/user/{username}")
async def _(username: str):
return {"message": username}

return app


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)
2 changes: 1 addition & 1 deletion ext/opentelemetry-instrumentation-starlette/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ long_description = file: README.rst
long_description_content_type = text/x-rst
author = OpenTelemetry Authors
author_email = [email protected]
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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading