diff --git a/README.md b/README.md index f4f801b..4854128 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,7 @@ The reporter provides a fine-grained configuration options through its builder t .withJvmMetrics() .withGlobalDimensions("Region=us-west-2", "Instance=stage") .withZeroValuesSubmission() + .withReportRawCountValue() .withDryRun() .build(); diff --git a/src/main/java/io/github/azagniotov/metrics/reporter/cloudwatch/CloudWatchReporter.java b/src/main/java/io/github/azagniotov/metrics/reporter/cloudwatch/CloudWatchReporter.java index 165ad84..b54f61c 100644 --- a/src/main/java/io/github/azagniotov/metrics/reporter/cloudwatch/CloudWatchReporter.java +++ b/src/main/java/io/github/azagniotov/metrics/reporter/cloudwatch/CloudWatchReporter.java @@ -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); } /** @@ -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; @@ -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 0.5 is included, it'll be * reported as median.This defaults to 0.75, 0.95 and 0.999. diff --git a/src/test/java/io/github/azagniotov/metrics/reporter/cloudwatch/CloudWatchReporterTest.java b/src/test/java/io/github/azagniotov/metrics/reporter/cloudwatch/CloudWatchReporterTest.java index 34cd74d..979526e 100644 --- a/src/test/java/io/github/azagniotov/metrics/reporter/cloudwatch/CloudWatchReporterTest.java +++ b/src/test/java/io/github/azagniotov/metrics/reporter/cloudwatch/CloudWatchReporterTest.java @@ -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) {