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

AutobatcherTelemetryComponents stores metric references #7266

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,28 @@
package com.palantir.atlasdb.autobatch;

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;
private final AutobatchOverheadMetrics overheadMetrics;
private final Histogram waitTimer;
private final Histogram runningTimer;
private final Histogram waitTimeHistogram;
private final Histogram totalTimer;

private AutobatcherTelemetryComponents(String safeLoggablePurpose, AutobatchOverheadMetrics overheadMetrics) {
this.safeLoggablePurpose = safeLoggablePurpose;
this.overheadMetrics = overheadMetrics;
this.waitTimer = overheadMetrics.waitTimeNanos();
this.runningTimer = overheadMetrics.runningTimeNanos();
this.waitTimeHistogram = overheadMetrics.waitTimePercentage();
this.totalTimer = overheadMetrics.totalTimeNanos();
}

String getSafeLoggablePurpose() {
Expand All @@ -45,19 +57,19 @@ void markWaitingTimeAndRunningTimeMetrics(Duration waitTime, Duration runningTim
}

void markWaitingTimeMetrics(Duration waitTime) {
overheadMetrics.waitTimeNanos().update(waitTime.toNanos());
this.waitTimer.update(waitTime.toNanos());
}

private void markRunningTimeMetrics(Duration runningTime) {
overheadMetrics.runningTimeNanos().update(runningTime.toNanos());
this.runningTimer.update(runningTime.toNanos());
}

private void markTotalTimeMetrics(long totalTimeNanos) {
overheadMetrics.totalTimeNanos().update(totalTimeNanos);
this.totalTimer.update(totalTimeNanos);
}

private void markWaitingTimePercentage(long waitTimePercentage) {
overheadMetrics.waitTimePercentage().update(waitTimePercentage);
this.waitTimeHistogram.update(waitTimePercentage);
}

public static AutobatcherTelemetryComponents create(
Expand All @@ -67,26 +79,41 @@ public static AutobatcherTelemetryComponents create(
.operationType(safeLoggablePurpose)
.build();

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

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

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

overheadMetrics.totalTimeNanosP1((Gauge<Double>)
() -> overheadMetrics.totalTimeNanos().getSnapshot().getValue(0.01));
overheadMetrics.totalTimeNanosMedian((Gauge<Double>)
() -> overheadMetrics.totalTimeNanos().getSnapshot().getValue(0.5));
AutobatcherTelemetryComponents autobatcherTelemetry =
new AutobatcherTelemetryComponents(safeLoggablePurpose, overheadMetrics);
autobatcherTelemetry.registerGauges();
return autobatcherTelemetry;
}

return new AutobatcherTelemetryComponents(safeLoggablePurpose, overheadMetrics);
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>) () -> waitTimerSnapshot.get().getValue(0.01));
overheadMetrics.waitTimeNanosMedian(
(Gauge<Double>) () -> waitTimerSnapshot.get().getValue(0.5));

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

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

overheadMetrics.totalTimeNanosP1(
(Gauge<Double>) () -> totalTimerSnapshot.get().getValue(0.01));
overheadMetrics.totalTimeNanosMedian(
(Gauge<Double>) () -> totalTimerSnapshot.get().getValue(0.5));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,25 @@ private void assertTotalTimeMetricsAreReported(TaggedMetricRegistry registry, Lo
}

private void assertWaitTimeMetricsAreNotReported(TaggedMetricRegistry registry) {
assertThat(getWaitTimeHistogram(registry)).isNull();
assertThat(getWaitTimeHistogram(registry)).isNotNull().satisfies(histogram -> assertThat(histogram.getCount())
.isZero());
}

private void assertRunningTimeMetricsAreNotReported(TaggedMetricRegistry registry) {
assertThat(getRunningTimeHistogram(registry)).isNull();
assertThat(getRunningTimeHistogram(registry))
.isNotNull()
.satisfies(histogram -> assertThat(histogram.getCount()).isZero());
}

private void assertWaitTimePercentageMetricsAreNotReported(TaggedMetricRegistry registry) {
assertThat(getWaitTimePercentageHistogram(registry)).isNull();
assertThat(getWaitTimePercentageHistogram(registry))
.isNotNull()
.satisfies(histogram -> assertThat(histogram.getCount()).isZero());
}

private void assertTotalTimeMetricsAreNotReported(TaggedMetricRegistry registry) {
assertThat(getTotalTimeHistogram(registry)).isNull();
assertThat(getTotalTimeHistogram(registry)).isNotNull().satisfies(histogram -> assertThat(histogram.getCount())
.isZero());
}

private static Histogram getWaitTimeHistogram(TaggedMetricRegistry registry) {
Expand Down
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