-
Notifications
You must be signed in to change notification settings - Fork 469
fix(fastapi): apply tracing to user middlewares in fastapi [backport 1.8] #5084
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
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mabdinur
previously approved these changes
Feb 9, 2023
mabdinur
reviewed
Feb 9, 2023
tests/snapshots/tests.contrib.fastapi.test_fastapi.test_tracing_in_middleware.json
Outdated
Show resolved
Hide resolved
mabdinur
previously approved these changes
Feb 9, 2023
## Description Resolve: #4397 In Starlette and FastAPI applications the last middleware configured is the first one called ([here](https://github.com/encode/starlette/blob/0.21.0/starlette/applications.py#L168)). The fastapi trace middleware is applied when a fastapi application is created ([fastapi.FastAPI()](https://github.com/DataDog/dd-trace-py/blob/a21c8dbc433577245f5c145fd46f2a3dc4540402/ddtrace/contrib/fastapi/patch.py#L38)). If another middleware is configured after an application is initialized it will not be traced. Here's an example of a fastapi middleware being configured after `fastapi.FastApi()` is called: https://fastapi.tiangolo.com/tutorial/middleware/#create-a-middleware. Note - This PR only ensures Fastapi middlewares are traced. Supporting all starlette middlewares is out of scope. ## Reviewer Checklist - [ ] Title is accurate. - [ ] Description motivates each change. - [ ] No unnecessary changes were introduced in this PR. - [ ] Avoid breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [ ] Tests provided or description of manual testing performed is included in the code or PR. - [ ] Release note has been added for fixes and features, or else `changelog/no-changelog` label added. - [ ] All relevant GitHub issues are correctly linked. - [ ] Backports are identified and tagged with Mergifyio. Co-authored-by: Brett Langdon <[email protected]> (cherry picked from commit b5a4511)
1d0f779 to
3a3e0a3
Compare
emmettbutler
approved these changes
Feb 14, 2023
Yun-Kim
approved these changes
Feb 14, 2023
Codecov Report
@@ Coverage Diff @@
## 1.8 #5084 +/- ##
==========================================
+ Coverage 74.88% 74.89% +0.01%
==========================================
Files 816 816
Lines 63501 63513 +12
==========================================
+ Hits 47552 47569 +17
+ Misses 15949 15944 -5
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
|
For anyone who also finds their way here, this was released in ddtrace 1.9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Backport b5a4511 from #4478 to 1.8.
Description
Resolve: #4397
In Starlette and FastAPI applications the last middleware configured is the first one called (here). The fastapi trace middleware is applied when a fastapi application is created (fastapi.FastAPI()). If another middleware is configured after an application is initialized it will not be traced.
Here's an example of a fastapi middleware being configured after
fastapi.FastApi()is called: https://fastapi.tiangolo.com/tutorial/middleware/#create-a-middleware.Note - This PR only ensures Fastapi middlewares are traced. Supporting all starlette middlewares is out of scope.
Reviewer Checklist
changelog/no-changeloglabel added.