aboutsummaryrefslogtreecommitdiff
path: root/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java
diff options
context:
space:
mode:
authorMayur Kale <mayurkale@google.com>2018-10-19 10:05:26 -0700
committerGitHub <noreply@github.com>2018-10-19 10:05:26 -0700
commit633fde4378905bffb967b30857257427cced4228 (patch)
treeede84fcf0a9687d4907ae5f8a4788271d62e0922 /impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java
parentaf1358b7e29d15257cff835c25ee3b38382c3c9b (diff)
downloadopencensus-java-633fde4378905bffb967b30857257427cced4228.tar.gz
Plugs-in the DerivedLongGauge and DerivedDoubleGauge into the registry (#1505)
* Plugs-in the DerivedLongGauge into the registry * Plugs-in the DerivedDoubleGauge into the registry
Diffstat (limited to 'impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java')
-rw-r--r--impl_core/src/test/java/io/opencensus/implcore/metrics/MetricRegistryImplTest.java149
1 files changed, 147 insertions, 2 deletions
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 36440898..68bfda31 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 @@ package io.opencensus.implcore.metrics;
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 @@ public class MetricRegistryImplTest {
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 @@ public class MetricRegistryImplTest {
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 @@ public class MetricRegistryImplTest {
}
@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 @@ public class MetricRegistryImplTest {
}
@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 @@ public class MetricRegistryImplTest {
.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 @@ public class MetricRegistryImplTest {
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 @@ public class MetricRegistryImplTest {
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