Skip to content

Conversation

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented Feb 9, 2023

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

  • Title is accurate.
  • Description motivates each change.
  • No unnecessary changes were introduced in this PR.
  • Avoid breaking API 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.

@github-actions github-actions bot requested a review from a team as a code owner February 9, 2023 04:35
mabdinur
mabdinur previously approved these changes Feb 9, 2023
@mabdinur mabdinur enabled auto-merge (squash) February 9, 2023 04:42
mabdinur
mabdinur previously approved these changes Feb 9, 2023
mabdinur and others added 2 commits February 10, 2023 11:06
## 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)
@codecov-commenter
Copy link

codecov-commenter commented Feb 17, 2023

Codecov Report

Merging #5084 (d1c6d80) into 1.8 (55525ba) will increase coverage by 0.01%.
The diff coverage is 86.95%.

@@            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     
Impacted Files Coverage Δ
tests/contrib/fastapi/test_fastapi_patch.py 47.82% <0.00%> (ø)
ddtrace/contrib/fastapi/patch.py 87.75% <100.00%> (-0.93%) ⬇️
tests/contrib/fastapi/test_fastapi.py 100.00% <100.00%> (ø)
tests/utils.py 90.42% <0.00%> (+0.23%) ⬆️
ddtrace/internal/remoteconfig/client.py 20.15% <0.00%> (+0.38%) ⬆️
ddtrace/internal/telemetry/writer.py 34.48% <0.00%> (+0.68%) ⬆️
ddtrace/contrib/cassandra/session.py 87.91% <0.00%> (+1.09%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

auto-merge was automatically disabled February 24, 2023 21:55

Pull request was closed

@emmettbutler emmettbutler deleted the backport-4478-to-1.8 branch February 24, 2023 21:55
@ryan-mccaffrey
Copy link

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants