Expose measured duration on Timer context manager#1166
Merged
csmarchbanks merged 4 commits intoprometheus:masterfrom Apr 24, 2026
Merged
Expose measured duration on Timer context manager#1166csmarchbanks merged 4 commits intoprometheus:masterfrom
csmarchbanks merged 4 commits intoprometheus:masterfrom
Conversation
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>
0ced9a5 to
b26a859
Compare
Member
csmarchbanks
left a comment
There was a problem hiding this comment.
One small suggestion, but 👍, nice little quality of life improvement!
vokracko
commented
Apr 24, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:That's redundant and slightly off (the two measurements don't cover the same interval). This change stores the observed value on the
Timerso it can be read back after the block exits.Usage
t.durationisNonebefore the block runs and holds the observed seconds (the same value passed toobserve()/set()) afterwards.Compatibility
Purely additive. Existing
with metric.time():usage is unchanged — the attribute is only visible if you bind the context manager withas.