Skip to content

Commit 9fcad08

Browse files
committed
add 0 check
1 parent 39de58e commit 9fcad08

3 files changed

Lines changed: 11 additions & 0 deletions

File tree

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/http/pipeline/stages/AsyncApiCallAttemptMetricCollectionStage.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ private void reportWriteThroughput(RequestExecutionContext context) {
123123
long firstByteTime = metrics.firstByteWrittenNanoTime().get();
124124
if (bytesWritten > 0 && firstByteTime > 0) {
125125
long lastByteTime = metrics.lastByteWrittenNanoTime().get();
126+
// Skip reporting if duration is zero
127+
if (firstByteTime == lastByteTime) {
128+
return;
129+
}
126130
double writeThroughput = MetricUtils.bytesPerSec(bytesWritten, firstByteTime, lastByteTime);
127131
context.attemptMetricCollector().reportMetric(CoreMetric.WRITE_THROUGHPUT, writeThroughput);
128132
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/http/pipeline/stages/HandleResponseStage.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ private void reportWriteThroughput(RequestExecutionContext context, MetricCollec
8585
long firstByteTime = metrics.firstByteWrittenNanoTime().get();
8686
if (bytesWritten > 0 && firstByteTime > 0) {
8787
long lastByteTime = metrics.lastByteWrittenNanoTime().get();
88+
// Skip reporting if duration is zero
89+
if (firstByteTime == lastByteTime) {
90+
return;
91+
}
8892
double writeThroughput = MetricUtils.bytesPerSec(bytesWritten, firstByteTime, lastByteTime);
8993
attemptMetricCollector.reportMetric(CoreMetric.WRITE_THROUGHPUT, writeThroughput);
9094
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/util/MetricUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ public static OptionalLong responseHeadersReadEndNanoTime(RequestExecutionContex
182182

183183
public static double bytesPerSec(long totalBytes, long nanoStart, long nanoEnd) {
184184
long duration = nanoEnd - nanoStart;
185+
if (duration <= 0) {
186+
return 0.0; // for defensive programming. This won't be reached ideally
187+
}
185188
double bytesPerNs = (double) totalBytes / duration;
186189
return bytesPerNs * ONE_SEC_IN_NS;
187190
}

0 commit comments

Comments
 (0)