Skip to content

Expose measured duration on Timer context manager#1166

Merged
csmarchbanks merged 4 commits intoprometheus:masterfrom
vokracko:expose-timer-duration
Apr 24, 2026
Merged

Expose measured duration on Timer context manager#1166
csmarchbanks merged 4 commits intoprometheus:masterfrom
vokracko:expose-timer-duration

Conversation

@vokracko
Copy link
Copy Markdown
Contributor

Motivation

When you use .time() as a context manager, the duration is already computed internally but thrown away — if you want to log or reuse it, you end up measuring twice:

start = time.monotonic()
with REQUEST_LATENCY.time():
    response = handle_request()
logger.info("request took %.3fs", time.monotonic() - start)

That's redundant and slightly off (the two measurements don't cover the same interval). This change stores the observed value on the Timer so it can be read back after the block exits.

Usage

with REQUEST_LATENCY.time() as t:
    response = handle_request()

logger.info("request took %.3fs", t.duration)

t.duration is None before the block runs and holds the observed seconds (the same value passed to observe() / set()) afterwards.

Compatibility

Purely additive. Existing with metric.time(): usage is unchanged — the attribute is only visible if you bind the context manager with as.

Assigning the .time() context manager (with ... as t) now yields a Timer
whose .duration attribute holds the observed value in seconds after the
block exits. This lets callers reuse the measurement (logging, further
metrics) without calling default_timer() a second time.

Signed-off-by: Lukáš Vokráčko <lukas@vokracko.cz>
@vokracko vokracko force-pushed the expose-timer-duration branch from 0ced9a5 to b26a859 Compare April 23, 2026 07:46
Copy link
Copy Markdown
Member

@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

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

One small suggestion, but 👍, nice little quality of life improvement!

Comment thread docs/content/instrumenting/gauge.md Outdated
Comment thread docs/content/instrumenting/summary.md Outdated
Comment thread docs/content/instrumenting/histogram.md Outdated
vokracko and others added 3 commits April 24, 2026 07:18
Signed-off-by: Lukáš Vokráčko <lukas@vokracko.cz>
Signed-off-by: Lukáš Vokráčko <lukas@vokracko.cz>
Co-authored-by: Chris Marchbanks <csmarchbanks@gmail.com>
Signed-off-by: Lukáš Vokráčko <lukas@vokracko.cz>
@vokracko vokracko requested a review from csmarchbanks April 24, 2026 05:18
Copy link
Copy Markdown
Member

@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

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

Thanks!

@csmarchbanks csmarchbanks merged commit e75a74f into prometheus:master Apr 24, 2026
12 checks passed
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