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

fix(asgi, starlette, fastapi): exclude background task durations from web requests #3799

Merged

Conversation

wantsui
Copy link
Collaborator

@wantsui wantsui commented Jun 7, 2022

Following up on #3023, currently libraries traced via the asgi integration, such asfastapi.request and starlette.request spans include the duration of background tasks. This PR updates the asgi middleware code so that spans end earlier by excluding background tasks to better represent the web requests response times.

Current Behavior
$$\begin{align}duration = {http response time} + {background tasks time}\end{align}$$

What it looks like with the PR
$$\begin{align}duration = {http response time}\end{align}$$

Example: An application return a "hello world!" to the user in $2 milliseconds$, but the events that happen in the background lasts another $8 seconds$.

With this PR, the span will report a duration of $2 milliseconds$, instead of $2 milliseconds + 8 seconds$.

Fixes #3023

Also fixes this failing test: https://app.circleci.com/pipelines/github/DataDog/dd-trace-py/18150/workflows/24caa985-5805-4bff-a4eb-5b143f24a2fb/jobs/1233079

Checklist

  • Library documentation is updated.
  • Corp site documentation is updated (link to the PR).

Reviewer Checklist

  • Title is accurate.
  • Description motivates each change.
  • No unnecessary changes were introduced in this PR.
  • PR cannot be broken up into smaller PRs.
  • 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.
  • Add to milestone.

@wantsui wantsui changed the title fastapi: exclude background task duration from fastapi request fastapi: exclude background task duration from fastapi and starlette requests Jun 9, 2022
@wantsui wantsui changed the title fastapi: exclude background task duration from fastapi and starlette requests fastapi: exclude background task durations from fastapi and starlette requests Jun 9, 2022
@wantsui wantsui marked this pull request as ready for review June 9, 2022 21:21
@wantsui wantsui requested a review from a team as a code owner June 9, 2022 21:21
@wantsui wantsui changed the title fastapi: exclude background task durations from fastapi and starlette requests fix(asgi, starlette, fastapi): exclude background task durations from web requests Jun 10, 2022
@mergify
Copy link
Contributor

mergify bot commented Jun 12, 2022

@wantsui this pull request is now in conflict 😩

@mergify mergify bot added conflict and removed conflict labels Jun 12, 2022
tests/contrib/fastapi/test_fastapi.py Outdated Show resolved Hide resolved
tests/contrib/fastapi/test_fastapi.py Outdated Show resolved Hide resolved
tests/contrib/starlette/test_starlette.py Outdated Show resolved Hide resolved
tests/contrib/starlette/test_starlette.py Outdated Show resolved Hide resolved
@wantsui wantsui force-pushed the wantsui/fastapi-background-duration-update branch from 2bf1234 to 157af78 Compare June 13, 2022 18:49
@wantsui wantsui force-pushed the wantsui/fastapi-background-duration-update branch from 157af78 to 423ae28 Compare June 13, 2022 21:04
mabdinur
mabdinur previously approved these changes Jun 13, 2022
Copy link
Contributor

@mabdinur mabdinur left a comment

Choose a reason for hiding this comment

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

Nice fix!

Copy link
Contributor

@ZStriker19 ZStriker19 left a comment

Choose a reason for hiding this comment

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

This looks great! I just have a nitpick/question or two and then should be ready!

ddtrace/contrib/asgi/middleware.py Show resolved Hide resolved
ddtrace/contrib/asgi/middleware.py Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Jun 14, 2022

Codecov Report

Merging #3799 (d754419) into 1.x (5bd3f79) will increase coverage by 0.10%.
The diff coverage is 95.22%.

@@            Coverage Diff             @@
##              1.x    #3799      +/-   ##
==========================================
+ Coverage   78.00%   78.11%   +0.10%     
==========================================
  Files         672      674       +2     
  Lines       51777    52098     +321     
==========================================
+ Hits        40390    40695     +305     
- Misses      11387    11403      +16     
Impacted Files Coverage Δ
ddtrace/contrib/logging/patch.py 91.17% <0.00%> (-2.77%) ⬇️
ddtrace/profiling/profiler.py 0.00% <ø> (ø)
tests/tracer/telemetry/test_writer.py 92.53% <ø> (ø)
ddtrace/_logger.py 56.25% <56.25%> (ø)
ddtrace/__init__.py 100.00% <100.00%> (ø)
ddtrace/contrib/asgi/middleware.py 95.37% <100.00%> (+0.13%) ⬆️
ddtrace/contrib/redis/util.py 100.00% <100.00%> (ø)
ddtrace/internal/telemetry/writer.py 96.26% <100.00%> (+0.08%) ⬆️
tests/contrib/asgi/test_asgi.py 100.00% <100.00%> (ø)
tests/contrib/fastapi/app.py 93.33% <100.00%> (+0.65%) ⬆️
... and 5 more

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

Copy link
Member

@brettlangdon brettlangdon left a comment

Choose a reason for hiding this comment

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

looks great.

I am worried about a case where we might not finish the request span.

I also think if we can add test cases when more_data is present in the http response body events would be good as well.

ddtrace/contrib/asgi/middleware.py Show resolved Hide resolved
ddtrace/contrib/asgi/middleware.py Show resolved Hide resolved
tests/contrib/asgi/test_asgi.py Show resolved Hide resolved
ZStriker19
ZStriker19 previously approved these changes Jun 17, 2022
Copy link
Contributor

@ZStriker19 ZStriker19 left a comment

Choose a reason for hiding this comment

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

Nice tests for more_body ! Looks great!

Copy link
Member

@brettlangdon brettlangdon left a comment

Choose a reason for hiding this comment

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

minor nit pick over how you are asserting that the span was not finished, but otherwise everything looks great

tests/contrib/asgi/test_asgi.py Outdated Show resolved Hide resolved
tests/contrib/asgi/test_asgi.py Outdated Show resolved Hide resolved
tests/contrib/asgi/test_asgi.py Outdated Show resolved Hide resolved
wantsui and others added 4 commits June 21, 2022 09:14
Co-authored-by: Brett Langdon <me@brett.is>
Co-authored-by: Brett Langdon <me@brett.is>
Co-authored-by: Brett Langdon <me@brett.is>
@mergify mergify bot merged commit 5b97489 into DataDog:1.x Jun 21, 2022
@brettlangdon brettlangdon added this to the v1.3.0 milestone Jun 21, 2022
@wantsui wantsui deleted the wantsui/fastapi-background-duration-update branch June 21, 2022 16:00
@wantsui
Copy link
Collaborator Author

wantsui commented Jun 21, 2022

@Mergifyio backport 1.2

mergify bot pushed a commit that referenced this pull request Jun 21, 2022
… web requests (#3799)

Following up on #3023, currently libraries traced via the `asgi` integration, such as`fastapi.request` and `starlette.request` spans include the duration of background tasks. This PR updates the asgi middleware code so that spans end earlier by excluding background tasks to better represent the web requests response times.

**Current Behavior**
$$\begin{align}duration = {http response time} + {background tasks time}\end{align}$$

**What it looks like with the PR**
$$\begin{align}duration = {http response time}\end{align}$$

**Example:** An application return a "hello world!" to the user in $2 milliseconds$, but the events that happen in the background lasts another $8 seconds$.

With this PR, the span will report a duration of $2 milliseconds$, instead of $2 milliseconds + 8 seconds$.

Fixes #3023

Also fixes this failing test: https://app.circleci.com/pipelines/github/DataDog/dd-trace-py/18150/workflows/24caa985-5805-4bff-a4eb-5b143f24a2fb/jobs/1233079

## Checklist
- [ ] Library documentation is updated.
- [ ] [Corp site](https://github.com/DataDog/documentation/) documentation is updated (link to the PR).

## Reviewer Checklist
- [ ] Title is accurate.
- [ ] Description motivates each change.
- [ ] No unnecessary changes were introduced in this PR.
- [ ] PR cannot be broken up into smaller PRs.
- [ ] 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.
- [ ] Add to milestone.

(cherry picked from commit 5b97489)
@mergify
Copy link
Contributor

mergify bot commented Jun 21, 2022

backport 1.2

✅ Backports have been created

mergify bot added a commit that referenced this pull request Jun 21, 2022
… web requests (backport #3799) (#3840)

This is an automatic backport of pull request #3799 done by [Mergify](https://mergify.com).


---


<details>
<summary>Mergify commands and options</summary>

<br />

More conditions and actions can be found in the [documentation](https://docs.mergify.com/).

You can also trigger Mergify actions by commenting on this pull request:

- `@Mergifyio refresh` will re-evaluate the rules
- `@Mergifyio rebase` will rebase this PR on its base branch
- `@Mergifyio update` will merge the base branch into this PR
- `@Mergifyio backport <destination>` will backport this PR on `<destination>` branch

Additionally, on Mergify [dashboard](https://dashboard.mergify.com/) you can:

- look at your merge queues
- generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.com
</details>
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.

Starlette/Fastapi: endpoint duration includes the duration of background tasks
6 participants