Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fastapi instrumentation #890

Merged

Conversation

toumorokoshi
Copy link
Member

Full fastapi instrumentation. This is very similar to starlette.

setuptools + pip update didn't run for python3.8, despite
our prevalent usage of it.
@toumorokoshi toumorokoshi requested a review from a team July 9, 2020 15:59
Fixing changelog to appropriate changes

updating some incorrect links in setup.cfg

reverting unneeded changes to tox.
@lzchen
Copy link
Contributor

lzchen commented Jul 9, 2020

Curious, are FastAPI and Starlette different web frameworks built on top of asgi?

"""Callback to retrieve the starlette 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.

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).

@toumorokoshi
Copy link
Member Author

Curious, are FastAPI and Starlette different web frameworks built on top of asgi?

Yes! It's actually asgi <- starlette <- fastapi.

Copy link
Contributor

@ocelotl ocelotl left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

@codeboten codeboten merged commit 7bec76a into open-telemetry:master Jul 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants