Plugs-in the DerivedLongGauge and DerivedDoubleGauge into the registry (#1505)

* Plugs-in the DerivedLongGauge into the registry

* Plugs-in the DerivedDoubleGauge into the registry
diff --git a/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java b/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java
index 10119ee..3aaca15 100644
--- a/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java
+++ b/api/src/main/java/io/opencensus/metrics/DerivedDoubleGauge.java
@@ -40,7 +40,6 @@
  *   List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc"));
  *   List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound"));
  *
- *   // TODO(mayurkale): Plugs-in the DerivedDoubleGauge into the registry.
  *   DerivedDoubleGauge gauge = metricRegistry.addDerivedDoubleGauge(
  *       "queue_size", "Pending jobs in a queue", "1", labelKeys);
  *
diff --git a/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java b/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java
index 43943fa..621873f 100644
--- a/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java
+++ b/api/src/main/java/io/opencensus/metrics/DerivedLongGauge.java
@@ -40,7 +40,6 @@
  *   List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc"));
  *   List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound"));
  *
- *   // TODO(mayurkale): Plugs-in the DerivedLongGauge into the registry.
  *   DerivedLongGauge gauge = metricRegistry.addDerivedLongGauge(
  *       "queue_size", "Pending jobs in a queue", "1", labelKeys);
  *
diff --git a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java
index 557f886..5be1559 100644
--- a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java
+++ b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java
@@ -17,6 +17,8 @@
 package io.opencensus.metrics;
 
 import io.opencensus.common.ExperimentalApi;
+import io.opencensus.common.ToDoubleFunction;
+import io.opencensus.common.ToLongFunction;
 import io.opencensus.internal.Utils;
 import java.util.List;
 
@@ -63,6 +65,40 @@
   public abstract DoubleGauge addDoubleGauge(
       String name, String description, String unit, List<LabelKey> labelKeys);
 
+  /**
+   * Builds a new derived long gauge to be added to the registry. This is more convenient form when
+   * you want to define a gauge by executing a {@link ToLongFunction} on an object.
+   *
+   * @param name the name of the metric.
+   * @param description the description of the metric.
+   * @param unit the unit of the metric.
+   * @param labelKeys the list of the label keys.
+   * @throws NullPointerException if {@code labelKeys} is null OR any element of {@code labelKeys}
+   *     is null OR {@code name}, {@code description}, {@code unit} is null.
+   * @throws IllegalArgumentException if different metric with the same name already registered.
+   * @since 0.17
+   */
+  @ExperimentalApi
+  public abstract DerivedLongGauge addDerivedLongGauge(
+      String name, String description, String unit, List<LabelKey> labelKeys);
+
+  /**
+   * Builds a new derived double gauge to be added to the registry. This is more convenient form
+   * when you want to define a gauge by executing a {@link ToDoubleFunction} on an object.
+   *
+   * @param name the name of the metric.
+   * @param description the description of the metric.
+   * @param unit the unit of the metric.
+   * @param labelKeys the list of the label keys.
+   * @throws NullPointerException if {@code labelKeys} is null OR any element of {@code labelKeys}
+   *     is null OR {@code name}, {@code description}, {@code unit} is null.
+   * @throws IllegalArgumentException if different metric with the same name already registered.
+   * @since 0.17
+   */
+  @ExperimentalApi
+  public abstract DerivedDoubleGauge addDerivedDoubleGauge(
+      String name, String description, String unit, List<LabelKey> labelKeys);
+
   static MetricRegistry newNoopMetricRegistry() {
     return new NoopMetricRegistry();
   }
@@ -92,5 +128,29 @@
           Utils.checkNotNull(unit, "unit"),
           labelKeys);
     }
+
+    @Override
+    public DerivedLongGauge addDerivedLongGauge(
+        String name, String description, String unit, List<LabelKey> labelKeys) {
+      Utils.checkListElementNotNull(
+          Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null.");
+      return DerivedLongGauge.newNoopDerivedLongGauge(
+          Utils.checkNotNull(name, "name"),
+          Utils.checkNotNull(description, "description"),
+          Utils.checkNotNull(unit, "unit"),
+          labelKeys);
+    }
+
+    @Override
+    public DerivedDoubleGauge addDerivedDoubleGauge(
+        String name, String description, String unit, List<LabelKey> labelKeys) {
+      Utils.checkListElementNotNull(
+          Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null.");
+      return DerivedDoubleGauge.newNoopDerivedDoubleGauge(
+          Utils.checkNotNull(name, "name"),
+          Utils.checkNotNull(description, "description"),
+          Utils.checkNotNull(unit, "unit"),
+          labelKeys);
+    }
   }
 }
diff --git a/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java b/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java
index 2de0409..d8a26cc 100644
--- a/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java
+++ b/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java
@@ -33,6 +33,8 @@
 
   private static final String NAME = "name";
   private static final String NAME_2 = "name2";
+  private static final String NAME_3 = "name3";
+  private static final String NAME_4 = "name4";
   private static final String DESCRIPTION = "description";
   private static final String UNIT = "1";
   private static final List<LabelKey> LABEL_KEY =
@@ -115,6 +117,78 @@
   }
 
   @Test
+  public void noopAddDerivedLongGauge_NullName() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("name");
+    metricRegistry.addDerivedLongGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
+  }
+
+  @Test
+  public void noopAddDerivedLongGauge_NullDescription() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("description");
+    metricRegistry.addDerivedLongGauge(NAME_3, null, UNIT, LABEL_KEY);
+  }
+
+  @Test
+  public void noopAddDerivedLongGauge_NullUnit() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("unit");
+    metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, null, LABEL_KEY);
+  }
+
+  @Test
+  public void noopAddDerivedLongGauge_NullLabels() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("labelKeys");
+    metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, null);
+  }
+
+  @Test
+  public void noopAddDerivedLongGauge_WithNullElement() {
+    List<LabelKey> labelKeys = Collections.singletonList(null);
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("labelKey element should not be null.");
+    metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, labelKeys);
+  }
+
+  @Test
+  public void noopAddDerivedDoubleGauge_NullName() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("name");
+    metricRegistry.addDerivedDoubleGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
+  }
+
+  @Test
+  public void noopAddDerivedDoubleGauge_NullDescription() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("description");
+    metricRegistry.addDerivedDoubleGauge(NAME_4, null, UNIT, LABEL_KEY);
+  }
+
+  @Test
+  public void noopAddDerivedDoubleGauge_NullUnit() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("unit");
+    metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, null, LABEL_KEY);
+  }
+
+  @Test
+  public void noopAddDerivedDoubleGauge_NullLabels() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("labelKeys");
+    metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, null);
+  }
+
+  @Test
+  public void noopAddDerivedDoubleGauge_WithNullElement() {
+    List<LabelKey> labelKeys = Collections.singletonList(null);
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("labelKey element should not be null.");
+    metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, labelKeys);
+  }
+
+  @Test
   public void noopSameAs() {
     LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
     assertThat(longGauge.getDefaultTimeSeries()).isSameAs(longGauge.getDefaultTimeSeries());
@@ -134,5 +208,13 @@
     assertThat(metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY))
         .isInstanceOf(
             DoubleGauge.newNoopDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY).getClass());
+    assertThat(metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, LABEL_KEY))
+        .isInstanceOf(
+            DerivedLongGauge.newNoopDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, LABEL_KEY)
+                .getClass());
+    assertThat(metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, LABEL_KEY))
+        .isInstanceOf(
+            DerivedDoubleGauge.newNoopDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, LABEL_KEY)
+                .getClass());
   }
 }
diff --git a/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java b/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java
index c606d7e..1a301ec 100644
--- a/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java
+++ b/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java
@@ -20,6 +20,8 @@
 
 import io.opencensus.common.Clock;
 import io.opencensus.implcore.internal.Utils;
+import io.opencensus.metrics.DerivedDoubleGauge;
+import io.opencensus.metrics.DerivedLongGauge;
 import io.opencensus.metrics.DoubleGauge;
 import io.opencensus.metrics.LabelKey;
 import io.opencensus.metrics.LongGauge;
@@ -73,6 +75,36 @@
     return doubleGaugeMetric;
   }
 
+  @Override
+  public DerivedLongGauge addDerivedLongGauge(
+      String name, String description, String unit, List<LabelKey> labelKeys) {
+    Utils.checkListElementNotNull(
+        checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null.");
+    DerivedLongGaugeImpl derivedLongGauge =
+        new DerivedLongGaugeImpl(
+            checkNotNull(name, "name"),
+            checkNotNull(description, "description"),
+            checkNotNull(unit, "unit"),
+            Collections.unmodifiableList(new ArrayList<LabelKey>(labelKeys)));
+    registeredMeters.registerMeter(name, derivedLongGauge);
+    return derivedLongGauge;
+  }
+
+  @Override
+  public DerivedDoubleGauge addDerivedDoubleGauge(
+      String name, String description, String unit, List<LabelKey> labelKeys) {
+    Utils.checkListElementNotNull(
+        checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null.");
+    DerivedDoubleGaugeImpl derivedDoubleGauge =
+        new DerivedDoubleGaugeImpl(
+            checkNotNull(name, "name"),
+            checkNotNull(description, "description"),
+            checkNotNull(unit, "unit"),
+            Collections.unmodifiableList(new ArrayList<LabelKey>(labelKeys)));
+    registeredMeters.registerMeter(name, derivedDoubleGauge);
+    return derivedDoubleGauge;
+  }
+
   private static final class RegisteredMeters {
     private volatile Map<String, Meter> registeredMeters = Collections.emptyMap();
 
diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java
index 3644089..68bfda3 100644
--- a/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java
+++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java
@@ -19,6 +19,10 @@
 import static com.google.common.truth.Truth.assertThat;
 
 import io.opencensus.common.Timestamp;
+import io.opencensus.common.ToDoubleFunction;
+import io.opencensus.common.ToLongFunction;
+import io.opencensus.metrics.DerivedDoubleGauge;
+import io.opencensus.metrics.DerivedLongGauge;
 import io.opencensus.metrics.DoubleGauge;
 import io.opencensus.metrics.DoubleGauge.DoublePoint;
 import io.opencensus.metrics.LabelKey;
@@ -48,6 +52,8 @@
 
   private static final String NAME = "name";
   private static final String NAME_2 = "name2";
+  private static final String NAME_3 = "name3";
+  private static final String NAME_4 = "name4";
   private static final String DESCRIPTION = "description";
   private static final String UNIT = "1";
   private static final List<LabelKey> LABEL_KEY =
@@ -63,6 +69,25 @@
       MetricDescriptor.create(NAME, DESCRIPTION, UNIT, Type.GAUGE_INT64, LABEL_KEY);
   private static final MetricDescriptor DOUBLE_METRIC_DESCRIPTOR =
       MetricDescriptor.create(NAME_2, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, LABEL_KEY);
+  private static final MetricDescriptor DERIVED_LONG_METRIC_DESCRIPTOR =
+      MetricDescriptor.create(NAME_3, DESCRIPTION, UNIT, Type.GAUGE_INT64, LABEL_KEY);
+  private static final MetricDescriptor DERIVED_DOUBLE_METRIC_DESCRIPTOR =
+      MetricDescriptor.create(NAME_4, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, LABEL_KEY);
+
+  private static final ToLongFunction<Object> longFunction =
+      new ToLongFunction<Object>() {
+        @Override
+        public long applyAsLong(Object value) {
+          return 5;
+        }
+      };
+  private static final ToDoubleFunction<Object> doubleFunction =
+      new ToDoubleFunction<Object>() {
+        @Override
+        public double applyAsDouble(Object value) {
+          return 5.0;
+        }
+      };
 
   @Test
   public void addLongGauge_NullName() {
@@ -137,6 +162,78 @@
   }
 
   @Test
+  public void addDerivedLongGauge_NullName() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("name");
+    metricRegistry.addDerivedLongGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
+  }
+
+  @Test
+  public void addDerivedLongGauge_NullDescription() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("description");
+    metricRegistry.addDerivedLongGauge(NAME_3, null, UNIT, LABEL_KEY);
+  }
+
+  @Test
+  public void addDerivedLongGauge_NullUnit() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("unit");
+    metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, null, LABEL_KEY);
+  }
+
+  @Test
+  public void addDerivedLongGauge_NullLabels() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("labelKeys");
+    metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, null);
+  }
+
+  @Test
+  public void addDerivedLongGauge_WithNullElement() {
+    List<LabelKey> labelKeys = Collections.singletonList(null);
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("labelKey element should not be null.");
+    metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, labelKeys);
+  }
+
+  @Test
+  public void addDerivedDoubleGauge_NullName() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("name");
+    metricRegistry.addDerivedDoubleGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
+  }
+
+  @Test
+  public void addDerivedDoubleGauge_NullDescription() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("description");
+    metricRegistry.addDerivedDoubleGauge(NAME_4, null, UNIT, LABEL_KEY);
+  }
+
+  @Test
+  public void addDerivedDoubleGauge_NullUnit() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("unit");
+    metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, null, LABEL_KEY);
+  }
+
+  @Test
+  public void addDerivedDoubleGauge_NullLabels() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("labelKeys");
+    metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, null);
+  }
+
+  @Test
+  public void addDerivedDoubleGauge_WithNullElement() {
+    List<LabelKey> labelKeys = Collections.singletonList(null);
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("labelKey element should not be null.");
+    metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, labelKeys);
+  }
+
+  @Test
   public void addLongGauge_GetMetrics() {
     LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
     longGauge.getOrCreateTimeSeries(LABEL_VALUES);
@@ -166,6 +263,36 @@
   }
 
   @Test
+  public void addDerivedLongGauge_GetMetrics() {
+    DerivedLongGauge derivedLongGauge =
+        metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, LABEL_KEY);
+    derivedLongGauge.createTimeSeries(LABEL_VALUES, null, longFunction);
+    Collection<Metric> metricCollections = metricRegistry.getMetricProducer().getMetrics();
+    assertThat(metricCollections.size()).isEqualTo(1);
+    assertThat(metricCollections)
+        .containsExactly(
+            Metric.createWithOneTimeSeries(
+                DERIVED_LONG_METRIC_DESCRIPTOR,
+                TimeSeries.createWithOnePoint(
+                    LABEL_VALUES, Point.create(Value.longValue(5), TEST_TIME), null)));
+  }
+
+  @Test
+  public void addDerivedDoubleGauge_GetMetrics() {
+    DerivedDoubleGauge derivedDoubleGauge =
+        metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, LABEL_KEY);
+    derivedDoubleGauge.createTimeSeries(LABEL_VALUES, null, doubleFunction);
+    Collection<Metric> metricCollections = metricRegistry.getMetricProducer().getMetrics();
+    assertThat(metricCollections.size()).isEqualTo(1);
+    assertThat(metricCollections)
+        .containsExactly(
+            Metric.createWithOneTimeSeries(
+                DERIVED_DOUBLE_METRIC_DESCRIPTOR,
+                TimeSeries.createWithOnePoint(
+                    LABEL_VALUES, Point.create(Value.doubleValue(5.0), TEST_TIME), null)));
+  }
+
+  @Test
   public void empty_GetMetrics() {
     assertThat(metricRegistry.getMetricProducer().getMetrics()).isEmpty();
   }
@@ -176,6 +303,10 @@
         .isInstanceOf(LongGaugeImpl.class);
     assertThat(metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY))
         .isInstanceOf(DoubleGaugeImpl.class);
+    assertThat(metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, LABEL_KEY))
+        .isInstanceOf(DerivedLongGaugeImpl.class);
+    assertThat(metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, LABEL_KEY))
+        .isInstanceOf(DerivedDoubleGaugeImpl.class);
   }
 
   @Test
@@ -186,9 +317,15 @@
     DoubleGauge doubleGauge = metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY);
     DoublePoint doublePoint = doubleGauge.getOrCreateTimeSeries(LABEL_VALUES);
     doublePoint.set(-300.13);
+    DerivedLongGauge derivedLongGauge =
+        metricRegistry.addDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, LABEL_KEY);
+    derivedLongGauge.createTimeSeries(LABEL_VALUES, null, longFunction);
+    DerivedDoubleGauge derivedDoubleGauge =
+        metricRegistry.addDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, LABEL_KEY);
+    derivedDoubleGauge.createTimeSeries(LABEL_VALUES, null, doubleFunction);
 
     Collection<Metric> metricCollections = metricRegistry.getMetricProducer().getMetrics();
-    assertThat(metricCollections.size()).isEqualTo(2);
+    assertThat(metricCollections.size()).isEqualTo(4);
     assertThat(metricCollections)
         .containsExactly(
             Metric.createWithOneTimeSeries(
@@ -198,7 +335,15 @@
             Metric.createWithOneTimeSeries(
                 DOUBLE_METRIC_DESCRIPTOR,
                 TimeSeries.createWithOnePoint(
-                    LABEL_VALUES, Point.create(Value.doubleValue(-300.13), TEST_TIME), null)));
+                    LABEL_VALUES, Point.create(Value.doubleValue(-300.13), TEST_TIME), null)),
+            Metric.createWithOneTimeSeries(
+                DERIVED_LONG_METRIC_DESCRIPTOR,
+                TimeSeries.createWithOnePoint(
+                    LABEL_VALUES, Point.create(Value.longValue(5), TEST_TIME), null)),
+            Metric.createWithOneTimeSeries(
+                DERIVED_DOUBLE_METRIC_DESCRIPTOR,
+                TimeSeries.createWithOnePoint(
+                    LABEL_VALUES, Point.create(Value.doubleValue(5.0), TEST_TIME), null)));
   }
 
   @Test