Skip to content

feature: record duration using monotonic time - #3399

Open
sfozz wants to merge 2 commits into
benoitc:masterfrom
sfozz:nano_duration_access_log
Open

feature: record duration using monotonic time#3399
sfozz wants to merge 2 commits into
benoitc:masterfrom
sfozz:nano_duration_access_log

Conversation

@sfozz

@sfozz sfozz commented May 16, 2025

Copy link
Copy Markdown

Adds nanosecond-resolution request timing to the access log and switches the underlying measurement to a monotonic
clock.

What's new

  • New %(N)s access-log atom for request time in nanoseconds. This lets gunicorn line up directly with other systems
    (tracing, metrics pipelines, various proxies) that already record duration at nanosecond resolution, so they agree
    without any unit conversion.
  • Monotonic timing for request duration is measured using time.monotonic_ns() instead of datetime.now(). Using the monotonic timer means that duration is recorded precisely and without the possibility of inaccurate records due to the system clock changing.

Details

  • request_time is now an integer count of nanoseconds rather than a timedelta. All existing atoms are derived from it
    with plain integer arithmetic:
    • T: seconds
    • M: milliseconds
    • D: microseconds
    • N: nanoseconds (new)
    • L: decimal seconds (to microsecond precision)
  • The statsd instrumentation is updated to compute its millisecond duration from the same nanosecond value.
  • Applies consistently across the sync, async, and gthread workers, plus the error-handling path.
  • Tests updated to pass nanosecond integers and to cover the new N atom.

Implements #3398

Comment thread gunicorn/workers/base.py Outdated
Comment thread gunicorn/glogging.py Outdated
@sfozz
sfozz force-pushed the nano_duration_access_log branch 7 times, most recently from c94a100 to 9a59cee Compare June 27, 2025 03:20
@sfozz
sfozz requested a review from pajod June 27, 2025 03:24
@sfozz

sfozz commented Jun 27, 2025

Copy link
Copy Markdown
Author

I've cleaned this up and made the change more intentional.

  • Cleaned up the atoms so that the math is easier to follow
  • updated the tests to handle the change

So that if you have other systems that record durations to this
resolution then they can be in alignment without needing calculation.
@sfozz
sfozz force-pushed the nano_duration_access_log branch from 9a59cee to 691f71d Compare June 23, 2026 10:38
@sfozz sfozz changed the title feature: Add request duration in nanoseconds feature: record duration using monotonic time Jun 23, 2026
@sfozz

sfozz commented Jun 23, 2026

Copy link
Copy Markdown
Author

Oh I see what you mean now... I've just tested extending the gunicorn.glogging.Logger which would have been a breaking change because of this PR!

I've updated the PR description to more acuuratly cover the purpose of the change.

@sfozz
sfozz force-pushed the nano_duration_access_log branch from eadeb5c to 1ea2f3b Compare June 23, 2026 21:20
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.

2 participants