Make sending counter metrics as a raw value configurable

CloudWatch dashboard doesn't have built in delta function. To allow
presenting counters as a graph add configuration parameter to send
delta counters instead of actual value

Feature: Issue 13218
Change-Id: I61026be71ee82d1bed6614f4517f48c547cbd7c2
diff --git a/src/main/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/GerritCloudwatchReporter.java b/src/main/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/GerritCloudwatchReporter.java
index 4803e14..fb9c1d1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/GerritCloudwatchReporter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/GerritCloudwatchReporter.java
@@ -43,7 +43,6 @@
             .convertDurationsTo(TimeUnit.MILLISECONDS)
             .filter(config.getExclusionFilter())
             .withZeroValuesSubmission()
-            .withReportRawCountValue()
             .withHighResolution();
 
     config
@@ -61,6 +60,10 @@
       cloudWatchReporterBuilder.withJvmMetrics();
     }
 
+    if (config.getReportRawCountValue()) {
+      cloudWatchReporterBuilder.withReportRawCountValue();
+    }
+
     cloudWatchReporter = cloudWatchReporterBuilder.build();
   }
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/GerritCloudwatchReporterConfig.java b/src/main/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/GerritCloudwatchReporterConfig.java
index 62535dd..fed875e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/GerritCloudwatchReporterConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/GerritCloudwatchReporterConfig.java
@@ -37,6 +37,7 @@
   protected static final String KEY_INITIAL_DELAY = "initialDelay";
   protected static final String KEY_EXCLUDE_METRICS = "excludeMetrics";
   protected static final String KEY_JVM_METRICS = "jvmMetrics";
+  protected static final String KEY_REPORT_RAW_COUNT_VALUE = "reportRawCountValue";
 
   protected static final String DEFAULT_NAMESPACE = "gerrit";
   protected static final String DEFAULT_EMPTY_STRING = "";
@@ -44,6 +45,7 @@
   protected static final Boolean DEFAULT_JVM_METRICS = false;
   protected static final Long DEFAULT_RATE_SECS = 60L;
   protected static final Integer DEFAULT_INITIAL_DELAY_SECS = 0;
+  protected static final Boolean DEFAULT_REPORT_RAW_COUNT_VALUE = false;
 
   private final int rate;
   private final String namespace;
@@ -51,6 +53,7 @@
   private final Boolean dryRun;
   private final MetricFilter exclusionFilter;
   private final Boolean jvmMetrics;
+  private final Boolean reportRawCountValue;
   private final Optional<String> maybeInstanceId;
 
   @Inject
@@ -83,6 +86,8 @@
                 TimeUnit.SECONDS);
 
     this.exclusionFilter = buildExclusionFilter(pluginConfig.getStringList(KEY_EXCLUDE_METRICS));
+    this.reportRawCountValue =
+        pluginConfig.getBoolean(KEY_REPORT_RAW_COUNT_VALUE, DEFAULT_REPORT_RAW_COUNT_VALUE);
   }
 
   public int getRate() {
@@ -121,4 +126,8 @@
   public Optional<String> getMaybeInstanceId() {
     return maybeInstanceId;
   }
+
+  public boolean getReportRawCountValue() {
+    return reportRawCountValue;
+  }
 }
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index b29d4d2..493a758 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -87,4 +87,11 @@
     * Example: "plugins.*"
 
 In case of invalid pattern, the plugin will fail to load and the relevant error will
-be logged in the _error_log_ file.
\ No newline at end of file
+be logged in the _error_log_ file.
+
+* `plugin.@PLUGIN@.reportRawCountValue` (Optional): Will report the raw value of count
+metrics instead of reporting only the count difference since the last report.
+
+   * Type: Boolean
+   * Default: false
+   * Example: true
\ No newline at end of file
diff --git a/src/test/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/GerritCloudwatchReporterConfigTest.java b/src/test/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/GerritCloudwatchReporterConfigTest.java
index f00ee2d..3866861 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/GerritCloudwatchReporterConfigTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/GerritCloudwatchReporterConfigTest.java
@@ -56,6 +56,8 @@
     assertThat(reporterConfig.getJvmMetrics())
         .isEqualTo(GerritCloudwatchReporterConfig.DEFAULT_JVM_METRICS);
     assertThat(reporterConfig.getMaybeInstanceId()).isEqualTo(Optional.empty());
+    assertThat(reporterConfig.getReportRawCountValue())
+        .isEqualTo(GerritCloudwatchReporterConfig.DEFAULT_REPORT_RAW_COUNT_VALUE);
   }
 
   @Test
@@ -66,6 +68,7 @@
     globalPluginConfig.setString(GerritCloudwatchReporterConfig.KEY_INITIAL_DELAY, "20s");
     globalPluginConfig.setBoolean(GerritCloudwatchReporterConfig.KEY_DRYRUN, true);
     globalPluginConfig.setBoolean(GerritCloudwatchReporterConfig.KEY_JVM_METRICS, true);
+    globalPluginConfig.setBoolean(GerritCloudwatchReporterConfig.KEY_REPORT_RAW_COUNT_VALUE, true);
 
     when(configFactory.getFromGerritConfig(PLUGIN_NAME)).thenReturn(globalPluginConfig);
     reporterConfig =
@@ -77,6 +80,7 @@
     assertThat(reporterConfig.getDryRun()).isTrue();
     assertThat(reporterConfig.getJvmMetrics()).isTrue();
     assertThat(reporterConfig.getMaybeInstanceId()).isEqualTo(Optional.of(gerritInstanceId));
+    assertThat(reporterConfig.getReportRawCountValue()).isTrue();
   }
 
   @Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/MetricsReporterCloudwatchIT.java b/src/test/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/MetricsReporterCloudwatchIT.java
index 94aaf21..ed377fa 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/MetricsReporterCloudwatchIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/metricsreportercloudwatch/MetricsReporterCloudwatchIT.java
@@ -75,6 +75,57 @@
   @Test
   @GerritConfig(name = "plugin.metrics-reporter-cloudwatch.dryrun", value = "true")
   @GerritConfig(name = "plugin.metrics-reporter-cloudwatch.rate", value = TEST_TIMEOUT)
+  public void shouldReportMetricValueAsDeltaToCloudwatch() throws Exception {
+    InMemoryLoggerAppender dryRunMetricsOutput = newInMemoryLogger();
+
+    testCounterMetric.incrementBy(5);
+
+    waitUntil(
+        () ->
+            dryRunMetricsOutput
+                .metricsStream()
+                .filter(l -> l.contains("MetricName=" + TEST_METRIC_NAME))
+                .anyMatch(l -> l.contains("Value=5.0")));
+
+    testCounterMetric.incrementBy(3);
+
+    waitUntil(
+        () ->
+            dryRunMetricsOutput
+                .metricsStream()
+                .filter(l -> l.contains("MetricName=" + TEST_METRIC_NAME))
+                .anyMatch(l -> l.contains("Value=3.0")));
+  }
+
+  @Test
+  @GerritConfig(name = "plugin.metrics-reporter-cloudwatch.dryrun", value = "true")
+  @GerritConfig(name = "plugin.metrics-reporter-cloudwatch.rate", value = TEST_TIMEOUT)
+  @GerritConfig(name = "plugin.metrics-reporter-cloudwatch.reportRawCountValue", value = "true")
+  public void shouldReportMetricValueAsRawToCloudwatch() throws Exception {
+    InMemoryLoggerAppender dryRunMetricsOutput = newInMemoryLogger();
+
+    testCounterMetric.incrementBy(5);
+
+    waitUntil(
+        () ->
+            dryRunMetricsOutput
+                .metricsStream()
+                .filter(l -> l.contains("MetricName=" + TEST_METRIC_NAME))
+                .anyMatch(l -> l.contains("Value=5.0")));
+
+    testCounterMetric.incrementBy(3);
+
+    waitUntil(
+        () ->
+            dryRunMetricsOutput
+                .metricsStream()
+                .filter(l -> l.contains("MetricName=" + TEST_METRIC_NAME))
+                .anyMatch(l -> l.contains("Value=8.0")));
+  }
+
+  @Test
+  @GerritConfig(name = "plugin.metrics-reporter-cloudwatch.dryrun", value = "true")
+  @GerritConfig(name = "plugin.metrics-reporter-cloudwatch.rate", value = TEST_TIMEOUT)
   public void shouldReportMetricValueToCloudwatch() throws Exception {
     InMemoryLoggerAppender dryRunMetricsOutput = newInMemoryLogger();