-
Notifications
You must be signed in to change notification settings - Fork 649
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
fastapi instrumentation #890
Conversation
setuptools + pip update didn't run for python3.8, despite our prevalent usage of it.
Fixing changelog to appropriate changes updating some incorrect links in setup.cfg reverting unneeded changes to tox.
Curious, are FastAPI and Starlette different web frameworks built on top of asgi? |
ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py
Outdated
Show resolved
Hide resolved
ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py
Show resolved
Hide resolved
ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py
Outdated
Show resolved
Hide resolved
"""Callback to retrieve the starlette route being served. | ||
|
||
TODO: there is currently no way to retrieve http.route from | ||
a starlette application from scope. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a starlette application from scope. | |
a fastapi application from scope. |
There was a problem hiding this comment.
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.
ext/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py
Show resolved
Hide resolved
route = None | ||
for starlette_route in app.routes: | ||
match, _ = starlette_route.matches(scope) | ||
if match == Match.FULL: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
…strumentation/fastapi/__init__.py Co-authored-by: Leighton Chen <lechen@microsoft.com>
Yes! It's actually asgi <- starlette <- fastapi. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
There was a problem hiding this 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!
Full fastapi instrumentation. This is very similar to starlette.