Skip to content

Commit

Permalink
add option to report raw count values instead of delta values
Browse files Browse the repository at this point in the history
  • Loading branch information
fyi-coursera committed Aug 14, 2018
1 parent cf7e024 commit db4df5f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,15 @@ private void processCounter(final String metricName, final Counting counter, fin
lastCount = 0L;
}

// Only submit metrics that have changed - let's save some money!
final long delta = currentCount - lastCount;
stageMetricDatum(true, metricName, delta, StandardUnit.Count, DIMENSION_COUNT, metricData);
final long reportValue;
if (builder.withReportRawCountValue) {
reportValue = currentCount;
} else {
// Only submit metrics that have changed - let's save some money!
reportValue = currentCount - lastCount;
}

stageMetricDatum(true, metricName, reportValue, StandardUnit.Count, DIMENSION_COUNT, metricData);
}

/**
Expand Down Expand Up @@ -464,6 +470,7 @@ public static class Builder {
private boolean withZeroValuesSubmission;
private boolean withStatisticSet;
private boolean withJvmMetrics;
private boolean withReportRawCountValue;
private MetricFilter metricFilter;
private TimeUnit rateUnit;
private TimeUnit durationUnit;
Expand Down Expand Up @@ -669,6 +676,18 @@ public Builder withZeroValuesSubmission() {
return this;
}

/**
* Will report the raw value of count metrics instead of reporting only the count difference since the last
* report
* {@code false} by default.
*
* @return {@code this}
*/
public Builder withReportRawCountValue() {
withReportRawCountValue = true;
return this;
}

/**
* The {@link Histogram} and {@link Timer} percentiles to send. If <code>0.5</code> is included, it'll be
* reported as <code>median</code>.This defaults to <code>0.75, 0.95 and 0.999</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,31 @@ public void shouldReportHistogramSubsequentSnapshotValues_SumMaxMinValues() thro
assertThat(secondMetricData.getStatisticValues().getSampleCount().intValue()).isEqualTo(4);
assertThat(secondMetricData.getStatisticValues().getSum().intValue()).isEqualTo(115);
assertThat(secondMetricData.getUnit()).isEqualTo(None.toString());
}

@Test
public void shouldNotReportCounterValueDeltaWhenReportingRawCountValue() throws Exception {
metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
final CloudWatchReporter cloudWatchReporter = reporterBuilder.withReportRawCountValue().build();

cloudWatchReporter.report();
MetricDatum metricDatum = firstMetricDatumFromCapturedRequest();
assertThat(metricDatum.getValue().intValue()).isEqualTo(2);
metricDataRequestCaptor.getAllValues().clear();

metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();

cloudWatchReporter.report();
metricDatum = firstMetricDatumFromCapturedRequest();
assertThat(metricDatum.getValue().intValue()).isEqualTo(8);

verify(mockAmazonCloudWatchAsyncClient, times(2)).putMetricDataAsync(any(PutMetricDataRequest.class));
}

private MetricDatum metricDatumByDimensionFromCapturedRequest(final String dimensionValue) {
Expand Down

0 comments on commit db4df5f

Please sign in to comment.