starlette/fastapi: fix error on host-based routing #3507
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Handle case where the Starlette/FastAPI app uses a hosted-based routing. In this case, there is no
path
attribute to read from the routing class. Therefore, fallback to the path present inscope
. The fix is inspired from what Sentry did to fix a similar bug: https://github.com/getsentry/sentry-python/blob/312f85739af27bb9665f61fedd9a589178a67000/sentry_sdk/integrations/starlette.py#L696-L700Fixes #3506
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Added basic unit tests to assess the instrument not longer crashes when a host-based route is called.
Note
I've not added assertions regarding the collected spans/traces, wasn't really sure where to start. But happy to improve that with some guidance :)
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.
[ ] Documentation has been updated(not relevant)