Skip to content

fix: Duration including background task execution time - #353

Open
yourboirusty wants to merge 8 commits into
trallnag:masterfrom
yourboirusty:fix-duration-including-background-tasks
Open

fix: Duration including background task execution time#353
yourboirusty wants to merge 8 commits into
trallnag:masterfrom
yourboirusty:fix-duration-including-background-tasks

Conversation

@yourboirusty

@yourboirusty yourboirusty commented Jan 14, 2026

Copy link
Copy Markdown

What does this do?

  1. Tracks response end time in the send wrapper rather than measuring time in the finally after full app execution.
  2. Minor middleware refactor.

Why do we need it?

  1. To stop background task execution time from inflating response time metrics in most cases.
  2. Cyclomatic complexity was too large for pre-commit to pass. Took the liberty of refactoring a bit more than absolutely necessary.

Who is this for?

People who use relatively long-running background tasks but still want accurate response times for every endpoint.

Linked issues

Fixes #275

Reviewer notes

If the refactors are too heavy-handed I can tune it back, but it made sense to me to flatten out the low-hanging fruit while I was at it.

This does not cover two potential issues:

  • in progress tracker will still only decrement after background tasks have finished,
  • requests without response body will still count background tasks into their duration.

I thought it wouldn't cover responses without body, but tests disagree with me.

@yourboirusty yourboirusty changed the title Fix duration including background task execution time fix: Duration including background task execution time Jan 14, 2026
@yourboirusty
yourboirusty force-pushed the fix-duration-including-background-tasks branch from 0012908 to a103310 Compare January 14, 2026 13:23
@marrrcin

Copy link
Copy Markdown

@trallnag any plans to merge this?

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.

Instrumentator middleware includes BackgroundTask duration in http request latency metrics

3 participants