SAMZA-2583: Fix NPE in update SamzaHistogram Gauge values (#1418)

* SAMZA-2583: Fix NPE in update SamzaHistogram Gauge values

SamzaHistogram gauges are instantiated via passed in metric registry
newGauge api. The new Gauge api in turn tries to instantiate
HistogramGauge. The newGauge api calls getValue on HistogramGauge
which in turn tries to update the Histogram Gauge values. This means
we are updating Histogram Gauge values in process of creating it.
As a fix, we add  null check in Gauge getValue to avoid NPE.
diff --git a/samza-api/src/main/java/org/apache/samza/metrics/SamzaHistogram.java b/samza-api/src/main/java/org/apache/samza/metrics/SamzaHistogram.java
index 38cf0fb..bdbe403 100644
--- a/samza-api/src/main/java/org/apache/samza/metrics/SamzaHistogram.java
+++ b/samza-api/src/main/java/org/apache/samza/metrics/SamzaHistogram.java
@@ -81,6 +81,14 @@
      * and do not have MetricsReporter to update the values.
      */
     public Double getValue() {
+      /*
+       * We cannot updateGaugeValues if the histogram gauges are being instantiated
+       * and getValue is called in this context.
+       */
+      if (gauges == null) {
+        return 0.0;
+      }
+
       updateGaugeValues(percentile);
       return super.getValue();
     }
diff --git a/samza-api/src/test/java/org/apache/samza/metrics/TestSamzaHistogram.java b/samza-api/src/test/java/org/apache/samza/metrics/TestSamzaHistogram.java
new file mode 100644
index 0000000..ef4fc20
--- /dev/null
+++ b/samza-api/src/test/java/org/apache/samza/metrics/TestSamzaHistogram.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.samza.metrics;
+
+import org.junit.Test;
+import org.mockito.stubbing.Answer;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+
+public class TestSamzaHistogram {
+  private static final String GROUP = "Group0";
+  private static final String METRIC_NAME = "Metric1";
+
+  private MetricsRegistry metricsRegistry;
+
+  @Test
+  public void testCreateHistogramGaugeNullCheck() {
+    metricsRegistry = mock(MetricsRegistry.class);
+
+    doAnswer((Answer<Gauge<Double>>) invocation -> {
+      Object[] args = invocation.getArguments();
+      return new Gauge<>((String) args[0], (Double) ((Gauge) args[1]).getValue());
+    }).when(metricsRegistry).newGauge(anyString(), any(Gauge.class));
+
+    SamzaHistogram histogram = new SamzaHistogram(metricsRegistry, GROUP, METRIC_NAME);
+    assertNotNull(histogram);
+  }
+}
\ No newline at end of file