aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMayur Kale <mayurkale@google.com>2018-10-17 22:09:40 -0700
committerGitHub <noreply@github.com>2018-10-17 22:09:40 -0700
commit30b613fcbf329bf130ae18267b431c4f820bbe28 (patch)
tree327465dd6ad7c9d34815ed0baa25209711560027
parent2f53b977da6b10b7f8e8c29d83f0478d470b229c (diff)
downloadopencensus-java-30b613fcbf329bf130ae18267b431c4f820bbe28.tar.gz
Plugs-in the DoubleGauge into the registry (#1503)
-rw-r--r--api/src/main/java/io/opencensus/metrics/DoubleGauge.java5
-rw-r--r--api/src/main/java/io/opencensus/metrics/MetricRegistry.java29
-rw-r--r--api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java48
-rw-r--r--impl_core/src/main/java/io/opencensus/implcore/metrics/MetricRegistryImpl.java16
-rw-r--r--impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java95
5 files changed, 185 insertions, 8 deletions
diff --git a/api/src/main/java/io/opencensus/metrics/DoubleGauge.java b/api/src/main/java/io/opencensus/metrics/DoubleGauge.java
index de2b053b..32759973 100644
--- a/api/src/main/java/io/opencensus/metrics/DoubleGauge.java
+++ b/api/src/main/java/io/opencensus/metrics/DoubleGauge.java
@@ -32,7 +32,7 @@ import javax.annotation.concurrent.ThreadSafe;
* private static final MetricRegistry metricRegistry = Metrics.getMetricRegistry();
*
* List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc"));
- * // TODO(mayurkale): Plugs-in the DoubleGauge into the registry.
+ *
* DoubleGauge gauge = metricRegistry.addDoubleGauge("queue_size",
* "Pending jobs", "1", labelKeys);
*
@@ -57,7 +57,6 @@ import javax.annotation.concurrent.ThreadSafe;
* List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("Name", "desc"));
* List<LabelValue> labelValues = Arrays.asList(LabelValue.create("Inbound"));
*
- * // TODO(mayurkale): Plugs-in the DoubleGauge into the registry.
* DoubleGauge gauge = metricRegistry.addDoubleGauge("queue_size",
* "Pending jobs", "1", labelKeys);
*
@@ -86,7 +85,7 @@ public abstract class DoubleGauge {
* method for manual operations.
*
* @param labelValues the list of label values. The number of label values must be the same to
- * that of the label keys.
+ * that of the label keys passed to {@link MetricRegistry#addDoubleGauge}.
* @return a {@code DoublePoint} the value of single gauge.
* @throws NullPointerException if {@code labelValues} is null OR any element of {@code
* labelValues} is null.
diff --git a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java
index 840ad787..557f886a 100644
--- a/api/src/main/java/io/opencensus/metrics/MetricRegistry.java
+++ b/api/src/main/java/io/opencensus/metrics/MetricRegistry.java
@@ -46,6 +46,23 @@ public abstract class MetricRegistry {
public abstract LongGauge addLongGauge(
String name, String description, String unit, List<LabelKey> labelKeys);
+ /**
+ * Builds a new double gauge to be added to the registry. This is more convenient form when you
+ * want to manually increase and decrease values as per your service requirements.
+ *
+ * @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 DoubleGauge addDoubleGauge(
+ String name, String description, String unit, List<LabelKey> labelKeys);
+
static MetricRegistry newNoopMetricRegistry() {
return new NoopMetricRegistry();
}
@@ -63,5 +80,17 @@ public abstract class MetricRegistry {
Utils.checkNotNull(unit, "unit"),
labelKeys);
}
+
+ @Override
+ public DoubleGauge addDoubleGauge(
+ String name, String description, String unit, List<LabelKey> labelKeys) {
+ Utils.checkListElementNotNull(
+ Utils.checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null.");
+ return DoubleGauge.newNoopDoubleGauge(
+ 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 0d90fa97..2de04096 100644
--- a/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java
+++ b/api/src/test/java/io/opencensus/metrics/MetricRegistryTest.java
@@ -32,6 +32,7 @@ public class MetricRegistryTest {
@Rule public ExpectedException thrown = ExpectedException.none();
private static final String NAME = "name";
+ private static final String NAME_2 = "name2";
private static final String DESCRIPTION = "description";
private static final String UNIT = "1";
private static final List<LabelKey> LABEL_KEY =
@@ -78,17 +79,60 @@ public class MetricRegistryTest {
}
@Test
+ public void noopAddDoubleGauge_NullName() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("name");
+ metricRegistry.addDoubleGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddDoubleGauge_NullDescription() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("description");
+ metricRegistry.addDoubleGauge(NAME_2, null, UNIT, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddDoubleGauge_NullUnit() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("unit");
+ metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, null, LABEL_KEY);
+ }
+
+ @Test
+ public void noopAddDoubleGauge_NullLabels() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelKeys");
+ metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, null);
+ }
+
+ @Test
+ public void noopAddDoubleGauge_WithNullElement() {
+ List<LabelKey> labelKeys = Collections.singletonList(null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelKey element should not be null.");
+ metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, labelKeys);
+ }
+
+ @Test
public void noopSameAs() {
LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
assertThat(longGauge.getDefaultTimeSeries()).isSameAs(longGauge.getDefaultTimeSeries());
assertThat(longGauge.getDefaultTimeSeries())
.isSameAs(longGauge.getOrCreateTimeSeries(LABEL_VALUES));
+
+ DoubleGauge doubleGauge = metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY);
+ assertThat(doubleGauge.getDefaultTimeSeries()).isSameAs(doubleGauge.getDefaultTimeSeries());
+ assertThat(doubleGauge.getDefaultTimeSeries())
+ .isSameAs(doubleGauge.getOrCreateTimeSeries(LABEL_VALUES));
}
@Test
public void noopInstanceOf() {
- LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
- assertThat(longGauge)
+ assertThat(metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY))
.isInstanceOf(LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY).getClass());
+ assertThat(metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY))
+ .isInstanceOf(
+ DoubleGauge.newNoopDoubleGauge(NAME_2, 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 8f8dab2b..c606d7e9 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,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import io.opencensus.common.Clock;
import io.opencensus.implcore.internal.Utils;
+import io.opencensus.metrics.DoubleGauge;
import io.opencensus.metrics.LabelKey;
import io.opencensus.metrics.LongGauge;
import io.opencensus.metrics.MetricRegistry;
@@ -57,6 +58,21 @@ public final class MetricRegistryImpl extends MetricRegistry {
return longGaugeMetric;
}
+ @Override
+ public DoubleGauge addDoubleGauge(
+ String name, String description, String unit, List<LabelKey> labelKeys) {
+ Utils.checkListElementNotNull(
+ checkNotNull(labelKeys, "labelKeys"), "labelKey element should not be null.");
+ DoubleGaugeImpl doubleGaugeMetric =
+ new DoubleGaugeImpl(
+ checkNotNull(name, "name"),
+ checkNotNull(description, "description"),
+ checkNotNull(unit, "unit"),
+ Collections.unmodifiableList(new ArrayList<LabelKey>(labelKeys)));
+ registeredMeters.registerMeter(name, doubleGaugeMetric);
+ return doubleGaugeMetric;
+ }
+
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 c7cd99be..36440898 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,9 +19,12 @@ package io.opencensus.implcore.metrics;
import static com.google.common.truth.Truth.assertThat;
import io.opencensus.common.Timestamp;
+import io.opencensus.metrics.DoubleGauge;
+import io.opencensus.metrics.DoubleGauge.DoublePoint;
import io.opencensus.metrics.LabelKey;
import io.opencensus.metrics.LabelValue;
import io.opencensus.metrics.LongGauge;
+import io.opencensus.metrics.LongGauge.LongPoint;
import io.opencensus.metrics.export.Metric;
import io.opencensus.metrics.export.MetricDescriptor;
import io.opencensus.metrics.export.MetricDescriptor.Type;
@@ -44,6 +47,7 @@ public class MetricRegistryImplTest {
@Rule public ExpectedException thrown = ExpectedException.none();
private static final String NAME = "name";
+ private static final String NAME_2 = "name2";
private static final String DESCRIPTION = "description";
private static final String UNIT = "1";
private static final List<LabelKey> LABEL_KEY =
@@ -55,8 +59,10 @@ public class MetricRegistryImplTest {
private final TestClock testClock = TestClock.create(TEST_TIME);
private final MetricRegistryImpl metricRegistry = new MetricRegistryImpl(testClock);
- private static final MetricDescriptor METRIC_DESCRIPTOR =
+ private static final MetricDescriptor LONG_METRIC_DESCRIPTOR =
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);
@Test
public void addLongGauge_NullName() {
@@ -95,6 +101,42 @@ public class MetricRegistryImplTest {
}
@Test
+ public void addDoubleGauge_NullName() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("name");
+ metricRegistry.addDoubleGauge(null, DESCRIPTION, UNIT, LABEL_KEY);
+ }
+
+ @Test
+ public void addDoubleGauge_NullDescription() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("description");
+ metricRegistry.addDoubleGauge(NAME_2, null, UNIT, LABEL_KEY);
+ }
+
+ @Test
+ public void addDoubleGauge_NullUnit() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("unit");
+ metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, null, LABEL_KEY);
+ }
+
+ @Test
+ public void addDoubleGauge_NullLabels() {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelKeys");
+ metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, null);
+ }
+
+ @Test
+ public void addDoubleGauge_WithNullElement() {
+ List<LabelKey> labelKeys = Collections.singletonList(null);
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("labelKey element should not be null.");
+ metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, labelKeys);
+ }
+
+ @Test
public void addLongGauge_GetMetrics() {
LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
longGauge.getOrCreateTimeSeries(LABEL_VALUES);
@@ -104,19 +146,66 @@ public class MetricRegistryImplTest {
assertThat(metricCollections)
.containsExactly(
Metric.createWithOneTimeSeries(
- METRIC_DESCRIPTOR,
+ LONG_METRIC_DESCRIPTOR,
TimeSeries.createWithOnePoint(
LABEL_VALUES, Point.create(Value.longValue(0), TEST_TIME), null)));
}
@Test
+ public void addDoubleGauge_GetMetrics() {
+ DoubleGauge doubleGauge = metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY);
+ doubleGauge.getOrCreateTimeSeries(LABEL_VALUES);
+ Collection<Metric> metricCollections = metricRegistry.getMetricProducer().getMetrics();
+ assertThat(metricCollections.size()).isEqualTo(1);
+ assertThat(metricCollections)
+ .containsExactly(
+ Metric.createWithOneTimeSeries(
+ DOUBLE_METRIC_DESCRIPTOR,
+ TimeSeries.createWithOnePoint(
+ LABEL_VALUES, Point.create(Value.doubleValue(0.0), TEST_TIME), null)));
+ }
+
+ @Test
public void empty_GetMetrics() {
assertThat(metricRegistry.getMetricProducer().getMetrics()).isEmpty();
}
@Test
public void checkInstanceOf() {
+ assertThat(metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY))
+ .isInstanceOf(LongGaugeImpl.class);
+ assertThat(metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY))
+ .isInstanceOf(DoubleGaugeImpl.class);
+ }
+
+ @Test
+ public void getMetrics() {
LongGauge longGauge = metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
- assertThat(longGauge).isInstanceOf(LongGaugeImpl.class);
+ LongPoint longPoint = longGauge.getOrCreateTimeSeries(LABEL_VALUES);
+ longPoint.set(200);
+ DoubleGauge doubleGauge = metricRegistry.addDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEY);
+ DoublePoint doublePoint = doubleGauge.getOrCreateTimeSeries(LABEL_VALUES);
+ doublePoint.set(-300.13);
+
+ Collection<Metric> metricCollections = metricRegistry.getMetricProducer().getMetrics();
+ assertThat(metricCollections.size()).isEqualTo(2);
+ assertThat(metricCollections)
+ .containsExactly(
+ Metric.createWithOneTimeSeries(
+ LONG_METRIC_DESCRIPTOR,
+ TimeSeries.createWithOnePoint(
+ LABEL_VALUES, Point.create(Value.longValue(200), TEST_TIME), null)),
+ Metric.createWithOneTimeSeries(
+ DOUBLE_METRIC_DESCRIPTOR,
+ TimeSeries.createWithOnePoint(
+ LABEL_VALUES, Point.create(Value.doubleValue(-300.13), TEST_TIME), null)));
+ }
+
+ @Test
+ public void registerDifferentMetricSameName() {
+ metricRegistry.addLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("A different metric with the same name already registered.");
+ metricRegistry.addDoubleGauge(NAME, DESCRIPTION, UNIT, LABEL_KEY);
}
}