Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
memoize gauge snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
schlosna committed Sep 7, 2024
1 parent 9f4405b commit 660e148
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Snapshot;
import com.google.common.base.Suppliers;
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;
import java.time.Duration;
import java.util.function.Supplier;

public final class AutobatcherTelemetryComponents {
private final String safeLoggablePurpose;
Expand Down Expand Up @@ -83,24 +86,34 @@ public static AutobatcherTelemetryComponents create(
}

private void registerGauges() {
// capturing a snapshot is expensive, so memoize for 10 milliseconds to reduce overhead during metrics read 50%.
Duration memoizeDuration = Duration.ofMillis(10);
Supplier<Snapshot> waitTimerSnapshot = Suppliers.memoizeWithExpiration(waitTimer::getSnapshot, memoizeDuration);
Supplier<Snapshot> waitTimeHistogramSnapshot =
Suppliers.memoizeWithExpiration(waitTimeHistogram::getSnapshot, memoizeDuration);
Supplier<Snapshot> runningTimerSnapshot =
Suppliers.memoizeWithExpiration(runningTimer::getSnapshot, memoizeDuration);
Supplier<Snapshot> totalTimerSnapshot =
Suppliers.memoizeWithExpiration(totalTimer::getSnapshot, memoizeDuration);

overheadMetrics.waitTimeNanosP1(
(Gauge<Double>) () -> waitTimer.getSnapshot().getValue(0.01));
(Gauge<Double>) () -> waitTimerSnapshot.get().getValue(0.01));
overheadMetrics.waitTimeNanosMedian(
(Gauge<Double>) () -> waitTimer.getSnapshot().getValue(0.5));
(Gauge<Double>) () -> waitTimerSnapshot.get().getValue(0.5));

overheadMetrics.waitTimePercentageP1(
(Gauge<Double>) () -> waitTimeHistogram.getSnapshot().getValue(0.01));
(Gauge<Double>) () -> waitTimeHistogramSnapshot.get().getValue(0.01));
overheadMetrics.waitTimePercentageMedian(
(Gauge<Double>) () -> waitTimeHistogram.getSnapshot().getValue(0.5));
(Gauge<Double>) () -> waitTimeHistogramSnapshot.get().getValue(0.5));

overheadMetrics.runningTimeNanosP1(
(Gauge<Double>) () -> runningTimer.getSnapshot().getValue(0.01));
(Gauge<Double>) () -> runningTimerSnapshot.get().getValue(0.01));
overheadMetrics.runningTimeNanosMedian(
(Gauge<Double>) () -> runningTimer.getSnapshot().getValue(0.5));
(Gauge<Double>) () -> runningTimerSnapshot.get().getValue(0.5));

overheadMetrics.totalTimeNanosP1(
(Gauge<Double>) () -> totalTimer.getSnapshot().getValue(0.01));
(Gauge<Double>) () -> totalTimerSnapshot.get().getValue(0.01));
overheadMetrics.totalTimeNanosMedian(
(Gauge<Double>) () -> totalTimer.getSnapshot().getValue(0.5));
(Gauge<Double>) () -> totalTimerSnapshot.get().getValue(0.5));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,24 @@ private void assertWaitTimeAndRunningTimeAndTotalTimeMetricsAreProduced(
}

private void assertNoWaitTimeAndRunningTimeMetricsAreProduced(TaggedMetricRegistry registry) {
assertThat(getWaitTimeHistogram(registry)).isNull();
assertThat(getWaitTimePercentageHistogram(registry)).isNull();
assertThat(getRunningTimeHistogram(registry)).isNull();
assertThat(getTotalTimeHistogram(registry)).isNull();
assertThat(getWaitTimeHistogram(registry))
.satisfies(histogram -> assertThat(histogram.getCount()).isZero());
assertThat(getWaitTimePercentageHistogram(registry))
.satisfies(histogram -> assertThat(histogram.getCount()).isZero());
assertThat(getRunningTimeHistogram(registry))
.satisfies(histogram -> assertThat(histogram.getCount()).isZero());
assertThat(getTotalTimeHistogram(registry))
.satisfies(histogram -> assertThat(histogram.getCount()).isZero());
}

private void assertOnlyWaitTimeMetricsAreProduced(TaggedMetricRegistry registry, int waitTimeNanos) {
assertThat(getWaitTimeHistogram(registry).getSnapshot().getValues()).containsExactly(waitTimeNanos);
assertThat(getWaitTimePercentageHistogram(registry)).isNull();
assertThat(getRunningTimeHistogram(registry)).isNull();
assertThat(getTotalTimeHistogram(registry)).isNull();
assertThat(getWaitTimePercentageHistogram(registry))
.satisfies(histogram -> assertThat(histogram.getCount()).isZero());
assertThat(getRunningTimeHistogram(registry))
.satisfies(histogram -> assertThat(histogram.getCount()).isZero());
assertThat(getTotalTimeHistogram(registry))
.satisfies(histogram -> assertThat(histogram.getCount()).isZero());
}

private static Histogram getWaitTimeHistogram(TaggedMetricRegistry registry) {
Expand Down

0 comments on commit 660e148

Please sign in to comment.