Skip to content

Commit

Permalink
pageserver: return duration from StorageTimeMetricsTimer (#10468)
Browse files Browse the repository at this point in the history
## Problem

It's sometimes useful to obtain the elapsed duration from a
`StorageTimeMetricsTimer` for purposes beyond just recording it in
metrics (e.g. to log it).

Extracted from #10405.

## Summary of changes

Add `StorageTimeMetricsTimer.elapsed()` and return the duration from
`stop_and_record()`.
  • Loading branch information
erikgrinaker authored Jan 21, 2025
1 parent 7d4bfcd commit a75e11c
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions pageserver/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2550,12 +2550,19 @@ impl StorageTimeMetricsTimer {
}
}

/// Record the time from creation to now.
pub fn stop_and_record(self) {
let duration = self.start.elapsed().as_secs_f64();
self.metrics.timeline_sum.inc_by(duration);
/// Returns the elapsed duration of the timer.
pub fn elapsed(&self) -> Duration {
self.start.elapsed()
}

/// Record the time from creation to now and return it.
pub fn stop_and_record(self) -> Duration {
let duration = self.elapsed();
let seconds = duration.as_secs_f64();
self.metrics.timeline_sum.inc_by(seconds);
self.metrics.timeline_count.inc();
self.metrics.global_histogram.observe(duration);
self.metrics.global_histogram.observe(seconds);
duration
}

/// Turns this timer into a timer, which will always record -- usually this means recording
Expand All @@ -2575,6 +2582,14 @@ impl Drop for AlwaysRecordingStorageTimeMetricsTimer {
}
}

impl AlwaysRecordingStorageTimeMetricsTimer {
/// Returns the elapsed duration of the timer.
#[allow(unused)]
pub fn elapsed(&self) -> Duration {
self.0.as_ref().expect("not dropped yet").elapsed()
}
}

/// Timing facilities for an globally histogrammed metric, which is supported by per tenant and
/// timeline total sum and count.
#[derive(Clone, Debug)]
Expand Down

1 comment on commit a75e11c

@github-actions
Copy link

Choose a reason for hiding this comment

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

7370 tests run: 6984 passed, 1 failed, 385 skipped (full report)


Failures on Postgres 17

# Run all failed tests locally:
scripts/pytest -vv -n $(nproc) -k "test_scrubber_physical_gc_ancestors[debug-pg17-None]"
Flaky tests (4)

Postgres 17

Postgres 16

Postgres 15

Test coverage report is not available

The comment gets automatically updated with the latest test results
a75e11c at 2025-01-21T21:49:38.251Z :recycle:

Please sign in to comment.